Skip to content
Browse files

fix item methods and add tests

  • Loading branch information...
1 parent f3f9131 commit cbb2d0cf35995b6e258c87dc8ac250d2f400c6fe @phoet phoet committed
Showing with 90 additions and 61 deletions.
  1. +9 −31 lib/asin/cart.rb
  2. +81 −30 spec/cart_spec.rb
View
40 lib/asin/cart.rb
@@ -33,7 +33,13 @@ def price
end
def items
- @raw.CartItems.reject{|item| item != 'CartItem'}.map(&:last)
+ return [] unless @raw.CartItems
+ @raw.CartItems.CartItem.is_a?(Array) ? @raw.CartItems.CartItem : [@raw.CartItems.CartItem]
+ end
+
+ def saved_items
+ return [] unless @raw.SavedForLaterItems
+ @raw.SavedForLaterItems.SavedForLaterItem.is_a?(Array) ? @raw.SavedForLaterItems.SavedForLaterItem : [@raw.SavedForLaterItems.SavedForLaterItem]
end
def valid?
@@ -43,34 +49,6 @@ def valid?
def empty?
@raw.CartItems.nil?
end
- end
-end
-
-# {"Request"=>
-# {"IsValid"=>"",
-# "CartCreateRequest"=>
-# {"Items"=>{"Item"=>{"ASIN"=>"1430218150", "Quantity"=>"1"}}}},
-# "CartId"=>"186-7155396-8661342",
-# "HMAC"=>"kajZJ8BoZStXyHg4LfNEzyjvQJw=",
-# "URLEncodedHMAC"=>"kajZJ8BoZStXyHg4LfNEzyjvQJw%3D",
-# "PurchaseURL"=>
-# "https://www.amazon.com/gp/cart/aws-merge.html?cart-id=186-7155396-8661342%26associate-id=ws%26hmac=kajZJ8BoZStXyHg4LfNEzyjvQJw=%26SubscriptionId=AKIAJFA5X7RTOKFNPVZQ%26MergeCart=False",
-# "SubTotal"=>
-# {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
-# "CartItems"=>
-# {"SubTotal"=>
-# {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
-# "CartItem"=>
-# {"CartItemId"=>"U3G241HVLLB8N6",
-# "ASIN"=>"1430218150",
-# "MerchantId"=>"ATVPDKIKX0DER",
-# "SellerId"=>"A2R2RITDJNW1Q6",
-# "SellerNickname"=>"Amazon.com",
-# "Quantity"=>"1",
-# "Title"=>"Learn Objective-C on the Mac (Learn Series)",
-# "ProductGroup"=>"Book",
-# "Price"=>
-# {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
-# "ItemTotal"=>
-# {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"}}}}
+ end
+end
View
111 spec/cart_spec.rb
@@ -50,10 +50,13 @@ module ASIN
cart = @helper.add_items(@cart, {:asin => OTHER_ASIN, :quantity => 2})
cart.valid?.should be(true)
cart.empty?.should be(false)
+ cart.items.should have(2).things
end
it "should update a cart" do
- cart = @helper.update_items(@cart, {:cart_item_id => @cart.items.first.CartItemId, :action => 'SaveForLater'}, {:cart_item_id => @cart.items.first.CartItemId, :quantity => 7})
+ item_id = @cart.items.first.CartItemId
+ cart = @helper.update_items(@cart, {:cart_item_id => item_id, :action => 'SaveForLater'}, {:cart_item_id => item_id, :quantity => 7})
+ cart.saved_items.should have(1).things
cart.valid?.should be(true)
end
@@ -63,42 +66,90 @@ module ASIN
before do
@helper.configure :secret => @secret, :key => @key
- @ok = {"Request"=>
- {"IsValid"=>"True",
- "CartCreateRequest"=>
- {"Items"=>{"Item"=>{"ASIN"=>"1430218150", "Quantity"=>"1"}}}},
- "CartId"=>"186-7155396-8661342",
- "HMAC"=>"kajZJ8BoZStXyHg4LfNEzyjvQJw=",
- "URLEncodedHMAC"=>"kajZJ8BoZStXyHg4LfNEzyjvQJw%3D",
+ @two_items = {"Request"=>
+ {"IsValid"=>"True",
+ "CartAddRequest"=>
+ {"CartId"=>"186-8702292-9782208",
+ "HMAC"=>"Ck5MXUE+OQiC/Jh8u6NhBf5FbV8=",
+ "Items"=>{"Item"=>{"ASIN"=>"1430216263", "Quantity"=>"2"}}}},
+ "CartId"=>"186-8702292-9782208",
+ "HMAC"=>"Ck5MXUE+OQiC/Jh8u6NhBf5FbV8=",
+ "URLEncodedHMAC"=>"Ck5MXUE%2BOQiC%2FJh8u6NhBf5FbV8%3D",
"PurchaseURL"=>
- "https://www.amazon.com/gp/cart/aws-merge.html?cart-id=186-7155396-8661342%26associate-id=ws%26hmac=kajZJ8BoZStXyHg4LfNEzyjvQJw=%26SubscriptionId=AKIAJFA5X7RTOKFNPVZQ%26MergeCart=False",
+ "https://www.amazon.com/gp/cart/aws-merge.html?cart-id=186-8702292-9782208%26associate-id=ws%26hmac=Ck5MXUE%2BOQiC/Jh8u6NhBf5FbV8=%26SubscriptionId=AKIAJFA5X7RTOKFNPVZQ%26MergeCart=False",
"SubTotal"=>
- {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
+ {"Amount"=>"6595", "CurrencyCode"=>"USD", "FormattedPrice"=>"$65.95"},
"CartItems"=>
- {"SubTotal"=>
- {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
- "CartItem"=>
- {"CartItemId"=>"U3G241HVLLB8N6",
- "ASIN"=>"1430218150",
- "MerchantId"=>"ATVPDKIKX0DER",
- "SellerId"=>"A2R2RITDJNW1Q6",
- "SellerNickname"=>"Amazon.com",
- "Quantity"=>"1",
- "Title"=>"Learn Objective-C on the Mac (Learn Series)",
- "ProductGroup"=>"Book",
- "Price"=>
- {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
- "ItemTotal"=>
- {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"}}}}
+ {"SubTotal"=>
+ {"Amount"=>"6595", "CurrencyCode"=>"USD", "FormattedPrice"=>"$65.95"},
+ "CartItem"=>
+ [{"CartItemId"=>"U3CFEHHIPJNW3L",
+ "ASIN"=>"1430216263",
+ "MerchantId"=>"ATVPDKIKX0DER",
+ "SellerId"=>"A2R2RITDJNW1Q6",
+ "SellerNickname"=>"Amazon.com",
+ "Quantity"=>"2",
+ "Title"=>"Beginning iPhone Development: Exploring the iPhone SDK",
+ "ProductGroup"=>"Book",
+ "Price"=>
+ {"Amount"=>"1978", "CurrencyCode"=>"USD", "FormattedPrice"=>"$19.78"},
+ "ItemTotal"=>
+ {"Amount"=>"3956", "CurrencyCode"=>"USD", "FormattedPrice"=>"$39.56"}},
+ {"CartItemId"=>"U3G241HVLLB8N6",
+ "ASIN"=>"1430218150",
+ "MerchantId"=>"ATVPDKIKX0DER",
+ "SellerId"=>"A2R2RITDJNW1Q6",
+ "SellerNickname"=>"Amazon.com",
+ "Quantity"=>"1",
+ "Title"=>"Learn Objective-C on the Mac (Learn Series)",
+ "ProductGroup"=>"Book",
+ "Price"=>
+ {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
+ "ItemTotal"=>
+ {"Amount"=>"2639",
+ "CurrencyCode"=>"USD",
+ "FormattedPrice"=>"$26.39"}}]}}
+ @one_item = {"Request"=>
+ {"IsValid"=>"True",
+ "CartCreateRequest"=>
+ {"Items"=>{"Item"=>{"ASIN"=>"1430218150", "Quantity"=>"1"}}}},
+ "CartId"=>"176-9182855-2326919",
+ "HMAC"=>"KgeVCA0YJTbuN/7Ibakrk/KnHWA=",
+ "URLEncodedHMAC"=>"KgeVCA0YJTbuN%2F7Ibakrk%2FKnHWA%3D",
+ "PurchaseURL"=>
+ "https://www.amazon.com/gp/cart/aws-merge.html?cart-id=176-9182855-2326919%26associate-id=ws%26hmac=KgeVCA0YJTbuN/7Ibakrk/KnHWA=%26SubscriptionId=AKIAJFA5X7RTOKFNPVZQ%26MergeCart=False",
+ "SubTotal"=>
+ {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
+ "CartItems"=>
+ {"SubTotal"=>
+ {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
+ "CartItem"=>
+ {"CartItemId"=>"U3G241HVLLB8N6",
+ "ASIN"=>"1430218150",
+ "MerchantId"=>"ATVPDKIKX0DER",
+ "SellerId"=>"A2R2RITDJNW1Q6",
+ "SellerNickname"=>"Amazon.com",
+ "Quantity"=>"1",
+ "Title"=>"Learn Objective-C on the Mac (Learn Series)",
+ "ProductGroup"=>"Book",
+ "Price"=>
+ {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"},
+ "ItemTotal"=>
+ {"Amount"=>"2639", "CurrencyCode"=>"USD", "FormattedPrice"=>"$26.39"}}}}
end
it "should handle response data" do
- cart = Cart.new(@ok)
+ cart = Cart.new(@two_items)
cart.valid?.should be(true)
- cart.cart_id.should eql('186-7155396-8661342')
- cart.hmac.should eql('kajZJ8BoZStXyHg4LfNEzyjvQJw=')
- cart.url.should eql('https://www.amazon.com/gp/cart/aws-merge.html?cart-id=186-7155396-8661342%26associate-id=ws%26hmac=kajZJ8BoZStXyHg4LfNEzyjvQJw=%26SubscriptionId=AKIAJFA5X7RTOKFNPVZQ%26MergeCart=False')
- cart.price.should eql('$26.39')
+ cart.cart_id.should eql('186-8702292-9782208')
+ cart.hmac.should eql('Ck5MXUE+OQiC/Jh8u6NhBf5FbV8=')
+ cart.url.should eql('https://www.amazon.com/gp/cart/aws-merge.html?cart-id=186-8702292-9782208%26associate-id=ws%26hmac=Ck5MXUE%2BOQiC/Jh8u6NhBf5FbV8=%26SubscriptionId=AKIAJFA5X7RTOKFNPVZQ%26MergeCart=False')
+ cart.price.should eql('$65.95')
+ cart.items.first.CartItemId eql('U3G241HVLLB8N6')
+ end
+
+ it "should handle one item" do
+ cart = Cart.new(@two_items)
cart.items.first.CartItemId eql('U3G241HVLLB8N6')
end

0 comments on commit cbb2d0c

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