Fork me on GitHub

ASIN-WEB

examples using the ASIN gem

Install:

Installing ASIN for Rails 5.1.4 is easy. Just put it into your Gemfile

source 'https://rubygems.org'
ruby   '2.4.2'

gem 'rails',      '~> 5.1'
gem 'erubis',     '~> 2.7'
gem 'puma',       '~> 3.6'

gem 'asin',       '~> 2.0'
gem 'curb',       '~> 0.8'

gem 'haml',       '~> 5.0'
gem 'rouge',      '~> 3.0'
gem 'memo-it',    '~> 0.4'
gem 'sass-rails', '~> 5.0'
gem 'uglifier',   '~> 3.0'

ASIN has a modular setup and is built on top of the HTTP adapter library HTTPI. Since Curb is my favourite HTTP library, i put this into the Gemfile too.

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

HTTPI.adapter = :curb
HTTPI.logger  = Rails.logger

I am used to read the config-vars from the ENV because it's the best way to configure stuff on Heroku.

Beeing a lazy programmer, I tend to put the configuration of HTTPI into the same file.

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: Learn Objective-C on the Mac (Learn Series)

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: DVD & Blu-ray