Docunext


Ruby Gem ruby aaws

March 8th, 2010

This is an awesome gem, though I ran into many errors when I first started to try it out. Here's the code I was trying:

require 'amazon/aws'
require 'amazon/aws/search'
require 'pp'
include Amazon::AWS
include Amazon::AWS::Search
ASSOCIATES_ID = "removed"
KEY_ID = "removed"
#req = Request.new(KEY_ID, ASSOCIATES_ID, 'us', false)
is = ItemSearch.new( 'Books', { 'Title' => 'Ruby' } )
is.response_group = ResponseGroup.new( :Small )
req = Request.new(KEY_ID, ASSOCIATES_ID, 'us', false)
resp = req.search( is )
pp(resp.item_search_response)
pp(resp)

I first had trouble figuring out that I needed to put my AWS "secret" key in ~/.amazonrc.

I was getting UTF-8 errors, too. I think adding this to my ~/.amazonrc fixed the problem:

encoding = 'UTF-8'

Hmmm. I guess using the cache also causes UTF-8 related errors.

UPDATE: Finally, late last night I figured it out. The example aaws code I was basing my tests on uses ItemLookup.

ItemLookup has an item_lookup_response object. When I switched to searching, item_lookup_response is not there. I eventually switched to item_search_response, but had cleared out the rest of the example for debugging. Adding it back in returned the right set.
is = ItemSearch.new( 'Books', { 'Title' => 'Ruby' } )
is.response_group = ResponseGroup.new( :Small )
req = Request.new(KEY_ID, ASSOCIATES_ID, 'us', true)
resp = req.search( is )
item_sets = resp.item_search_response[0].items
  item_sets[0..3].each do |item_set|
  item_set.item.each do |item|
    attribs = item.item_attributes[0]
    puts attribs
  end
end

The way the secret key is handled seems odd. Maybe there is a better way? The ruby-aaws RDoc says its the only way though.

UPDATE: There are different ways to set where the amazonrc file is. In my case, running unicorn via daemontools caused ENV['HOME'] to be nil, resulting in amazon.rb trying to evaluate ENV['HOMEDRIVE'] + ENV['HOMEPATH'], causing a NoMethodError that was very difficult to decipher (running unicorn with the "-d" option after invoking the Ruby interpreter helped track down the cause).

I had tried setting $AMAZONRCFILE at first, then $AMAZONRCDIR, but that didn't work I noticed in amazon.rb that it was looking for ENV['AMAZONRCDIR'], so I set it in config.ru and voila - functionality!!

UPDATE:

I've send Ian an email:

Hi Ian,

Nice work on the ruby-aaws gem. I'm using it with Sinatra and Unicorn, which is run by daemontools. For whatever reason, ENV['HOME'] was resulting in nil, causing it to try ENV['HOMEDRIVE'] + ENV['HOMEPATH'], which for me resulted in a NoMethodError.

Setting ENV['AMAZONRCDIR'] fixed the issue for me, but perhaps you'd consider putting an exception trap there, as the NoMethodError is so vague it was tough to track down.

Thanks for this sweet gem!

  • Albert

PS - I've blogged about the gem, too:

http://www.docunext.com/2010/03/ruby-gem-ruby-aaws/

Still not sure what the problem is with UTF-8 and caching....

--

http://www.docunext.com/

¥

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022