Ruby/Amazon & Amazon Web Services 

by Ian MacDonald



Listing One



#!/usr/bin/ruby -w

# Use the Ruby/Amazon library

require 'amazon/search'



# Include the constants, classes and methods from the Amazon::Search module

include Amazon::Search



# Create a new request object, passing in my AWS developer's token, my

# Associates ID, the locale in which I wish to work (the US or amazon.com in

# this case) and the fact that I do not wish to use a results cache.

#

# Only the first parameter is required. The locale defaults to US and a cache

# is used unless specified otherwise. Even the need for the first parameter

# can be avoided by entering it in ~/.amazonrc.

#

r = Request.new( 'D23XFCO2UKJY82', 'calibanorg-20', 'us', false )



# Search for for items related to Ruby programming in the books section of the

# Amazon catalogue. Pass each product found into a block, calling the product

# object p on each iteration.

#

r.keyword_search( 'ruby programming', 'books' ) do |p|

  authors = p.authors.join( ', ' )



  # Information for books that are currently in print or soon to be available.

  unless p.availability =~ /not available/

    printf( "%s by %s (ASIN %s) for %s\n%s\n\n", p.product_name, authors,  p.asin, p.our_price, p.availability )

  end

end





Listing Two



#!/usr/bin/ruby -w

require 'amazon/search'



# Construct an array of search strings and product catalog in which to search.

favourites = [

  [ 'global underground', 'music' ],

  [ 'the divine comedy', 'music' ],

  [ 'only fools horses', 'dvd' ],

  [ 'the office', 'dvd' ],

  [ 'minder', 'dvd' ]

]

q = Amazon::Search::Request.new( 'D23XFCO2UKJY82' )



# I wish to search the British (amazon.co.uk) catalogue.

q.locale = 'uk'

favourites.each do |search_string, category|

  r = q.keyword_search( search_string, category )

  r.products.each do |p|

    if p.availability =~ /not yet released/i

      case p.catalogue

      when 'Music'

        printf( "%s CD by %s\n(ASIN %s)\n\n", p.product_name, p.artists, p.asin

)

      when 'DVD'

        printf( "%s DVD\n(ASIN %s)\n\n", p.product_name, p.asin )

      end

    end

  end

end





Listing Three



#!/usr/bin/ruby -w

require 'amazon/search'

include Amazon::Search



# The iRiver iHP140 jukebox

asin = 'B0001G6UAW'



# Look it up

r = Request.new( 'D23XFCO2UKJY82' )

resp = r.asin_search( asin )



# Quit if we didn't get a response

exit unless resp



new_price = resp.products[0].our_price

begin

  # Read the price from the last check

  cur_price = File::readlines( '/tmp/price' ).to_s.chomp

rescue Errno::ENOENT    # File won't exist the first time we are run

  cur_price = '0'

end



# Quit if there's no price change

exit if new_price == cur_price



# Write new price out to file and stdout

File.open( '/tmp/price', 'w') { |file| file.puts new_price }



printf( "Price of %s has changed from %s to %s\n",

        resp.products[0].product_name, cur_price, new_price )





Listing Four



#!/usr/bin/ruby -w

require 'amazon/shoppingcart'

include Amazon



DEV_TOKEN = 'D23XFCO2UKJY82'

sc = ShoppingCart.new( DEV_TOKEN )



ARGV.each { |asin| sc.add_items( asin, 1 ) }

puts sc.purchase_url



