Ruby Amazon E-Commerce REST Service API   (amazon-ecs)

What is amazon-ecs?

A generic Amazon E-commerce REST API with configurable default options and method call options. It uses Hpricot to parse the XML output. Use Response and Element wrapper classes for easy access to the XML elements, and it supports ECS 4.0.

It is generic, so you can extend Amazon::Ecs to support the other not-implemented operations easily; and the response object just wraps around Hpricot element object, instead of providing one-to-one object/attributes to XML elements map.

With that, if in the future, there is a change in REST XML output structure, no changes will be required on amazon-ecs, instead you just need to change your element path.

And if you love history, click here.

Links

Installation

$ gem install amazon-ecs

Example

Initiating a search request:

require 'amazon/ecs'

# default options; will be camelized and converted 
# to REST request parameters.
Amazon::Ecs.options = {:aWS_access_key_id => [your access key]}
res = Amazon::Ecs.item_search('ruby')

# or you can provide additional options
res = Amazon::Ecs.item_search('ruby', :response_group => 'Medium', :sort => 'salesrank')

# to search other Amazon UK
res = Amazon::Ecs.item_search('ruby', :country => :uk)

Retrieving common response values:

# some common response object methods
res.is_valid_request?
res.has_error?
res.error
res.total_pages
res.total_results
res.item_page

Retrieving items, and item attributes:

# traverse through each item (Amazon::Element)
res.items.each do |item|
  # retrieve string value using XML path
  item.get('asin')
  item.get('itemattributes/title')

  # or return Amazon::Element instance
  atts = item.search_and_convert('itemattributes')
  
  # get title as a string 
  atts.get('title')

  # get only returns the first element of an array
  atts.get('author')            # 'Author 1'

  # to retrieve an array
  atts.get_array('author')    # ['Author 1', 'Author 2', ...]

  # to retrieve a hash of children text values
  item.get_hash('smallimage') # {:url => ..., :width => ..., :height => ...}

  # '/' returns Hpricot::Elements array object, nil if not found
  reviews = item/'editorialreview'

  # traverse through Hpricot elements
  reviews.each do |review|
    # Getting a hash value out of Hpricot element
    Amazon::Element.get_hash(review) # [:source => ..., :content ==> ...]

    # Or to get unescaped HTML values
    Amazon::Element.get_unescaped(review, 'source')
    Amazon::Element.get_unescaped(review, 'content')
        
    # Or this way
    el = Amazon::Element.new(review)
    el.get_unescaped('source')
    el.get_unescaped('content')  
  end
end

Tip

If you are on irb console, this trick helps to print a nicely formatted xml result:

require 'pp'
pp res = Amazon::Ecs.item_search('ruby').doc

More Information

Refer to Amazon ECS documentation for more information on Amazon REST request parameters and XML output structure.

Or you can also get a sample of Amazon REST XML output from AWSZone.com.

18 Comments . Comments Feed . Trackback URI
Fri, 25 May 07 10:49 am . Tad wrote:

I have integrated Amazon-ecs into my site and it is working very well on my PC. When I upload it to the host, however, I get the following error:

stack level too deep ../vendor/rails/activesupport/lib/active_support/
vendor/builder/blankslate.rb:58:in `blank_slate_method_added’

Here is a larger chunk of the backtrace…
../vendor/rails/activesupport/lib/active_support/vendor/builder/blankslate.rb:58:in `blank_slate_method_added’
../vendor/rails/activesupport/lib/active_support/vendor/builder/blankslate.rb:58:in `blank_slate_method_added’
/home/hic/gems/gems/hpricot-0.5.123/lib/hpricot/blankslate.rb:58:in `method_added’
/home/hic/gems/gems/hpricot-0.5.123/lib/hpricot/builder.rb:154
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require’
/home/hic/gems/gems/hpricot-0.5.123/lib/hpricot.rb:26
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require’
/home/hic/gems/gems/amazon-ecs-0.5.1/lib/amazon/ecs.rb:25
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:32:in `require’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require’
../app/models/book.rb:1
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:104:in `require_or_load’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:248:in `load_missing_constant’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:452:in `const_missing’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:464:in `const_missing’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:260:in `load_missing_constant’
../vendor/rails/activesupport/lib/active_support/dependencies.rb:468:in `const_missing’
../app/controllers/approaches_controller.rb:58:in `get_info’

Do you have an idea of why this is happening, or, more importantly, how to keep it from happening?

Thanks for your help and the very useful gem!

Fri, 25 May 07 08:08 pm . Herryanto Siatono wrote:

Tad, seems like a bug in Hpricot. Found this when I googled.

http://code.whytheluckystiff.net/hpricot/ticket/73

Has just been fixed lately.

Wed, 30 May 07 08:42 am . Tad wrote:

Thanks for the link. After I upgraded Hpricot it started working very nicely.

Mon, 16 Jul 07 01:25 am . Mike wrote:

Good stuff, Herryanto! I’ve been using amazon-ecs for a new site, and it’s working perfectly. Thank you!

Mon, 16 Jul 07 09:16 am . Herryanto Siatono wrote:

You are welcome, Mike, great that it works.

Thu, 9 Aug 07 03:11 pm . Binary Code » Rails ActiveRecord Instances and ’super’ wrote:

[…] For a class of objects in my database I extract data from one or two ways: the first directly from the database via ActiveRecord, the second from Amazon using Amazon::Ecs. […]

Thu, 16 Aug 07 03:04 pm . Dave wrote:

Thanks a lot for the API, Herryanto. Was looking for something that supported ECS 4.0 and this works like a charm.

Thu, 13 Sep 07 03:21 am . tota diary wrote:

proxy経由でamazon-ecsを利用するためのpatch…

A patch for amazon-ecs to enable to use http proxy…

Tue, 18 Sep 07 04:29 am . links for 2007-09-17 wrote:

[…] Pluit Solutions » Ruby Amazon E-Commerce REST Service API (amazon-ecs) A Hpricot-based library for Amazon E-Commerce Services (tags: api aws amazon ruby ecs) […]

Wed, 17 Oct 07 01:02 pm . ror blog » Blog Archive » [Rails] Re: Production mode bug with ruby/amazon wrote:

[…] For what it’s worth, I’ve now ditched Ruby/Amazon and made the move to ECS 4 (ECS 3 is being phased out early next year, so you’ll need to do it pretty soon), and used the amazon-ecs gem (http://www.pluitsolutions.com/projects/amazon-ecs). […]

Sat, 10 Nov 07 12:05 pm . Bernhard wrote:

Just what I needed! Easy enought for a Ruby beginner. Thanks a lot!

Sun, 25 Nov 07 10:14 am . Dan wrote:

Thanks for building this! I was using the Ruby/Amazon plugin, but yours is a lot cleaner.

I have one question, though: when I try a test search using AWSzone for a book, and select all the possible checkboxes, there are lots of fields I get that your Gem doesn’t return. For example, ProductDimensions (width, height, weight). Is there a way to get at that data?

Sun, 25 Nov 07 10:14 am . Dan wrote:

My bad! I didn’t read closely enough. If I add :response_group => ‘Large’ I get back the package dimensions attributes. Whee!

Fri, 30 Nov 07 11:08 pm . Tools in the Studio « syntatic wrote:

[…] I’ve been working with Amazon ECS, Streamlined and a session bridge between rails and Perl’s CGI Session. I strapped subdomain/SEO love onto a project using request_routing, url_for_domain, and acts_as_sluggable. Nate and I pulled ActiveMerchant into a project which was much less painful than expected. If you’re doing complex condition building my tool of choice is condition_builder. I also extended restful_authentication so that it can support authentication for multiple types of users. […]

Sun, 6 Jan 08 01:10 am . Dianna wrote:

Hi, I can’t get this working… can you help me out?
Rails 1.2.3
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]

sudo gem install amazon-ecs
Install required dependency hpricot? [Yn] Y
Select which gem to install for your platform (i686-darwin8.9.1)
1. hpricot 0.6 (mswin32)
2. hpricot 0.6 (jruby)
3. hpricot 0.6 (ruby)
4. hpricot 0.5 (ruby)
5. hpricot 0.5 (mswin32)
6. Skip this gem
7. Cancel installation
> 3
Building native extensions. This could take a while…
ERROR: While executing gem … (Gem::Installer::ExtensionBuildError)
ERROR: Failed to build gem native extension.

ruby extconf.rb install amazon-ecs
checking for main() in -lc… no
creating Makefile

make
gcc -I. -I. -I/usr/local/lib/ruby/1.8/i686-darwin8.9.1 -I. -fno-common -g -O2 -pipe -fno-common -c hpricot_scan.c
cc -dynamic -bundle -undefined suppress -flat_namespace -L”/usr/local/lib” -o hpricot_scan.bundle hpricot_scan.o -lpthread -ldl -lobjc
/usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libpthread.dylib unknown flags (type) of section 6 (__TEXT,__dof_plockstat) in load command 0
/usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libdl.dylib unknown flags (type) of section 6 (__TEXT,__dof_plockstat) in load command 0
/usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libobjc.dylib load command 9 unknown cmd field
/usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libSystem.dylib unknown flags (type) of section 6 (__TEXT,__dof_plockstat) in load command 0
collect2: ld returned 1 exit status
make: *** [hpricot_scan.bundle] Error 1

Gem files will remain installed in /usr/local/lib/ruby/gems/1.8/gems/hpricot-0.6 for inspection.
Results logged to /usr/local/lib/ruby/gems/1.8/gems/hpricot-0.6/ext/hpricot_scan/gem_make.out

Sun, 6 Jan 08 01:51 am . Dianna wrote:

PS. gem version 0.9.2

Sun, 6 Jan 08 11:02 am . Herryanto Siatono wrote:

Seems like the hpricot installation failed. You can also install it separately, then install amazon-ecs.

gem install hpricot

If it still fails, you may want to get help from Hpricot mailing list. Hope it helps.

Sun, 6 Jan 08 02:54 pm . Dianna wrote:

Thanks, Herry. I did some more searching and found this:

http://www.prestonlee.com/archives/184

It says that since I have Leopard, I need to upgrade Xcode also. I can’t do this right now since I don’t want to mess up another app that is for work, but I will try it out and hopefully write back and let everyone know if that was the issue… in case anyone has the same problem :)

Add Your Comment



(optional)