Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add trunk for cart operations

  • Loading branch information...
commit 585569f372c2907e06f16e0151d07efc251dbe92 1 parent 3c60040
Peter Schröder phoet authored
Showing with 28 additions and 178 deletions.
  1. +1 −1  Gemfile.lock
  2. +14 −77 lib/asin.rb
  3. +13 −100 spec/asin_spec.rb
2  Gemfile.lock
View
@@ -19,7 +19,7 @@ GEM
httpclient (2.1.6.1)
httpi (0.7.9)
rack
- rack (1.2.1)
+ rack (1.2.2)
rspec (2.4.0)
rspec-core (~> 2.4.0)
rspec-expectations (~> 2.4.0)
91 lib/asin.rb
View
@@ -143,85 +143,22 @@ def search(params={:SearchIndex => :Books})
(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
- #
- # [{:asin => "foo", :quantity => 2},
- # {:offer_listing_id => "bar", :quantity => 3}
- # {:cart_item_id => "baz", :quantity => 4}]
- #
- # #=>
- #
- # {
- # "Item.1.ASIN" => "foo",
- # "Item.1.Quantity" => 2,
- # "Item.2.OfferListingId" => "bar",
- # "Item.2.Quantity" => 3,
- # "Item.3.CartItemId" => "baz",
- # "Item.3.Quantity" => 3
- # }
-
- 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]
+ # TODO (ps) add decent documentation
+ def cart(operation, items, params={})
+ operations = {
+ :add => :CartAdd,
+ :clear => :CartClear,
+ :create => :CartCreate,
+ :get => :CartGet,
+ :modify => :CartModify
}
- 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
-
- items.map! do |item|
- item.each do |key,value|
- item[key.to_s.downcase.to_sym] = item.delete(key)
- end
+ items_hash = {}
+ items.each_with_index do |item, i|
+ items_hash["Item.#{i}.ASIN"] = item[:asin]
+ items_hash["Item.#{i}.Quantity"] = item[:quantity]
end
-
- params = {}
- items.each_with_index do |item_hash, index|
- item_identifiers.each do |identifier|
- param_key = item_identifiers_override_map[identifier] ||
- identifier.to_s.split('_').map!{|m| m.capitalize}.join # poor-mans String#camelize
- params["Item.#{index+1}.#{param_key}"] = item_hash[identifier] if item_hash.has_key?(identifier)
- end
- params["Item.#{index+1}.Quantity"] = item_hash[:quantity] || 1
- end
- params
+ response = call(params.merge(items_hash.merge(:Operation => operations[operation])))
+ response["CartCreateResponse"]["Cart"]
end
private
113 spec/asin_spec.rb
View
@@ -1,4 +1,9 @@
+require 'spec_helper'
+
describe ASIN do
+
+ ANY_ASIN = '1430218150'
+
before do
@helper = ASIN.client
@helper.configure :logger => nil
@@ -27,10 +32,10 @@
end
it "should work with a configuration block" do
- config = ASIN::Configuration.configure do |config|
- config.key = 'bla'
+ conf = ASIN::Configuration.configure do |config|
+ conf.key = 'bla'
end
- config.key.should eql('bla')
+ conf.key.should eql('bla')
end
end
@@ -40,7 +45,7 @@
end
it "should lookup a book" do
- item = @helper.lookup('1430218150')
+ item = @helper.lookup(ANY_ASIN)
item.title.should =~ /Learn Objective/
end
@@ -71,107 +76,15 @@
end
end
- context "cart helpers" do
+ context "cart" do
before do
@helper.configure :secret => @secret, :key => @key
- @items = [
- {:quantity=>3, :offer_listing_id=>"321"},
- {:quantity=>2, :asin=>"foo"},
- {:quantity=>4, :cart_item_id=>"bar"}]
end
- describe "#create_cart_item_params" do
- it "should filter items with a valid Item Identifier" do
- items = @items + [{:foo => "bar"}]
- @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_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_item_params(:items => items)
- params.should be_empty
- end
-
- it "should return a valid hash of parameters" do
- 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/ }
- params.values.should include "321"
- params.values.should include "foo"
- params.values.should include "bar"
- end
+ it "should create a cart" do
+ cart = @helper.cart :create, [{:asin => ANY_ASIN, :quantity => 1}]
+ cart.inspect.should be(nil)
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
Please sign in to comment.
Something went wrong with that request. Please try again.