ASIN-WEB
examples using the ASIN gem
Install:
Installing ASIN for Rails
4.0.0.beta
is easy. Just put it into your
Gemfile:
source :rubygems ruby "1.9.3" gem "rails", github: "rails/rails" gem "activerecord-deprecated_finders", github: "rails/activerecord-deprecated_finders" gem "journey", github: "rails/journey" gem "sprockets-rails", github: "rails/sprockets-rails" gem "asin", "~> 1.0" gem "httpclient", "~> 2.2" gem "haml", "~> 3.1" gem "coderay", "~> 1.0" gem "thin", "~> 1.4" gem "foreman", "~> 0.51" group :assets do gem "sass-rails", github: "rails/sass-rails" gem "uglifier", "~> 1.2" end
ASIN has a modular setup and is built on top of the HTTP adapter library
HTTPI.
Since
HTTPClient
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 = :httpclient 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.title end end
The result of this query executed in
LookupExample#lookup_item_title
is:
"Learn Objective-C on the Mac (Learn Series)"
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(&:title) end end
Calling
SearchExample#search_ruby_titles
results in a list of Ruby and Ruby on Rails titles:
"Ruby on Rails Tutorial: Learn Web Development with Rails (2nd Edition) (Addison-Wesley Professional Ruby Series)", "Ruby on Rails 3 Tutorial: Learn Rails by Example (Addison-Wesley Professional Ruby Series)", "Practical Object-Oriented Design in Ruby: An Agile Primer (Addison-Wesley Professional Ruby Series)", "Agile Web Development with Rails (Pragmatic Programmers)", "Ruby on Rails 3.2 - Step by Step", "Ruby on Rails For Dummies", "The Rails 3 Way (2nd Edition) (Addison-Wesley Professional Ruby Series)", "Ruby for Rails: Ruby Techniques for Rails Developers", "The Ruby Programming Language", "Ruby on Rails Tutorial and LiveLesson Video Bundle: Learn Web Development with Rails (2nd Edition) (LiveLessons)"
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).url end end
This query of the method
CartExample#checkout_url
results in a checkout deeplink to the Amazon page:
Learn Objective-C on the Mac (Learn Series)
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"