Add reader for shipments
This commit is contained in:
parent
4afb514743
commit
2ce3c4d826
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
require_relative "planet_express_express/version"
|
require_relative "planet_express_express/version"
|
||||||
require_relative "planet_express_express/constants"
|
require_relative "planet_express_express/constants"
|
||||||
|
require_relative "planet_express_express/shipment_reader"
|
||||||
|
|
||||||
module PlanetExpressExpress
|
module PlanetExpressExpress
|
||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
|
66
lib/planet_express_express/shipment_reader.rb
Normal file
66
lib/planet_express_express/shipment_reader.rb
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
module PlanetExpressExpress
|
||||||
|
require 'date'
|
||||||
|
class ShipmentReader
|
||||||
|
def initialize(prices)
|
||||||
|
@prices = prices
|
||||||
|
end
|
||||||
|
|
||||||
|
# Expects to read a file
|
||||||
|
def read_shipments(io)
|
||||||
|
shipments = {
|
||||||
|
S: [],
|
||||||
|
M: [],
|
||||||
|
L: []
|
||||||
|
}
|
||||||
|
|
||||||
|
current_month = 0
|
||||||
|
current_year = 0
|
||||||
|
x = 0
|
||||||
|
io.lines.each do |line|
|
||||||
|
shipment = parse(line)
|
||||||
|
|
||||||
|
if shipment[:date].year != current_year || shipment[:date].month != current_month
|
||||||
|
close_month(shipments)
|
||||||
|
end
|
||||||
|
shipments[shipment[:size]].last.push(shipment)
|
||||||
|
|
||||||
|
current_year = shipment[:date].year
|
||||||
|
current_month = shipment[:date].month
|
||||||
|
x += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
shipments
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
attr_reader :prices
|
||||||
|
|
||||||
|
def close_month(shipments)
|
||||||
|
shipments.keys.each do |key|
|
||||||
|
shipments[key].push([])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse(line)
|
||||||
|
parts = line.split(" ")
|
||||||
|
|
||||||
|
shipment = {
|
||||||
|
date: Date.parse(parts[0]),
|
||||||
|
size: parts[1]&.to_sym,
|
||||||
|
provider: parts[2]&.to_sym,
|
||||||
|
price: 0,
|
||||||
|
discount: 0,
|
||||||
|
valid: true
|
||||||
|
}
|
||||||
|
|
||||||
|
shipment[:price] = prices[
|
||||||
|
[
|
||||||
|
shipment[:provider], shipment[:size]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
shipment
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user