Amazon on Rails - search Amazon using Rails and Ruby/Amazon August 31, 2006

Posted by Slobodan Kovacevic in : Programming, Ruby & Rails , trackback

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.

private
  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:

map.search ‘:mode/:query/:page’,
              :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»

1. AS Workshop » Make OL list start from number different than 1 using CSS - August 31, 2006

[…] Now, there is a twist in the tale - even though this rule is specified in W3C CSS2 standard (with even more great solutions, that will make XHTML coder’s live easier), IE 6 completely ignores these rules! However, in the Amazon Search created in Ruby example, created by my colleague, you can see how IE6 ignores the rule, but search results appear correctly, and the whole appearance looks acceptable. However, if you use Firefox, you’ll see our OL list in its full glory. […]

2. Derek - October 28, 2006

Wow, thank god for finding this. I been all over looking for something like this.

Thanks a ton!

3. Jeff - April 2, 2007

I don’t have root on my server, is there a way to get this to work without root?

4. Slobodan Kovacevic - April 2, 2007

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.