ASIN-WEB
examples using the ASIN gem
Install:
Installing ASIN for Rails
7.2.1.2
is easy. Just put it into your
Gemfile
source 'https://rubygems.org'
ruby '3.3.5'
gem 'rails', '~> 7.0'
gem 'erubis', '~> 2.7'
gem 'puma', '~> 5.6'
gem 'asin', '~> 2.0'
gem 'haml', '~> 5.0'
gem 'rouge', '~> 3.0'
gem 'memo-it', '~> 0.4'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '~> 3.0'
Config:
The most common way of configuring ASIN is via a Rails initializer like this
asin.rb
file:
ASIN::Configuration.configure do |config|
config.secret = ENV['ASIN_SECRET']
config.key = ENV['ASIN_KEY']
config.associate_tag = ENV['ASIN_TAG']
config.host = ENV['ASIN_HOST'] || 'webservices.amazon.de'
end
I am used to read the config-vars from the
ENV
because it's the best way to configure stuff on
Heroku.
Lookup:
The
ASIN::Client#lookup
method is the simplest form of accessing the Amazon API:
class LookupExample
def initialize
@client = ASIN::Client.instance
end
def lookup_item_title
asin = '1430218150'
@client.lookup(asin).first.item_attributes.title
end
end
The result of this query executed in
LookupExample#lookup_item_title
is:
an error occured: request failed with response-code='301'
Search:
The
ASIN::Client#search_keywords
methods excepts one or more keywords to search against the Amazon book index:
class SearchExample
def initialize
@client = ASIN::Client.instance
end
def search_ruby_titles
@client.search_keywords('ruby', 'rails').map { |item| item.item_attributes.title }
end
end
Calling
SearchExample#search_ruby_titles
results in a list of Ruby and Ruby on Rails titles:
an error occured: request failed with response-code='301'
Cart:
With the
ASIN::Client#*_cart
methods, you get a convenient layer to access your virtual shopping cart:
class CartExample
def initialize
@client = ASIN::Client.instance
end
def checkout_url
@client.create_cart(asin: '1430218150', quantity: 1).purchase_url
end
end
This query of the method
CartExample#checkout_url
results in a checkout deeplink to the Amazon page:
Purchase Me
Node:
If you are interested in accessing Amazon browse nodes, you should have a look at the
ASIN::Client#browse_node
method:
class NodeExample
def initialize
@client = ASIN::Client.instance
end
def node_name
node_id = '284266' # DVD & Blu-ray, https://www.amazon.de/dvd-blu-ray-filme-3D-vhs-video/b/ref=sd_allcat_dvd_blu?ie=UTF8&node=284266
@client.browse_node(node_id, ResponseGroup: :TopSellers).first.name
end
end
In this case, the name of the node from the
NodeExample#node_name
method is:
an error occured: request failed with response-code='301'