Skip to content
Browse files

Update ShipmentPacker to cast item values

  • Loading branch information...
1 parent 1069067 commit 5fa2892259eb85c1a42514f3bfb2410d585fcd5f Denis Odorcic committed
Showing with 26 additions and 3 deletions.
  1. +2 −3 lib/active_shipping/shipping/shipment_packer.rb
  2. +24 −0 test/unit/shipment_packer_test.rb
View
5 lib/active_shipping/shipping/shipment_packer.rb
@@ -10,8 +10,7 @@ class OverweightItem < StandardError
# maximum_weight - maximum weight in grams
# currency - ISO currency code
def self.pack(items, dimensions, maximum_weight, currency)
- items.each(&:symbolize_keys!)
- items = items.collect{ |item| [item] * item[:quantity] }.flatten
+ items = items.map(&:symbolize_keys).map { |item| [item] * item[:quantity].to_i }.flatten
packages = []
state = :package_empty
@@ -22,7 +21,7 @@ def self.pack(items, dimensions, maximum_weight, currency)
state = :filling_package
when :filling_package
item = items.shift
- item_weight, item_price = item[:grams], Package.cents_from(item[:price])
+ item_weight, item_price = item[:grams].to_i, Package.cents_from(item[:price])
if item_weight > maximum_weight
raise OverweightItem, "The item with weight of #{item_weight}g is heavier than the allowable package weight of #{maximum_weight}g"
View
24 test/unit/shipment_packer_test.rb
@@ -97,4 +97,28 @@ def test_divide_items_and_prices_accordingly_when_splitting_into_two_packages
assert_equal 'USD', packages[0].currency
assert_equal 'USD', packages[1].currency
end
+
+ def test_symbolize_item_keys
+ string_key_items = [ {'grams' => 1, 'quantity' => 1, 'price' => 1.0} ]
+ indifferent_access_items = [ {'grams' => 1, 'quantity' => 1, 'price' => 1.0}.with_indifferent_access ]
+
+ [string_key_items, indifferent_access_items].each do |items|
+ packages = ShipmentPacker.pack(items, @dimensions, 1, 'USD')
+ assert_equal 1, packages.size
+
+ package = packages.first
+ assert_equal 1, package.weight
+ assert_equal 100, package.value
+ end
+ end
+
+ def test_cast_quantity_and_grams_to_int
+ items = [ {:grams => '1', :quantity => '1', :price => '1.0'} ]
+
+ packages = ShipmentPacker.pack(items, @dimensions, 1, 'USD')
+
+ package = packages.first
+ assert_equal 1, package.weight
+ assert_equal 100, package.value
+ end
end

0 comments on commit 5fa2892

Please sign in to comment.
Something went wrong with that request. Please try again.