1
0
2021-11-20 21:54:02 +01:00

41 lines
909 B
Ruby

# frozen_string_literal: true
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)
Date.strptime(row[0], "%Y-%m-%d")
rescue
nil
end
end
end