Fork me on GitHub

ASIN-WEB

examples using the ASIN gem

Install:

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

source "http://rubygems.org"
ruby "2.1.1"

gem "rails",    "4.1.1"

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

gem "haml",     "~> 4.0"
gem "rouge",    "~> 1.3"
gem "thin",     "~> 1.6"
gem "foreman",  "~> 0.51"

group :assets do
  gem "sass-rails", "~> 4.0"
  gem "uglifier",   "~> 2.1"
end

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

require 'httpi'
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 = '599826'
    @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: Comedy