Implement shipment formatter
This commit is contained in:
parent
6c4c9c75fe
commit
f43e4b700d
@ -13,7 +13,7 @@ require 'planet_express/invalid_shipment'
|
||||
require 'planet_express/shipment'
|
||||
require 'planet_express/shipment_repository'
|
||||
require 'planet_express/shipment_reader'
|
||||
# require 'planet_express/shipment_formatter'
|
||||
require 'planet_express/shipment_formatter'
|
||||
|
||||
require 'planet_express/shipping_option'
|
||||
require 'planet_express/shipping_option_repository'
|
||||
|
@ -1,5 +1,7 @@
|
||||
module PlanetExpress
|
||||
class InvalidShipment
|
||||
attr_reader :raw_string
|
||||
|
||||
def initialize(raw_string)
|
||||
@raw_string = raw_string
|
||||
end
|
||||
|
29
lib/planet_express/shipment_formatter.rb
Normal file
29
lib/planet_express/shipment_formatter.rb
Normal file
@ -0,0 +1,29 @@
|
||||
module PlanetExpress
|
||||
class ShipmentFormatter
|
||||
def format(shipment)
|
||||
return format_invalid_shipment(shipment) if shipment.instance_of?(InvalidShipment)
|
||||
|
||||
format_valid_shipment(shipment)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def format_invalid_shipment(shipment)
|
||||
"#{shipment.raw_string} IGNORED"
|
||||
end
|
||||
|
||||
def format_valid_shipment(shipment)
|
||||
date = shipment.date.iso8601
|
||||
package_size = shipment.shipping_option.package_size
|
||||
provider = shipment.shipping_option.provider
|
||||
price = shipment.price_with_discounts
|
||||
discount = shipment.total_discount_amount
|
||||
|
||||
"#{date} #{package_size} #{provider} #{format_money(price)} #{discount.zero? ? '-' : format_money(discount)}"
|
||||
end
|
||||
|
||||
def format_money(amount)
|
||||
amount.to_s.rjust(3,'0').insert(-3, '.')
|
||||
end
|
||||
end
|
||||
end
|
62
spec/shipment_formatter_spec.rb
Normal file
62
spec/shipment_formatter_spec.rb
Normal file
@ -0,0 +1,62 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe PlanetExpress::ShipmentFormatter do
|
||||
subject do
|
||||
described_class.new
|
||||
end
|
||||
|
||||
describe '#format' do
|
||||
context 'invalid shipment' do
|
||||
let(:shipment) do
|
||||
PlanetExpress::InvalidShipment.new("Invalid Shipment")
|
||||
end
|
||||
|
||||
it 'returns a well formatted string' do
|
||||
expect(subject.format(shipment)).to eq('Invalid Shipment IGNORED')
|
||||
end
|
||||
end
|
||||
|
||||
context 'valid shipment' do
|
||||
let(:date) { Date.today }
|
||||
let(:shipping_option) do
|
||||
build(:shipping_option,
|
||||
provider: 'T',
|
||||
package_size: 'S',
|
||||
price_in_cents: 120)
|
||||
end
|
||||
|
||||
let(:shipment) do
|
||||
PlanetExpress::Shipment.new(date, shipping_option)
|
||||
end
|
||||
|
||||
it 'returns a well formatted string' do
|
||||
result = subject.format(shipment)
|
||||
parts = result.split(' ')
|
||||
|
||||
expect(parts[0]).to eq(date.iso8601)
|
||||
expect(parts[1]).to eq(shipping_option.package_size)
|
||||
expect(parts[2]).to eq(shipping_option.provider)
|
||||
expect(parts[3]).to eq('1.20')
|
||||
expect(parts[4]).to eq('-')
|
||||
end
|
||||
|
||||
context 'with discounts' do
|
||||
before(:each) do
|
||||
shipment.add_discount(PlanetExpress::Discount.new(50, nil))
|
||||
shipment.add_discount(PlanetExpress::Discount.new(25, nil))
|
||||
end
|
||||
|
||||
it 'includes the discounts' do
|
||||
result = subject.format(shipment)
|
||||
parts = result.split(' ')
|
||||
|
||||
expect(parts[0]).to eq(date.iso8601)
|
||||
expect(parts[1]).to eq(shipping_option.package_size)
|
||||
expect(parts[2]).to eq(shipping_option.provider)
|
||||
expect(parts[3]).to eq('0.45')
|
||||
expect(parts[4]).to eq('0.75')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user