« rails で apache + fastcgi | メイン | rubyでhtmlを解析 »

ruby で amazon WEBサービス

[ruby] [amazon]

amazon-ecs-0.5.0

AmazonのWEBサービスを利用するためのrubyのライブラリ。
gemで提供されているので入れてみる。


gem install amazon-ecs

設定はこんな感じ。とりあえずASINコードで検索してみる。

Amazon::Ecs.options = {:aWS_access_key_id => ["your developer token"], :AssociateTag => "associate_id", :country => :jp}

res = Amazon::Ecs.item_lookup("4088762096")

以下READMEから抜粋。


    # set the default options; options will be camelized and converted to REST request parameters.
    Amazon::Ecs.options = {:aWS_access_key_id => [your developer token]}

    # options provided on method call will merge with the default options
    res = Amazon::Ecs.item_search('ruby', {:response_group => 'Medium', :sort => 'salesrank'})

    # some common response object methods
    res.is_valid_request?     # return true request is valid
    res.has_error?            # return true if there is an error
    res.error                 # return error message if there is any
    res.total_pages           # return total pages
    res.total_results         # return total pages
    res.item_page             # return current page no if :item_page option is provided

    # traverse through each item (Amazon::Element)
    res.items.each do |item|
      # retrieve element text value, following the XML output structure
      item.get('asin')
      item.get('itemattributes/title')

      # or you can also do it this way, to retrieve the title
      atts = item.get('itemattributes')
      atts.get('title')

      # return first author or a string array of authors
      atts.get('author')          # 'Author 1'
      atts.get_array('author')    # ['Author 1', 'Author 2', ...]

      # return an hash of children text values with the element names as the keys
      item.get_hash('smallimage') # {:url => ..., :width => ..., :height => ...}

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

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

        # Or can retrieve them seperately
        Amazon::Element.get(review, 'source')
        Amazon::Element.get(review, 'content')
      end
    end


参考URL

が、とんでもないバグを発見。
オプションの、:countory キーから、サーバーを選択するためのHashが間違ってる。

以下その修正のパッチ。パッチあてるより直接書き換えたほうが早いかも。


*** amazon-ecs-0.5.0/lib/amazon/ecs.rb.orig     2007-01-23 18:51:41.000000000 +0900
--- amazon-ecs-0.5.0/lib/amazon/ecs.rb  2007-01-23 18:47:22.000000000 +0900
***************
*** 11,18 ****
          :uk => 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
          :ca => 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService',
          :de => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
!         :jp => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
!         :fr => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService'
      }

      @@debug, @@options = nil
--- 11,18 ----
          :uk => 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
          :ca => 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService',
          :de => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
!         :fr => 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
!         :jp => 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService'
      }

      @@debug, @@options = nil

トラックバック

このエントリーのトラックバックURL:
http://i-am.web777.net/mt/mt-tb.cgi/20

コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)