#!/usr/bin/ruby -w
#
# $Id: search,v 1.5 2004/01/22 09:16:25 ianmacd Exp $

require 'amazon'

include Amazon::Search

DEV_TOKEN     = "D23XFCO2UKJY82"    # your development token

req = Request.new(DEV_TOKEN)

searches = [
  ['ruby programming', 'books', HEAVY, KEYWORD_SEARCH, 'Keyword'],
  ['angelina jolie', 'dvd', HEAVY, ACTOR_SEARCH, 'Actor'],
  ['stranglers', 'music', HEAVY, ARTIST_SEARCH, 'Artist'],
  ['0971394253', nil, HEAVY, ASIN_SEARCH, 'ASIN'],
  ['602360', 'magazines', HEAVY, NODE_SEARCH, 'Browse node'],
  ['iain banks', 'books', HEAVY, AUTHOR_SEARCH, 'Author'],
  ['oliver stone', 'video', HEAVY, DIRECTOR_SEARCH, 'Director'],
  ['red hat', 'software', HEAVY, MANUFACTURER_SEARCH, 'Manufacturer'],
  ['10AHWTZSWG8NC', nil, HEAVY, LISTMANIA_SEARCH, 'Listmania'],
  ['0596005423', nil, HEAVY, SIMILARITY_SEARCH, 'Similarity'],
  ['language:french and keywords:travel and title-begins:Michelin',
   'books', HEAVY, POWER_SEARCH, 'Power'],
  ]

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

  # parse the results of each search
  req.search(*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

