Permalink
Browse files

Added remote cart operations

  • Loading branch information...
1 parent bf565c0 commit 3c6004020710e980387b59bb709f4ab5b4ace1e0 @rehanift rehanift committed Mar 17, 2011
Showing with 108 additions and 7 deletions.
  1. +39 −1 lib/asin.rb
  2. +69 −6 spec/asin_spec.rb
View
@@ -142,6 +142,31 @@ def search(params={:SearchIndex => :Books})
response = call(params.merge(:Operation => :ItemSearch))
(response['ItemSearchResponse']['Items']['Item'] || []).map {|item| Item.new(item)}
end
+
+ def cart_create(params)
+ response = call(create_cart_item_params(params).merge(:Operation => :CartCreate))
+ response["CartCreateResponse"]["Cart"]
+ end
+
+ def cart_add(params)
+ response = call(create_cart_item_params(params).merge(create_cart_params(params)).merge({:Operation => :CartAdd}))
+ response["CartAddResponse"]["Cart"]
+ end
+
+ def cart_update(params)
+ response = call(create_cart_item_params(params).merge(create_cart_params(params)).merge({:Operation => :CartModify}))
+ response["CartModifyResponse"]["Cart"]
+ end
+
+ def cart_clear(params)
+ response = call(create_cart_params(params).merge(:Operation => :CartClear))
+ response["CartClearResponse"]["Cart"]
+ end
+
+ def cart_remove(params)
+ response = call(create_cart_item_params(params).merge(create_cart_params(params)).merge({:Operation => :CartModify}))
+ response["CartModifyResponse"]["Cart"]
+ end
# Takes an array of item => quantity pairs and returns a hash of usable parameters
#
@@ -160,10 +185,23 @@ def search(params={:SearchIndex => :Books})
# "Item.3.Quantity" => 3
# }
- def create_cart_params(items)
+ def create_cart_params(params)
+ raise ":cart_id is a required parameter for remote cart operations" unless params[:cart_id]
+ raise ":hmac is a required parameter for remote cart operations" unless params[:hmac]
+ {
+ :CartId => params[:cart_id],
+ :HMAC => params[:hmac]
+ }
+ end
+
+ def create_cart_item_params(params)
+ raise "You must specify at least one item for remote cart operations" unless params[:items]
+
item_identifiers = [:asin, :offer_listing_id, :cart_item_id]
item_identifiers_override_map = {:asin => "ASIN"}
+ items = params[:items]
+
items.reject! do |item|
!item.keys.any? {|key| item_identifiers.include? key}
end
View
@@ -71,7 +71,7 @@
end
end
- context "cart operations" do
+ context "cart helpers" do
before do
@helper.configure :secret => @secret, :key => @key
@items = [
@@ -80,26 +80,26 @@
{:quantity=>4, :cart_item_id=>"bar"}]
end
- describe "#create_cart_params" do
+ describe "#create_cart_item_params" do
it "should filter items with a valid Item Identifier" do
items = @items + [{:foo => "bar"}]
- @helper.create_cart_params(items).size.should == @items.size * 2
+ @helper.create_cart_item_params(:items => items).size.should == @items.size * 2
end
it "should use the default quantity of 1" do
items = [{:asin => "foo"}]
- params = @helper.create_cart_params(items)
+ params = @helper.create_cart_item_params(:items => items)
params["Item.1.Quantity"].should == 1
end
it "should reject items without a valid item identifier" do
items = [{:foo => "bar"}]
- params = @helper.create_cart_params(items)
+ params = @helper.create_cart_item_params(:items => items)
params.should be_empty
end
it "should return a valid hash of parameters" do
- params = @helper.create_cart_params(@items)
+ params = @helper.create_cart_item_params(:items => @items)
params.keys.should be_any {|m| m =~ /Item\.\d\.ASIN/ }
params.keys.should be_any {|m| m =~ /Item\.\d\.OfferListingId/ }
params.keys.should be_any {|m| m =~ /Item\.\d\.CartItemId/ }
@@ -110,5 +110,68 @@
end
end
+
+ context "cart operations" do
+ describe "cart_create" do
+ it "should create a new remote cart" do
+ response = @helper.cart_create :items => [{:asin => '0534950973', :quantity => 1}]
+ response.should have_key "CartId"
+ end
+ end
+
+ describe "cart modification" do
+ before do
+ @asin = '0534950973'
+ response = @helper.cart_create :items => [{:asin => @asin, :quantity => 1}]
+ @cart_id = response["CartId"]
+ @cart_item_id = response["CartItems"]["CartItem"]["CartItemId"]
+ # @hmac = response["URLEncodedHMAC"]
+ @hmac = response["HMAC"]
+ end
+
+ it "should fail with a missing HMAC" do
+ lambda { @helper.create_cart_params :cart_id => @cart_id }.should raise_error(RuntimeError)
+ end
+
+ it "should fail with a missing CartId" do
+ lambda { @helper.create_cart_params :hmac => @hmac }.should raise_error(RuntimeError)
+ end
+
+ describe "cart_add" do
+ it "should add an item to the cart" do
+ response = @helper.cart_add(:cart_id => @cart_id, :hmac => @hmac,
+ :items => [{:asin => '0321486811'}])
+ response["CartItems"]["CartItem"].should be_any {|m| m["ASIN"] == "0321486811"}
+ end
+ end
+
+ describe "cart_clear" do
+ it "should remove all the items from a cart" do
+ response = @helper.cart_clear(:cart_id => @cart_id, :hmac => @hmac)
+ response.should_not have_key "CartItems"
+ end
+ end
+
+ describe "cart_update" do
+ it "should update the quantity of an item in the cart" do
+ response = @helper.cart_update(:cart_id => @cart_id, :hmac => @hmac,
+ :items => [{:cart_item_id => @cart_item_id, :quantity => 2}])
+ response["CartItems"]["CartItem"]["Quantity"].to_i.should == 2
+ end
+ end
+
+ describe "cart_remove" do
+ it "should remove an item from the cart" do
+ @helper.cart_add(:cart_id => @cart_id, :hmac => @hmac,
+ :items => [{:asin => '0321486811'},{:asin => '0321751043'}])
+ response = @helper.cart_remove(:cart_id => @cart_id, :hmac => @hmac,
+ :items => [{:cart_item_id => @cart_item_id, :quantity => 0}])
+ response["CartItems"]["CartItem"].should_not be_any {|m| m["ASIN"] == @asin}
+ end
+ end
+
+ end
+
+ end
end

0 comments on commit 3c60040

Please sign in to comment.