acts_as_amazon_product

Posted by Scott on September 26, 2007

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.

[ruby]
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
[/ruby]

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

[ruby]
@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’)
[/ruby]

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:

[ruby]
ActiveRecord::Base.connection.create_table :amazon_products do |t|
t.column :asin, :string
t.column :xml, :text
t.column :created_at, :datetime, :null => false
t.column :amazonable_id, :integer, :default => 0, :null => false
t.column :amazonable_type, :string, :limit => 15, :default => “”, :null => false
end
[/ruby]

Leave a Reply

Who Are These Guys?

Netphase, LLC. is a Charlotte-based web application development company specializing in really rapid application development on the Ruby on Rails framework.

Netphase was founded by Scott Nedderman and Chris Beck in 2007. Both Scott and Chris have been active Ruby on Rails developers for more than 3 years, and each has spent more than a decade designing, devloping and deploying web applications.

We have worked on projects of all sizes, and delivered successful sites to production for some of the biggest names in the business. For all of the gory details, check out our about page.

Bookmark This

del.icio.us Digg Furl reddit