#!/usr/bin/ruby -w
#
# $Id: search,v 1.6 2004/01/29 09:26:00 ianmacd Exp $

require 'amazon'

include Amazon::Search

DEV_TOKEN     = "D23XFCO2UKJY82"    # your development token

req = Request.new(DEV_TOKEN)

searches = [
  ['keyword_search', 'ruby programming', 'books', HEAVY, 'Keyword'],
  ['actor_search', 'angelina jolie', 'dvd', HEAVY, 'Actor'],
  ['artist_search', 'stranglers', 'music', HEAVY, 'Artist'],
  ['asin_search', '0971394253', HEAVY, 'ASIN'],
  ['upc_search', '071331704462', 'electronics', HEAVY, 'UPC'],
  ['node_search', '602360', 'magazines', HEAVY, 'Browse node'],
  ['author_search', 'iain banks', 'books', HEAVY, 'Author'],
  ['director_search', 'oliver stone', 'vhs', HEAVY, 'Director'],
  ['manufacturer_search', 'red hat', 'software', HEAVY, 'Manufacturer'],
  ['listmania_search', '10AHWTZSWG8NC', HEAVY, 'Listmania'],
  ['similarity_search', '0596005423', HEAVY, 'Similarity'],
  ['power_search',
   'language:french and keywords:travel and title-begins:Michelin',
   'books', HEAVY, 'Power'],
  ]

searches.each do |s|
  printf(%Q(\nHit [Enter] for %s search on "%s"%s:\n\n), s.pop, s[1],
	 (s[2].kind_of?(String) ? " using %s mode" % s[2] : ''))
  $stdin.gets

  # parse the results of each search
  req.send(*s).parse.each do |prod|

    # get a list of each product's attributes
    vars = prod.instance_variables.sort

    # put the product name at the front of the list
    vars.unshift(vars.delete("@productname"))

    # display the product's details
    vars.each do |iv|
      next if iv =~ /url/ || iv == "@reviews"
      label = iv[1..-1]
      value = prod.instance_variable_get(iv)
      value = value.join(", ") if value.is_a? Array
      printf("%-20s = %s\n", label, value)
    end

    puts
  end
end

