Skip to content

Commit

Permalink
Added create_cart_params method to help with remote cart operations
Browse files Browse the repository at this point in the history
  • Loading branch information
rehanift committed Mar 16, 2011
1 parent 51b506d commit bf565c0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/asin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
require 'httpi'
require 'crack/xml'
require 'cgi'
Expand Down Expand Up @@ -142,6 +143,49 @@ def search(params={:SearchIndex => :Books})
(response['ItemSearchResponse']['Items']['Item'] || []).map {|item| Item.new(item)}
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(items)
item_identifiers = [:asin, :offer_listing_id, :cart_item_id]
item_identifiers_override_map = {:asin => "ASIN"}

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
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
end

private

def credentials_valid?
Expand Down
40 changes: 40 additions & 0 deletions spec/asin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,45 @@
items.first.title.should =~ /Nevermind/
end
end

context "cart operations" 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_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
end

it "should use the default quantity of 1" do
items = [{:asin => "foo"}]
params = @helper.create_cart_params(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.should be_empty
end

it "should return a valid hash of parameters" do
params = @helper.create_cart_params(@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
end

end
end

0 comments on commit bf565c0

Please sign in to comment.