amazon-ecs gem めも

EXAMPLE

require 'amazon/ecs'

# Configure your access key, secret key and other options such as the associate tag.
Amazon::Ecs.configure do |options|
  options[:AWS_access_key_id] = '[your access key]'
  options[:AWS_secret_key] = '[you secret key]'
  options[:associate_tag] = '[your associate tag]'
end

# Or if you need to replace the default options, set the options value directly.
=begin
Amazon::Ecs.options = {
 :version => "2013-08-01",
 :service => "AWSECommerceService"
 :associate_tag => '[your associate tag]',
 :AWS_access_key_id => '[your developer token]',
 :AWS_secret_key => '[your secret access key]'
}
=end

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

# search amazon uk
res = Amazon::Ecs.item_search('ruby', country: 'uk')

# search all items, default search index is Books
res = Amazon::Ecs.item_search('ruby', search_index: 'All')

# some common response object methods
res.is_valid_request?     # return true if 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 results
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 string value using XML path
  item.get('ASIN')
  item.get('ItemAttributes/Title')

  # return Amazon::Element instance
  item_attributes = item.get_element('ItemAttributes')
  item_attributes.get('Title')

  # return first author or a string array of authors
  item_attributes.get('Author')          # 'Author 1'
  item_attributes.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 => ...}

  # return the first matching path as Amazon::Element
  item_height = item.get_element('ItemDimensions/Height')

  # retrieve attributes from Amazon::Element
  item_height.attributes['Units']   # 'hundredths-inches'

  # return an array of Amazon::Element
  authors = item.get_elements('Author')

  # return Nokogiri::XML::NodeSet object or nil if not found
  reviews = item/'EditorialReview'

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

jugend/amazon-ecs(若干修正)

Documentation for amazon-ecs (2.3.0)
optionsに設定する場合は以下のようにも書ける。

Amazon::Ecs.options.merge!({
 associate_tag:     '[your associate tag]',
 AWS_access_key_id: '[your developer token]',
 AWS_secret_key:    '[your secret access key]'
})

ResponseGroup

参考:取得する情報の種類(ResponseGroup) - Amazon Web サービス

書籍(search_index: "Books")を指定したときのresponse_group指定によるレスポンスの違い
response_group ASIN DetailPageURL ItemLinks SalesRank SmallImage MediumImage LargeImage ImageSets ItemAttributes OfferSummary Offers CustomerReviews EditorialReviews SimilarProducts BrowseNodes
Request ASIN
Tracks ASIN
ItemAttributes ASIN DetailPageURL ItemLinks ItemAttributes
SalesRank ASIN SalesRank
Images ASIN SmallImage MediumImage LargeImage ImageSets
OfferSummary ASIN OfferSummary
OfferListings ASIN Offers
OfferFull ASIN OfferSummary Offers
Reviews ASIN CustomerReviews
EditorialReviews ASIN EditorialReviews
Similarities ASIN SimilarProducts
Small ASIN DetailPageURL ItemLinks ItemAttributes
Medium ASIN DetailPageURL ItemLinks SalesRank SmallImage MediumImage LargeImage ImageSets ItemAttributes OfferSummary
Large ASIN DetailPageURL ItemLinks SalesRank SmallImage MediumImage LargeImage ImageSets ItemAttributes OfferSummary Offers CustomerReviews EditorialReviews SimilarProducts BrowseNodes
レスポンスの例

Amazon::Ecs.item_search("Ruby", response_group: "Large", country: "jp").first_item.get_hash で取得した結果(読みやすいように整形した)

{
  "ASIN"=>"4797372273",
  "DetailPageURL"=>"http://www.amazon.co.jp/%E3%81%9F%E3%81%AE%E3%81%97%E3%81%84Ruby-%E7%AC%AC4%E7%89%88-%E9%AB%98%E6%A9%8B-%E5%BE%81%E7%BE%A9/dp/4797372273%3FSubscriptionId%3DAKIAIXUURNVWELYLTCZQ%26tag%3Dxxxxxxxx-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4797372273",
  "ItemLinks"=>"
    <ItemLink>
      <Description>Add To Wishlist</Description>
      <URL>http://www.amazon.co.jp/gp/registry/wishlist/add-item.html%3Fasin.0%3D4797372273%26SubscriptionId%3DAKIAIXUURNVWELYLTCZQ%26tag%3Dxxxxxxxx-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D5143%26creativeASIN%3D4797372273</URL>
    </ItemLink>
    <ItemLink>
      <Description>Tell A Friend</Description>
      <URL>http://www.amazon.co.jp/gp/pdp/taf/4797372273%3FSubscriptionId%3DAKIAIXUURNVWELYLTCZQ%26tag%3Dxxxxxxxx-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D5143%26creativeASIN%3D4797372273</URL>
    </ItemLink>
    <ItemLink>
      <Description>All Customer Reviews</Description>
      <URL>http://www.amazon.co.jp/review/product/4797372273%3FSubscriptionId%3DAKIAIXUURNVWELYLTCZQ%26tag%3Dxxxxxxxx-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D5143%26creativeASIN%3D4797372273</URL>
    </ItemLink>
    <ItemLink>
      <Description>All Offers</Description>
      <URL>http://www.amazon.co.jp/gp/offer-listing/4797372273%3FSubscriptionId%3DAKIAIXUURNVWELYLTCZQ%26tag%3Dxxxxxxxx-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D5143%26creativeASIN%3D4797372273</URL>
    </ItemLink>
  ",
  "SalesRank"=>"11098",
  "SmallImage"=>"
    <URL>http://ecx.images-amazon.com/images/I/41WMC2%2B5rfL._SL75_.jpg</URL>
    <Height Units='pixels'>75</Height>
    <Width Units='pixels'>53</Width>
  ",
  "MediumImage"=>"
    <URL>http://ecx.images-amazon.com/images/I/41WMC2%2B5rfL._SL160_.jpg</URL>
    <Height Units='pixels'>160</Height>
    <Width Units='pixels'>113</Width>
  ",
  "LargeImage"=>"
    <URL>http://ecx.images-amazon.com/images/I/41WMC2%2B5rfL.jpg</URL>
    <Height Units='pixels'>500</Height>
    <Width Units='pixels'>353</Width>  ",
  "ImageSets"=>"
    <ImageSet Category='primary'>
      <SwatchImage>
        <URL>http://ecx.images-amazon.com/images/I/41WMC2%2B5rfL._SL30_.jpg</URL>
        <Height Units='pixels'>30</Height>
        <Width Units='pixels'>21</Width>
      </SwatchImage>
      <SmallImage>
        <URL>http://ecx.images-amazon.com/images/I/41WMC2%2B5rfL._SL75_.jpg</URL>
        <Height Units='pixels'>75</Height>
        <Width Units='pixels'>53</Width>
      </SmallImage>
      <ThumbnailImage>
        <URL>http://ecx.images-amazon.com/images/I/41WMC2%2B5rfL._SL75_.jpg</URL>
        <Height Units='pixels'>75</Height>
        <Width Units='pixels'>53</Width>
      </ThumbnailImage>
      <TinyImage>
        <URL>http://ecx.images-amazon.com/images/I/41WMC2%2B5rfL._SL110_.jpg</URL>
        <Height Units='pixels'>110</Height>
        <Width Units='pixels'>78</Width>
      </TinyImage>
      <MediumImage>
        <URL>http://ecx.images-amazon.com/images/I/41WMC2%2B5rfL._SL160_.jpg</URL>
        <Height Units='pixels'>160</Height>
        <Width Units='pixels'>113</Width>
      </MediumImage>
      <LargeImage>
        <URL>http://ecx.images-amazon.com/images/I/41WMC2%2B5rfL.jpg</URL>
        <Height Units='pixels'>500</Height>
        <Width Units='pixels'>353</Width>
      </LargeImage>
    </ImageSet>
    <ImageSet Category='variant'>
      <SwatchImage>
        <URL>http://ecx.images-amazon.com/images/I/51n1StoOH0L._SL30_.jpg</URL>
        <Height Units='pixels'>30</Height>
        <Width Units='pixels'>21</Width>
      </SwatchImage>
      <SmallImage>
        <URL>http://ecx.images-amazon.com/images/I/51n1StoOH0L._SL75_.jpg</URL>
        <Height Units='pixels'>75</Height>
        <Width Units='pixels'>53</Width>
      </SmallImage>
      <ThumbnailImage>
        <URL>http://ecx.images-amazon.com/images/I/51n1StoOH0L._SL75_.jpg</URL>
        <Height Units='pixels'>75</Height>
        <Width Units='pixels'>53</Width>
      </ThumbnailImage>
      <TinyImage>
        <URL>http://ecx.images-amazon.com/images/I/51n1StoOH0L._SL110_.jpg</URL>
        <Height Units='pixels'>110</Height>
        <Width Units='pixels'>78</Width>
      </TinyImage>
      <MediumImage>
        <URL>http://ecx.images-amazon.com/images/I/51n1StoOH0L._SL160_.jpg</URL>
        <Height Units='pixels'>160</Height>
        <Width Units='pixels'>113</Width>
      </MediumImage>
      <LargeImage>
        <URL>http://ecx.images-amazon.com/images/I/51n1StoOH0L.jpg</URL>
        <Height Units='pixels'>500</Height>
        <Width Units='pixels'>353</Width>
      </LargeImage>
    </ImageSet>
  ",
  "ItemAttributes"=>"
    <Author>高橋 征義</Author>
    <Author>後藤 裕蔵</Author>
    <Binding>単行本</Binding>
    <Creator Role='監修'>まつもと ゆきひろ</Creator>
    <EAN>9784797372274</EAN>
    <EANList>
        <EANListElement>9784797372274</EANListElement>
    </EANList>
    <Edition>第4</Edition>
    <IsAdultProduct>0</IsAdultProduct>
    <ISBN>4797372273</ISBN>
    <Label>ソフトバンククリエイティブ</Label>
    <ListPrice>
        <Amount>2808</Amount>
        <CurrencyCode>JPY</CurrencyCode>
        <FormattedPrice>¥ 2,808</FormattedPrice>
    </ListPrice>
    <Manufacturer>ソフトバンククリエイティブ</Manufacturer>
    <NumberOfPages>520</NumberOfPages>
    <PackageDimensions>
        <Height Units='hundredths-inches'>126</Height>
        <Length Units='hundredths-inches'>835</Length>
        <Weight Units='hundredths-pounds'>154</Weight>
        <Width Units='hundredths-inches'>591</Width>
    </PackageDimensions>
    <ProductGroup>Book</ProductGroup>
    <ProductTypeName>ABIS_BOOK</ProductTypeName>
    <PublicationDate>2013-06-04</PublicationDate>
    <Publisher>ソフトバンククリエイティブ</Publisher>
    <Studio>ソフトバンククリエイティブ</Studio>
    <Title>たのしいRuby 第4版</Title>
  ",

書籍名を取得する場合にはAmazon::Ecs.item_search("Ruby", response_group: "ItemAttributes", country: "jp").first_item.get("ItemAttributes/Title")とする。
著者名を取得する場合にはAmazon::Ecs.item_search("Ruby", response_group: "ItemAttributes", country: "jp").first_item.get_array("ItemAttributes/Author")とする。

  "OfferSummary"=>"
    <LowestNewPrice>
      <Amount>2808</Amount>
      <CurrencyCode>JPY</CurrencyCode>
      <FormattedPrice>¥ 2,808</FormattedPrice>
    </LowestNewPrice>
    <LowestUsedPrice>
      <Amount>2100</Amount>
      <CurrencyCode>JPY</CurrencyCode>
      <FormattedPrice>¥ 2,100</FormattedPrice>
    </LowestUsedPrice>
    <TotalNew>1</TotalNew>
    <TotalUsed>4</TotalUsed>
    <TotalCollectible>0</TotalCollectible>
    <TotalRefurbished>0</TotalRefurbished>
  ",

中古価格の最安値を取得する場合にはAmazon::Ecs.item_search("Ruby", response_group: "OfferSummary", country: "jp").first_item.get("OfferSummary/LowestUsedPrice/FormattedPrice")とする。

  "Offers"=>"
    <TotalOffers>1</TotalOffers>
    <TotalOfferPages>1</TotalOfferPages>
    <MoreOffersUrl>http://www.amazon.co.jp/gp/offer-listing/4797372273%3FSubscriptionId%3DAKIAIXUURNVWELYLTCZQ%26tag%3Dxxxxxxxx-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D5143%26creativeASIN%3D4797372273</MoreOffersUrl>
    <Offer>
      <OfferAttributes>
        <Condition>New</Condition>
      </OfferAttributes>
      <OfferListing>
        <OfferListingId>5wwqsFFJIz89xc%2BftuRir%2BC6d%2B9o1fcKSVS6oA7IddZbBpl6Xvtg1%2Fpo%2Bjpqy0aW3p%2B%2Bjw%2F10SP9C%2FzKmax63NRmLowGq7wHNUDQpPNpKKk%3D</OfferListingId>
        <Price>
          <Amount>2808</Amount>
          <CurrencyCode>JPY</CurrencyCode>
          <FormattedPrice>¥ 2,808</FormattedPrice>
        </Price>
        <Availability>在庫あり。</Availability>
        <AvailabilityAttributes>
          <AvailabilityType>now</AvailabilityType>
          <MinimumHours>0</MinimumHours>
          <MaximumHours>0</MaximumHours>
        </AvailabilityAttributes>
        <IsEligibleForSuperSaverShipping>1</IsEligibleForSuperSaverShipping>
      </OfferListing>
      <LoyaltyPoints>
        <Points>85</Points>
        <TypicalRedemptionValue>
          <Amount>85</Amount>
          <CurrencyCode>JPY</CurrencyCode>
          <FormattedPrice>¥ 85</FormattedPrice>
        </TypicalRedemptionValue>
      </LoyaltyPoints>
    </Offer>
  ",
  "CustomerReviews"=>"
    <IFrameURL>http://www.amazon.jp/reviews/iframe?akid=AKIAIXUURNVWELYLTCZQ&amp;alinkCode=xm2&amp;asin=4797372273&amp;atag=xxxxxxxx-22&amp;exp=2015-03-07T03%3A58%3A24Z&amp;v=2&amp;sig=63GIbEoLdmPsFco0%2FxC84kRuXpgitSTRhtAL2e5hoxg%3D</IFrameURL>
    <HasReviews>true</HasReviews>
  ",
  "SimilarProducts"=>"
    <SimilarProduct>
      <ASIN>4774164100</ASIN>
      <Title>Ruby on Rails 4 アプリケーションプログラミング</Title>
    </SimilarProduct>
    <SimilarProduct>
      <ASIN>4797371277</ASIN>
      <Title>作りながら学ぶRuby入門 第2版</Title>
    </SimilarProduct>
    <SimilarProduct>
      <ASIN>4798040215</ASIN>
      <Title>たった2日でできるRuby―Ruby2.0対応</Title>
    </SimilarProduct>
    <SimilarProduct>
      <ASIN>4797380357</ASIN>
      <Title>Rubyによるクローラー開発技法 巡回・解析機能の実装と21の運用例</Title>
    </SimilarProduct>
    <SimilarProduct>
      <ASIN>4774158798</ASIN>
      <Title>パーフェクトRuby (PERFECT SERIES 6)</Title>
    </SimilarProduct>
  ",
  "BrowseNodes"=>"
    <BrowseNode>
      <BrowseNodeId>3229704051</BrowseNodeId>
      <Name>ソフトウェア開発・言語</Name>
      <Ancestors>
        <BrowseNode>
          <BrowseNodeId>492352</BrowseNodeId>
          <Name>プログラミング</Name>
          <Ancestors>
            <BrowseNode>
              <BrowseNodeId>466298</BrowseNodeId>
              <Name>コンピュータ・IT</Name>
              <Ancestors>
                <BrowseNode>
                  <BrowseNodeId>465610</BrowseNodeId>
                  <Name>ジャンル別</Name>
                  <IsCategoryRoot>1</IsCategoryRoot>
                  <Ancestors>
                    <BrowseNode>
                      <BrowseNodeId>465392</BrowseNodeId>
                      <Name>本</Name>
                    </BrowseNode>
                  </Ancestors>
                </BrowseNode>
              </Ancestors>
            </BrowseNode>
          </Ancestors>
        </BrowseNode>
      </Ancestors>
    </BrowseNode>
    <BrowseNode>
      <BrowseNodeId>12865841</BrowseNodeId>
      <Name>SBクリエイティブ</Name>
      <Ancestors>
        <BrowseNode>
          <BrowseNodeId>465614</BrowseNodeId>
          <Name>By Publishers</Name>
          <Ancestors>
            <BrowseNode>
              <BrowseNodeId>465392</BrowseNodeId>
              <Name>本</Name>
            </BrowseNode>
          </Ancestors>
        </BrowseNode>
      </Ancestors>
    </BrowseNode>
    <BrowseNode>
      <BrowseNodeId>616893011</BrowseNodeId>
      <Name>Custom Stores</Name>
      <Children>
        <BrowseNode>
          <BrowseNodeId>13783651</BrowseNodeId>
          <Name>IDGストア</Name>
        </BrowseNode>
        <BrowseNode>
          <BrowseNodeId>13014911</BrowseNodeId>
          <Name>ソフトバンクの本</Name>
        </BrowseNode>
        <BrowseNode>
          <BrowseNodeId>1198480</BrowseNodeId>
          <Name>光文社ストア</Name>
        </BrowseNode>
        <BrowseNode>
          <BrowseNodeId>10924781</BrowseNodeId>
          <Name>宝島社ストア</Name>
        </BrowseNode>
        <BrowseNode>
          <BrowseNodeId>3370851</BrowseNodeId>
          <Name>日経BP社ストア</Name>
        </BrowseNode>
        <BrowseNode>
          <BrowseNodeId>3078071</BrowseNodeId>
          <Name>講談社ストア</Name>
        </BrowseNode>
        <BrowseNode>
          <BrowseNodeId>3304155051</BrowseNodeId>
          <Name>Kinki U Combined</Name>
        </BrowseNode>
      </Children>
      <Ancestors>
        <BrowseNode>
          <BrowseNodeId>3824511</BrowseNodeId>
          <Name>COOP</Name>
          <Ancestors>
            <BrowseNode>
              <BrowseNodeId>515742</BrowseNodeId>
              <Name>Stores</Name>
              <Ancestors>
                <BrowseNode>
                  <BrowseNodeId>465392</BrowseNodeId>
                  <Name>本</Name>
                </BrowseNode>
              </Ancestors>
            </BrowseNode>
          </Ancestors>
        </BrowseNode>
      </Ancestors>
    </BrowseNode>
    <BrowseNode>
      <BrowseNodeId>15749671</BrowseNodeId>
      <Name>なか見!検索</Name>
      <Ancestors>
        <BrowseNode>
          <BrowseNodeId>515742</BrowseNodeId>
          <Name>Stores</Name>
          <Ancestors>
            <BrowseNode>
              <BrowseNodeId>465392</BrowseNodeId>
              <Name>本</Name>
            </BrowseNode>
          </Ancestors>
        </BrowseNode>
      </Ancestors>
    </BrowseNode>
  "

BrowseNode を指定して検索するには Amazon::Ecs.item_search のキーワード引数 browse_node に BrowseNodeId の文字列を渡す。複数の BrowseNodeId を渡す(browse_node: "466298,492352")ことも可能だが、残念ながら AND 検索になる。 OR 検索ならばお役立ちなのに。

}