Module: ASIN::Client

Includes:
Adapter
Defined in:
lib/asin/client.rb,
lib/asin/adapter.rb

Constant Summary collapse

DIGEST =
OpenSSL::Digest.new('sha256')
PATH =
'/onca/xml'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject

Convenience method to create an ASIN client.

An instance is not necessary though, you can simply include the ASIN module otherwise.



18
19
20
21
22
# File 'lib/asin/client.rb', line 18

def self.instance
  ins = Object.new
  ins.extend ASIN::Client
  ins
end

Instance Method Details

#add_items(cart, *items) ⇒ Object

Performs an CartAdd REST call against the Amazon API.

Expects a cart created with create_cart and one ore more Item-Hashes and returns an updated cart:

cart = add_items(cart, {:asin => '1430216263', :quantity => 2})

Options:

Additional parameters for the API call like this:

add_items(cart, {:asin => '1430218150', :quantity => 1}, {:asin => '1430216263', :quantity => 1, :action => :SaveForLater})

Have a look at the different cart item operation values on the Amazon-Documentation



179
180
181
# File 'lib/asin/client.rb', line 179

def add_items(cart, *items)
  cart(:CartAdd, create_item_params(items).merge({:CartId => cart.cart_id, :HMAC => cart.hmac}))
end

#browse_node(node_id, params = {:ResponseGroup => :BrowseNodeInfo}) ⇒ Object

Performs an BrowseNodeLookup REST call against the Amazon API.

Expects a node-id and returns a node:

node = browse_node '17'

Options:

Additional parameters for the API call like this:

browse_node('17', :ResponseGroup => :TopSellers)

Have a look at the different browse node values on the Amazon-Documentation



112
113
114
115
# File 'lib/asin/client.rb', line 112

def browse_node(node_id, params={:ResponseGroup => :BrowseNodeInfo})
  response = call(params.merge(:Operation => :BrowseNodeLookup, :BrowseNodeId => node_id))
  arrayfy(response['BrowseNodeLookupResponse']['BrowseNodes']['BrowseNode']).map {|item| handle_type(item, :node)}
end

#clear_cart(cart) ⇒ Object

Performs an CartClear REST call against the Amazon API.

Expects a cart created with create_cart and returns an empty cart:

cart = clear_cart(cart)


207
208
209
# File 'lib/asin/client.rb', line 207

def clear_cart(cart)
  cart(:CartClear, {:CartId => cart.cart_id, :HMAC => cart.hmac})
end

#configure(options = {}) ⇒ Object

Configures the basic request parameters for ASIN.

Expects at least secret, key and associate_tag for the API call:

configure :secret => 'your-secret', :key => 'your-key', :associate_tag => 'your-associate_tag'

See ASIN::Configuration for more infos.



32
33
34
# File 'lib/asin/client.rb', line 32

def configure(options={})
  Configuration.configure(options)
end

#create_cart(*items) ⇒ Object

Performs an CartCreate REST call against the Amazon API.

Expects one ore more item-hashes and returns a cart:

cart = create_cart({:asin => '1430218150', :quantity => 1})

Options:

Additional parameters for the API call like this:

create_cart({:asin => '1430218150', :quantity => 1}, {:asin => '1430216263', :quantity => 1, :action => :SaveForLater})

Have a look at the different cart item operation values on the Amazon-Documentation



151
152
153
# File 'lib/asin/client.rb', line 151

def create_cart(*items)
  cart(:CartCreate, create_item_params(items))
end

#get_cart(cart_id, hmac) ⇒ Object

Performs an CartGet REST call against the Amazon API.

Expects the CartId and the HMAC to identify the returning cart:

cart = get_cart('176-9182855-2326919', 'KgeVCA0YJTbuN/7Ibakrk/KnHWA=')


161
162
163
# File 'lib/asin/client.rb', line 161

def get_cart(cart_id, hmac)
  cart(:CartGet, {:CartId => cart_id, :HMAC => hmac})
end

#lookup(*asins) ⇒ Object

Performs an ItemLookup REST call against the Amazon API.

Expects an arbitrary number of ASIN (Amazon Standard Identification Number) and returns an array of item:

item = lookup '1430218150'
items = lookup ['1430218150', '0439023521']

Options:

Additional parameters for the API call like this:

lookup(asin, :ResponseGroup => :Medium)

Or with multiple parameters:

lookup(asin, :ResponseGroup => [:Small, :AlternateVersions])


53
54
55
56
57
# File 'lib/asin/client.rb', line 53

def lookup(*asins)
  params = asins.last.is_a?(Hash) ? asins.pop : {:ResponseGroup => :Medium}
  response = call(params.merge(:Operation => :ItemLookup, :ItemId => asins.join(',')))
  arrayfy(response['ItemLookupResponse']['Items']['Item']).map {|item| handle_type(item, :item)}
end

#search(params = {:SearchIndex => :Books, :ResponseGroup => :Medium}) ⇒ Object

Performs an ItemSearch REST call against the Amazon API.

Expects a Hash of search params and returns a list of items:

items = search :SearchIndex => :Music

Options:

Additional parameters for the API call like this:

search(:Keywords => 'nirvana', :SearchIndex => :Music)

Have a look at the different search index values on the Amazon-Documentation



93
94
95
96
# File 'lib/asin/client.rb', line 93

def search(params={:SearchIndex => :Books, :ResponseGroup => :Medium})
  response = call(params.merge(:Operation => :ItemSearch))
  arrayfy(response['ItemSearchResponse']['Items']['Item']).map {|item| handle_type(item, :item)}
end

#search_keywords(*keywords) ⇒ Object

Performs an ItemSearch REST call against the Amazon API.

Expects a search-string which can be an arbitrary array of strings (ASINs f.e.) and returns a list of items:

items = search_keywords 'Learn', 'Objective-C'

Options:

Additional parameters for the API call like this:

search_keywords('nirvana', 'never mind', :SearchIndex => :Music)

Have a look at the different search index values on the Amazon-Documentation



73
74
75
76
77
# File 'lib/asin/client.rb', line 73

def search_keywords(*keywords)
  params = keywords.last.is_a?(Hash) ? keywords.pop : {:SearchIndex => :Books, :ResponseGroup => :Medium}
  response = call(params.merge(:Operation => :ItemSearch, :Keywords => keywords.join(' ')))
  arrayfy(response['ItemSearchResponse']['Items']['Item']).map {|item| handle_type(item, :item)}
end

#similar(*asins) ⇒ Object

Performs an SimilarityLookup REST call against the Amazon API.

Expects one ore more asins and returns a list of nodes:

items = similar '1430218150'

Options:

Additional parameters for the API call like this:

similar('1430218150', :SimilarityType => :Intersection, :ResponseGroup => :Small)

Have a look at the optional config values on the Amazon-Documentation



131
132
133
134
135
# File 'lib/asin/client.rb', line 131

def similar(*asins)
  params = asins.last.is_a?(Hash) ? asins.pop : {:SimilarityType => :Random, :ResponseGroup => :Medium}
  response = call(params.merge(:Operation => :SimilarityLookup, :ItemId => asins.join(',')))
  arrayfy(response['SimilarityLookupResponse']['Items']['Item']).map {|item| handle_type(item, :item)}
end

#update_items(cart, *items) ⇒ Object

Performs an CartModify REST call against the Amazon API.

Expects a cart created with create_cart and one ore more Item-Hashes to modify and returns an updated cart:

cart = update_items(cart, {:cart_item_id => cart.items.first.CartItemId, :quantity => 7})

Options:

Additional parameters for the API call like this:

update_items(cart, {:cart_item_id => cart.items.first.CartItemId, :action => :SaveForLater}, {:cart_item_id => cart.items.first.CartItemId, :quantity => 7})

Have a look at the different cart item operation values on the Amazon-Documentation



197
198
199
# File 'lib/asin/client.rb', line 197

def update_items(cart, *items)
  cart(:CartModify, create_item_params(items).merge({:CartId => cart.cart_id, :HMAC => cart.hmac}))
end