Amazon on Rails – search Amazon using Rails and Ruby/Amazon
Posted By Slobodan Kovacevic on August 31, 2006
Currently I am working on my first big Rails application and I needed an simple and quick way to get matching movies and music based on user entered keywords. The best place to get such information is Amazon as they have almost anything in their store.
So I decided to write a small Rails application that takes input from user and displays products from Amazon matching that criteria – in essence Amazon search. In the end it will look something like this – Amazon on Rails
Ruby/Amazon
First I needed a way to communicate with Amazon Web Service. After some searching I found that you could use ActionWebService to do that, but too many people said that this was way too complicated and suggested using Ruby/Amazon library instead.
Ruby/Amazon is a Ruby library (i.e. it’s not Rails specific) and it uses REST (XML over HTTP) to talk to Amazon Web Services. The down side of using Ruby/Amazon is that you need to install it on your server, which might require root access to server. Luckily on our TextDrive hosting we already had it installed and installation on local server is simple enough.
You should really read Ruby/Amazon documentation and play around with some scripts that use it (library itself comes with an examples dir with few plain Ruby scripts which you can run in shell) – but to do that you’ll need to get Amazon Associate ID and Developer Token. You can get both by registering on Amazon for Associate Account and on Web Services.
Creating Amazon on Rails app
I should say that this whole example seriously lack error checking, data validation and similar. But this was intended to be quick example of what can be done. That being said, moving on…
Next thing is to create a simple Rails app that will have only one controller (AmazonSearchController) and one action (index – which will be used to display search form and search results). Index method is simple (see amazon_search_controller.rb, you can download it below) and it mainly deals with getting params and paginating result.
The main work is done in amazon_search method, which has been made private so it cannot be called as action.
def amazon_search
req = Request.new(DEV_TOKEN, ASSOCIATES_ID)
case @mode
when ‘actor’
response = req.actor_search(@query, ‘dvd’, HEAVY, @page)
when ‘director’
response = req.director_search(@query, ‘dvd’, HEAVY, @page)
when ‘artist’
response = req.artist_search(@query, ‘music’, HEAVY, @page)
when ‘book’
response = req.keyword_search(@query, ‘books’, HEAVY, @page)
when ‘author’
response = req.author_search(@query, ‘books’, HEAVY, @page)
else
response = nil
end
@products = response.products unless response.nil?
rescue => data
@products = nil
@query = nil
@mode = nil
flash[:notice] = data
end
So, depending on @mode it will invoke different Ruby/Amazon function which will in turn return different set of results. Beside @mode this function requires meaningful @query which will contain keywords to use in search.
Simple URLs
Another thing I wanted to do in this example is to make nice URLs for search pages, so you can link to search results easily. URLs should look like this:
http://arraystudio.textdriven.com/amazon_search/actor/Angelina+Jolie/1
in other words /search_mode/keywords/page. Also, when all params are missing from URL it should only display search form. This can be easily achieved by adding following to routes.rb:
:controller => ‘amazon_search’,
:mode => nil, :query => nil, :page => nil,
:requirements => {:page => /\d{1,5}/}
By setting default values for :mode, :query and :page you are basically telling Rails that this map should be used even if you don’t have anything in URL. Additionally, I also specified that last param should be number – which would act as sort of validation (which is lacking in this whole example as stated before).
In the end…
You can see whole Amazon On Rails example in action without any params. Also, if you’d like to play some more with this example you can download the Amazon On Rails example source code.
There’s a lot more that can be done both with this example and with Ruby/Amazon library. For example, you can use caching methods to speed up frequent searches, you can control users Amazon shopping cart, etc. Basically, you could build the whole site powered completely by Amazon. So, if you expand this example please let me know.
Comments
Derek says:
Jeff says:
Slobodan Kovacevic says:
4 Responses to “Amazon on Rails – search Amazon using Rails and Ruby/Amazon”
October 28th, 2006 at 7:20 pm
Wow, thank god for finding this. I been all over looking for something like this.
Thanks a ton!
April 2nd, 2007 at 6:36 am
I don’t have root on my server, is there a way to get this to work without root?
April 2nd, 2007 at 8:59 am
As far as I know it’s not possible to install Ruby/Amazon library without root access. You are basically installing a new program on your server and if you are on a shared hosting it’s unlikely you can do that.
You should check with your hosting company if they already have Ruby/Amazon installed and if not if they can install it for you. For example, TextDrive hosting already has Ruby/Amazon installed on their shared hosting.