Module: ASIN::Client
- Defined in:
- lib/asin/client.rb
Constant Summary
- DIGEST =
OpenSSL::Digest::Digest.new('sha256')
- PATH =
'/onca/xml'
Class Method Summary (collapse)
-
+ (Object) instance
Convenience method to create an ASIN client.
Instance Method Summary (collapse)
-
- (Object) add_items(cart, *items)
Performs an CartAdd REST call against the Amazon API.
-
- (Object) browse_node(node_id, params = {:ResponseGroup => :BrowseNodeInfo})
Performs an BrowseNodeLookup REST call against the Amazon API.
-
- (Object) clear_cart(cart)
Performs an CartClear REST call against the Amazon API.
-
- (Object) configure(options = {})
Configures the basic request parameters for ASIN.
-
- (Object) create_cart(*items)
Performs an CartCreate REST call against the Amazon API.
-
- (Object) get_cart(cart_id, hmac)
Performs an CartGet REST call against the Amazon API.
-
- (Object) lookup(*asins)
Performs an ItemLookup REST call against the Amazon API.
-
- (Object) search(params = {:SearchIndex => :Books, :ResponseGroup => :Medium})
Performs an ItemSearch REST call against the Amazon API.
-
- (Object) search_keywords(*keywords)
Performs an ItemSearch REST call against the Amazon API.
-
- (Object) similar(*asins)
Performs an SimilarityLookup REST call against the Amazon API.
-
- (Object) update_items(cart, *items)
Performs an CartModify REST call against the Amazon API.
Class Method Details
+ (Object) instance
Convenience method to create an ASIN client.
An instance is not necessary though, you can simply include the ASIN module otherwise.
113 114 115 116 117 |
# File 'lib/asin/client.rb', line 113 def self.instance ins = Object.new ins.extend ASIN::Client ins end |
Instance Method Details
- (Object) add_items(cart, *items)
Performs an CartAdd REST call against the Amazon API.
Expects a SimpleCart created with create_cart and one ore more Item-Hashes and returns an updated SimpleCart:
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
282 283 284 |
# File 'lib/asin/client.rb', line 282 def add_items(cart, *items) cart(:CartAdd, create_item_params(items).merge({:CartId => cart.cart_id, :HMAC => cart.hmac})) end |
- (Object) browse_node(node_id, params = {:ResponseGroup => :BrowseNodeInfo})
Performs an BrowseNodeLookup REST call against the Amazon API.
Expects a node-id and returns a SimpleNode:
node = browse_node '163357'
Options:
Additional parameters for the API call like this:
browse_node('163357', :ResponseGroup => :TopSellers)
Have a look at the different browse node values on the Amazon-Documentation
215 216 217 218 |
# File 'lib/asin/client.rb', line 215 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, Configuration.node_type)} end |
- (Object) clear_cart(cart)
Performs an CartClear REST call against the Amazon API.
Expects a SimpleCart created with create_cart and returns an empty SimpleCart:
cart = clear_cart(cart)
310 311 312 |
# File 'lib/asin/client.rb', line 310 def clear_cart(cart) cart(:CartClear, {:CartId => cart.cart_id, :HMAC => cart.hmac}) end |
- (Object) configure(options = {})
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.
127 128 129 |
# File 'lib/asin/client.rb', line 127 def configure(={}) Configuration.configure() end |
- (Object) create_cart(*items)
Performs an CartCreate REST call against the Amazon API.
Expects one ore more item-hashes and returns a SimpleCart:
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
254 255 256 |
# File 'lib/asin/client.rb', line 254 def create_cart(*items) cart(:CartCreate, create_item_params(items)) end |
- (Object) get_cart(cart_id, hmac)
Performs an CartGet REST call against the Amazon API.
Expects the CartId and the HMAC to identify the returning SimpleCart:
cart = get_cart('176-9182855-2326919', 'KgeVCA0YJTbuN/7Ibakrk/KnHWA=')
264 265 266 |
# File 'lib/asin/client.rb', line 264 def get_cart(cart_id, hmac) cart(:CartGet, {:CartId => cart_id, :HMAC => hmac}) end |
- (Object) lookup(*asins)
Performs an ItemLookup REST call against the Amazon API.
Expects an arbitrary number of ASIN (Amazon Standard Identification Number) and returns an array of SimpleItem:
item = lookup '1430218150'
item.title
=> "Learn Objective-C on the Mac (Learn Series)"
items = lookup ['1430218150', '0439023521']
items[0].title
=> "Learn Objective-C on the Mac (Learn Series)"
items[1].title
=> "The Hunger Games"
Options:
Additional parameters for the API call like this:
lookup(asin, :ResponseGroup => :Medium)
Or with multiple parameters:
lookup(asin, :ResponseGroup => [:Small, :AlternateVersions])
154 155 156 157 158 |
# File 'lib/asin/client.rb', line 154 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_item(item)} end |
- (Object) search(params = {:SearchIndex => :Books, :ResponseGroup => :Medium})
Performs an ItemSearch REST call against the Amazon API.
Expects a Hash of search params and returns a list of SimpleItems:
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
196 197 198 199 |
# File 'lib/asin/client.rb', line 196 def search(params={:SearchIndex => :Books, :ResponseGroup => :Medium}) response = call(params.merge(:Operation => :ItemSearch)) arrayfy(response['ItemSearchResponse']['Items']['Item']).map {|item| handle_item(item)} end |
- (Object) search_keywords(*keywords)
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 SimpleItems:
items = search_keywords 'Learn', 'Objective-C'
items.first.title
=> "Learn Objective-C on the Mac (Learn Series)"
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
176 177 178 179 180 |
# File 'lib/asin/client.rb', line 176 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_item(item)} end |
- (Object) similar(*asins)
Performs an SimilarityLookup REST call against the Amazon API.
Expects one ore more asins and returns a list of SimpleNodes:
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
234 235 236 237 238 |
# File 'lib/asin/client.rb', line 234 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_item(item)} end |
- (Object) update_items(cart, *items)
Performs an CartModify REST call against the Amazon API.
Expects a SimpleCart created with create_cart and one ore more Item-Hashes to modify and returns an updated SimpleCart:
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
300 301 302 |
# File 'lib/asin/client.rb', line 300 def update_items(cart, *items) cart(:CartModify, create_item_params(items).merge({:CartId => cart.cart_id, :HMAC => cart.hmac})) end |