#!/usr/bin/ruby -w
#
# $Id: example1,v 1.1 2008/03/24 16:08:37 ianmacd Exp $

require 'amazon/aws'
require 'amazon/aws/search'

# We don't want to have to fully qualify identifiers.
#
include Amazon::AWS
include Amazon::AWS::Search

# If you don't have one of these, don't pass the second argument to
# Request.new.
#
ASSOCIATES_ID = "webservices-20"

# Your access key ID.
#
KEY_ID        = "0Y44V8FAFNM119C6PTR2"

request  = Request.new( KEY_ID, ASSOCIATES_ID )

# Create an item search object.
#
# is = ItemSearch.new( 'Books', { 'Keywords' => 'ruby programming' } )
is = ItemSearch.new( 'Books', { 'Title' => 'ruby programming' } )

# Create a response group object. Examples of response groups are 'Small',
# 'Medium' and 'Large'. 'Large' returns all data about an item.
#
rg = ResponseGroup.new( 'Large' )

# Search for the items, passing the result into a block.
#
response = request.search( is, rg, :ALL_PAGES ) do |resp|
  resp.item_sets.items do |item|
    printf( "Found a product:\n%s---\n", item )
  end
end

printf( "Search had unique request ID %s.\n", response.operation_request.request_id )

# You don't have to access the items through a block.
#
items = response.item_sets.items
printf( "Search returned %d items.\n", items.size )

# The first item in the list.
#
product1 = items[0]
puts "Properties available for the first product returned:",
  product1.properties

# There are three ways to retrieve the property of a product:
#
p product1.asin		  # instance variable
p product1.title
p product1[:author]       # a variation on the Hash theme
p product1['list_price']  # feels more like a Hash
