ryanmarshall / sucker

A minimal Ruby wrapper to the Amazon Product Advertising API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sucker

Sucker is a Nokogiri-based Ruby wrapper to the Amazon Product Advertising API.

Sucker is fast and supports the entire Amazon API.

Hoover

Usage

Read the API. Jump to the Operations section if in a hurry.

Set up.

worker = Sucker.new \
  :locale => :us,
  :key    => api_key,
  :secret => api_secret

Build a request.

worker << {
  'Operation'     => 'ItemLookup',
  'IdType'        => 'ASIN',
  'ItemId'        => 10.asins,
  'ResponseGroup' => 'ItemAttributes' }

Get a response.

response = worker.get

Fulfill a business value.

if response.valid?
  response.each('Item') do |item|
    consume item
  end
end

Repeat ad infinitum.

The following are all valid ways to query a response:

items = response.find('Item')
items = response['Item']
items = response.map('Item') { |item| ... }
response.each('Item') { |item| ... }

To dig further into the response object:

p response.valid?,
  response.body,
  response.code,
  response.errors,
  response.has_errors?,
  response.to_hash,
  response.xml

Read further here and here.

API Usage

We have a home-grown collection of gems that help us manage our (relatively heavy) use of the Amazon API.

  • Multiplex binds a request to a specified local IP.
  • Throttler throttles requests to a venue to one per second per IP.

A hypothetical setup:

require 'multiplex'
require 'throttler'

ips.each do |ip|
  Thread.new do
    scope = "#{ip}-#{locale}"
    Throttler.throttle(scope) do
      Net::HTTP.bind ip do
        # Set up worker
        response = worker.get
        # Consume response
      end
    end
  end
end

We prefer to use Resque to manage multiple requests.

Generally, four or five workers per locale per IP proves enough to provide optimum throughput.

Stubbing in Tests

Use VCR.

Compatibility

Specs pass against Ruby 1.8.7, Ruby 1.9.2, JRuby 1.5.6, and Rubinius 1.2.1.

Moral of the story

Don't overabstract a spaghetti API.

About

A minimal Ruby wrapper to the Amazon Product Advertising API

License:MIT License


Languages

Language:Ruby 100.0%