acts_as_amazon_product

Today we released a new Ruby Gem that makes integrating with Amazon E-Commerce Service (ECS) a snap. It’s called acts_as_amazon_product and you can find it on RubyForge.

All that’s necessary to integrate any of your existing models with Amazon is to add a require line to the top of your model file and then an acts_as_amazon_product line just inside your class definition.

require 'acts_as_amazon_product'
 
class Book < ActiveRecord::Base
  acts_as_amazon_product(
      :asin => 'isbn', :name => 'title', 
      :access_key => '0123456', 
      :associate_tag => 'assoc-20')
end

After this, you can access Amazon data in your controllers or views like this:

@book = Book.new(:title => 'Getting Things Done')
@book.amazon.isbn
@book.amazon.title
@book.amazon.author
@book.amazon.small_image_url
 
@book.amazon.get('itemattributes/foobar')

The code does not require changing any current database tables. It only requires adding one migration for a single new table used to cache responses from Amazon:

ActiveRecord::Base.connection.create_table :amazon_products do |t|
  t.column :asin, :string
  t.column :xml, :text
  t.column :created_at, :datetime
  t.column :amazonable_id, :integer
  t.column :amazonable_type, :string
end

Leave a Reply