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.

31 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 :)

Mon, 11 Feb 08 01:46 am . Thibaut Barrère wrote:

Hi Herryanto,

I want to say thank you for releasing amazon/ecs. I’m using it with Mephisto to build a little book-store. Hpricot make it very easy to get everything needed.

thanks!

Sun, 17 Feb 08 12:29 am . Thibaut Barrère wrote:

I’ve released an adaptation of Nicholas Faiz Mephisto plugin where I use amazon/ecs instead of ruby/amazon.

Code is available here for those who may need it:

http://code.google.com/p/logeek-labs/

cheers.

Sun, 17 Feb 08 08:17 am . dianna wrote:

I’m back with a fresh install and xcode 3! I got everything installed fine :) One thing I am having trouble with is… I used “gemsonrails” to get amazon-ecs into my vendor directory

http://gemsonrails.rubyforge.org/

It went fine, but now when I try to start up the server, I get

ERROR: Please update /Users/dianna/Sites/mysite/vendor/gems/amazon-ecs-0.5.3/init.rb with the require path for linked RubyGem amazon-ecs

Can you tell me what path this could be looking for??

Thanks!!

Sun, 17 Feb 08 08:52 am . dianna wrote:

actually, sorry, i read some more (duh) and it turned out to be a file that was generated by the freeze… so i figured it out.

thanks!

Sun, 17 Feb 08 10:36 am . dianna wrote:

Can you advise me on how I should output the image? I finally figured out how I can get the data, and I could probably hack something up to output the image, but there has to be an easier way…

This is what I have to get the data

:

Sun, 17 Feb 08 12:34 pm . dianna wrote:

Ok I’m figuring stuff out… slowly! :)

I’m looking at the Amazon API and it says that you can enter a search index, which I am doing, but “blended” doesn’t work…

Any ideas?

http://docs.amazonwebservices.com/AWSEcommerceService/2005-10-05/index.html

Sun, 17 Feb 08 12:56 pm . Herryanto Siatono wrote:

Yep you can’t enter more than one index at a time, you probably want to raise this in Amazon Developers Forum. There may be some way or may be not, worse case scenario, multiple requests, one for each index.

Sun, 17 Feb 08 01:34 pm . dianna wrote:

But from the API, it says you can enter “blended” as the search index and that includes all of them.

http://docs.amazonwebservices.com/AWSEcommerceService/2005-10-05/ApiReference/ItemSearchOperation.html

If you select “blended” and view the results, it shows:
Argument Name=”SearchIndex” Value=”Blended”

http://www.awszone.com/scratchpads/aws/ecs.us/ItemSearch.aws

Am I missing something??

Sun, 17 Feb 08 01:43 pm . Herryanto Siatono wrote:

Ah….there’s a search index option ‘Blended’, meaning that it searches into a group of search indices as listed.

Amazon::Ecs.item_search(’test’, :search_index => ‘Blended’).

But I think you can’t specify like a specific group of indexes, like if you want to search only ‘Books’ and ‘Software’ indexes. To be sure drop a note in the developers forum.

Sun, 17 Feb 08 03:07 pm . dianna wrote:

@res = Amazon::Ecs.item_search(params[:amazon][:keywords], :type => ‘Keywords’, :response_group => ‘Large’, :sort => ’salesrank’, :search_index => ‘Blended’)

That brings back zero results… I just figured out what is throwing it off… if I remote the :sort, I get results… so that must not be supported with the blended search index.

Now to figure out how to sort those results! :)

Thanks for your help :)

Sat, 29 Mar 08 04:39 am . Jason wrote:

Totally new to rails and totally lost!

I like the idea of using this gem. I installed the gem. I made a controller and put the following code in the controller.

require ‘amazon/ecs’
Amazon::Ecs.options = {:aWS_access_key_id => ‘my key’}
@res = Amazon::Ecs.item_search(’ruby’)

that part seems to work as i can call res.total_results and I get a really big number.

I have no idea how to display any books in a view. for example how can i access the first 5 books in a view?

Ideally what i want is:
title:
medium image:
and price:

Any help would be great as I have been trying to learn rails. Thanks

Mon, 7 Apr 08 06:39 am . Chris K. wrote:

Hey Jason, I’m a newbie too. It’s taking me a while to figure out what all is going on. But Herryanto’s API is a great way to get something cool going on Rails. Kudos to you, Herry.

I use it like this. I have a model, let’s say “Book”, with fields like:

asin
title
author
ean
list_price
medium_image_url
small_image_url

Then in my controller (among other methods), I have a grab, a list, and a show like this:

def grab

Amazon::Ecs.options = {:aWS_access_key_id => ‘myKey’}
products = Amazon::Ecs.item_search(’mySearchTerm’)
# 2 blocks/loops here: the first one walks through amazon’s list of titles
# second one is just a block filling out the new record.
products.items.each do |item|
@book = Book.new do |b|
b.asin = item.get(’asin’)
b.title = item.get(’itemattributes/title’)
b.author = item.get(’itemattributes/author’)
b.medium_image_url = item.get(’mediumimage/url’)
b.small_image_url = item.get(’mediumimage/url’)
b.list_price = item.get(’itemattributes/listprice/formattedprice’)
end
end
if @item.save
flash[:notice] = ‘Item was successfully created.’
redirect_to :action => ’show’, :id => @item
else
flash[:warning] = ‘Save did not succeed.’
render :action => ‘grab’
end
end

def list
@books = Book.find(:all)
end

def show
@book = Book.find(params[:id])
end

Then in my views, I’ll have a show.html.erb and a list.html.erb like this

Show:

“>
Title:
Author:
…etc.

List:

Jacket
Title
Author
List Price

“>

Format to taste.

A sloppy summary, but maybe that’ll give you some ideas. Here is the site that I use to figure out what the Amazon field names and their paths are. I’ve been using it a lot :

http://www.awszone.com/scratchpads/aws/ecs.us/ItemSearch.aws

Good luck.

Wed, 9 Apr 08 11:55 pm . Rudie wrote:

Pfff… you made it real easy for me! ;)

5 minutes work!

Add Your Comment



(optional)