1
0

41 lines
920 B
Ruby

require 'csv'
module PlanetExpress
class ShipmentReader
include Enumerable
attr_reader :input, :shipping_option_repository
def initialize(input, shipping_option_repository)
@input = input
@shipping_option_repository = shipping_option_repository
end
def each
CSV.new(input, headers: false, col_sep: ' ').each do |row|
date = parse_date(row)
shipping_option = find_shipping_option(row)
if date.nil? || shipping_option.nil?
yield InvalidShipment.new(row.join(" "))
else
yield Shipment.new(date, shipping_option)
end
end
end
private
def find_shipping_option(row)
shipping_option_repository.find_by_provider_and_package_size(row[2], row[1])
end
def parse_date(row)
begin
return Date.iso8601(row[0])
rescue ArgumentError => _e
nil
end
end
end
end