Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add trunk for cart operations

  • Loading branch information...
commit 585569f372c2907e06f16e0151d07efc251dbe92 1 parent 3c60040
Peter Schröder phoet authored

Showing 3 changed files with 28 additions and 178 deletions. Show diff stats Hide diff stats

  1. +1 1  Gemfile.lock
  2. +14 77 lib/asin.rb
  3. +13 100 spec/asin_spec.rb
2  Gemfile.lock
@@ -19,7 +19,7 @@ GEM
19 19 httpclient (2.1.6.1)
20 20 httpi (0.7.9)
21 21 rack
22   - rack (1.2.1)
  22 + rack (1.2.2)
23 23 rspec (2.4.0)
24 24 rspec-core (~> 2.4.0)
25 25 rspec-expectations (~> 2.4.0)
91 lib/asin.rb
@@ -143,85 +143,22 @@ def search(params={:SearchIndex => :Books})
143 143 (response['ItemSearchResponse']['Items']['Item'] || []).map {|item| Item.new(item)}
144 144 end
145 145
146   - def cart_create(params)
147   - response = call(create_cart_item_params(params).merge(:Operation => :CartCreate))
148   - response["CartCreateResponse"]["Cart"]
149   - end
150   -
151   - def cart_add(params)
152   - response = call(create_cart_item_params(params).merge(create_cart_params(params)).merge({:Operation => :CartAdd}))
153   - response["CartAddResponse"]["Cart"]
154   - end
155   -
156   - def cart_update(params)
157   - response = call(create_cart_item_params(params).merge(create_cart_params(params)).merge({:Operation => :CartModify}))
158   - response["CartModifyResponse"]["Cart"]
159   - end
160   -
161   - def cart_clear(params)
162   - response = call(create_cart_params(params).merge(:Operation => :CartClear))
163   - response["CartClearResponse"]["Cart"]
164   - end
165   -
166   - def cart_remove(params)
167   - response = call(create_cart_item_params(params).merge(create_cart_params(params)).merge({:Operation => :CartModify}))
168   - response["CartModifyResponse"]["Cart"]
169   - end
170   -
171   - # Takes an array of item => quantity pairs and returns a hash of usable parameters
172   - #
173   - # [{:asin => "foo", :quantity => 2},
174   - # {:offer_listing_id => "bar", :quantity => 3}
175   - # {:cart_item_id => "baz", :quantity => 4}]
176   - #
177   - # #=>
178   - #
179   - # {
180   - # "Item.1.ASIN" => "foo",
181   - # "Item.1.Quantity" => 2,
182   - # "Item.2.OfferListingId" => "bar",
183   - # "Item.2.Quantity" => 3,
184   - # "Item.3.CartItemId" => "baz",
185   - # "Item.3.Quantity" => 3
186   - # }
187   -
188   - def create_cart_params(params)
189   - raise ":cart_id is a required parameter for remote cart operations" unless params[:cart_id]
190   - raise ":hmac is a required parameter for remote cart operations" unless params[:hmac]
191   - {
192   - :CartId => params[:cart_id],
193   - :HMAC => params[:hmac]
  146 + # TODO (ps) add decent documentation
  147 + def cart(operation, items, params={})
  148 + operations = {
  149 + :add => :CartAdd,
  150 + :clear => :CartClear,
  151 + :create => :CartCreate,
  152 + :get => :CartGet,
  153 + :modify => :CartModify
194 154 }
195   - end
196   -
197   - def create_cart_item_params(params)
198   - raise "You must specify at least one item for remote cart operations" unless params[:items]
199   -
200   - item_identifiers = [:asin, :offer_listing_id, :cart_item_id]
201   - item_identifiers_override_map = {:asin => "ASIN"}
202   -
203   - items = params[:items]
204   -
205   - items.reject! do |item|
206   - !item.keys.any? {|key| item_identifiers.include? key}
207   - end
208   -
209   - items.map! do |item|
210   - item.each do |key,value|
211   - item[key.to_s.downcase.to_sym] = item.delete(key)
212   - end
  155 + items_hash = {}
  156 + items.each_with_index do |item, i|
  157 + items_hash["Item.#{i}.ASIN"] = item[:asin]
  158 + items_hash["Item.#{i}.Quantity"] = item[:quantity]
213 159 end
214   -
215   - params = {}
216   - items.each_with_index do |item_hash, index|
217   - item_identifiers.each do |identifier|
218   - param_key = item_identifiers_override_map[identifier] ||
219   - identifier.to_s.split('_').map!{|m| m.capitalize}.join # poor-mans String#camelize
220   - params["Item.#{index+1}.#{param_key}"] = item_hash[identifier] if item_hash.has_key?(identifier)
221   - end
222   - params["Item.#{index+1}.Quantity"] = item_hash[:quantity] || 1
223   - end
224   - params
  160 + response = call(params.merge(items_hash.merge(:Operation => operations[operation])))
  161 + response["CartCreateResponse"]["Cart"]
225 162 end
226 163
227 164 private
113 spec/asin_spec.rb
... ... @@ -1,4 +1,9 @@
  1 +require 'spec_helper'
  2 +
1 3 describe ASIN do
  4 +
  5 + ANY_ASIN = '1430218150'
  6 +
2 7 before do
3 8 @helper = ASIN.client
4 9 @helper.configure :logger => nil
@@ -27,10 +32,10 @@
27 32 end
28 33
29 34 it "should work with a configuration block" do
30   - config = ASIN::Configuration.configure do |config|
31   - config.key = 'bla'
  35 + conf = ASIN::Configuration.configure do |config|
  36 + conf.key = 'bla'
32 37 end
33   - config.key.should eql('bla')
  38 + conf.key.should eql('bla')
34 39 end
35 40 end
36 41
@@ -40,7 +45,7 @@
40 45 end
41 46
42 47 it "should lookup a book" do
43   - item = @helper.lookup('1430218150')
  48 + item = @helper.lookup(ANY_ASIN)
44 49 item.title.should =~ /Learn Objective/
45 50 end
46 51
@@ -71,107 +76,15 @@
71 76 end
72 77 end
73 78
74   - context "cart helpers" do
  79 + context "cart" do
75 80 before do
76 81 @helper.configure :secret => @secret, :key => @key
77   - @items = [
78   - {:quantity=>3, :offer_listing_id=>"321"},
79   - {:quantity=>2, :asin=>"foo"},
80   - {:quantity=>4, :cart_item_id=>"bar"}]
81 82 end
82 83
83   - describe "#create_cart_item_params" do
84   - it "should filter items with a valid Item Identifier" do
85   - items = @items + [{:foo => "bar"}]
86   - @helper.create_cart_item_params(:items => items).size.should == @items.size * 2
87   - end
88   -
89   - it "should use the default quantity of 1" do
90   - items = [{:asin => "foo"}]
91   - params = @helper.create_cart_item_params(:items => items)
92   - params["Item.1.Quantity"].should == 1
93   - end
94   -
95   - it "should reject items without a valid item identifier" do
96   - items = [{:foo => "bar"}]
97   - params = @helper.create_cart_item_params(:items => items)
98   - params.should be_empty
99   - end
100   -
101   - it "should return a valid hash of parameters" do
102   - params = @helper.create_cart_item_params(:items => @items)
103   - params.keys.should be_any {|m| m =~ /Item\.\d\.ASIN/ }
104   - params.keys.should be_any {|m| m =~ /Item\.\d\.OfferListingId/ }
105   - params.keys.should be_any {|m| m =~ /Item\.\d\.CartItemId/ }
106   - params.values.should include "321"
107   - params.values.should include "foo"
108   - params.values.should include "bar"
109   - end
  84 + it "should create a cart" do
  85 + cart = @helper.cart :create, [{:asin => ANY_ASIN, :quantity => 1}]
  86 + cart.inspect.should be(nil)
110 87 end
111   -
112   - end
113   -
114   - context "cart operations" do
115   - describe "cart_create" do
116   - it "should create a new remote cart" do
117   - response = @helper.cart_create :items => [{:asin => '0534950973', :quantity => 1}]
118   - response.should have_key "CartId"
119   - end
120   - end
121   -
122   - describe "cart modification" do
123   - before do
124   - @asin = '0534950973'
125   - response = @helper.cart_create :items => [{:asin => @asin, :quantity => 1}]
126   - @cart_id = response["CartId"]
127   - @cart_item_id = response["CartItems"]["CartItem"]["CartItemId"]
128   - # @hmac = response["URLEncodedHMAC"]
129   - @hmac = response["HMAC"]
130   - end
131   -
132   - it "should fail with a missing HMAC" do
133   - lambda { @helper.create_cart_params :cart_id => @cart_id }.should raise_error(RuntimeError)
134   - end
135   -
136   - it "should fail with a missing CartId" do
137   - lambda { @helper.create_cart_params :hmac => @hmac }.should raise_error(RuntimeError)
138   - end
139   -
140   - describe "cart_add" do
141   - it "should add an item to the cart" do
142   - response = @helper.cart_add(:cart_id => @cart_id, :hmac => @hmac,
143   - :items => [{:asin => '0321486811'}])
144   - response["CartItems"]["CartItem"].should be_any {|m| m["ASIN"] == "0321486811"}
145   - end
146   - end
147   -
148   - describe "cart_clear" do
149   - it "should remove all the items from a cart" do
150   - response = @helper.cart_clear(:cart_id => @cart_id, :hmac => @hmac)
151   - response.should_not have_key "CartItems"
152   - end
153   - end
154   -
155   - describe "cart_update" do
156   - it "should update the quantity of an item in the cart" do
157   - response = @helper.cart_update(:cart_id => @cart_id, :hmac => @hmac,
158   - :items => [{:cart_item_id => @cart_item_id, :quantity => 2}])
159   - response["CartItems"]["CartItem"]["Quantity"].to_i.should == 2
160   - end
161   - end
162   -
163   - describe "cart_remove" do
164   - it "should remove an item from the cart" do
165   - @helper.cart_add(:cart_id => @cart_id, :hmac => @hmac,
166   - :items => [{:asin => '0321486811'},{:asin => '0321751043'}])
167   - response = @helper.cart_remove(:cart_id => @cart_id, :hmac => @hmac,
168   - :items => [{:cart_item_id => @cart_item_id, :quantity => 0}])
169   - response["CartItems"]["CartItem"].should_not be_any {|m| m["ASIN"] == @asin}
170   - end
171   - end
172   -
173   - end
174   -
175 88 end
176 89 end
177 90

0 comments on commit 585569f

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