Dobb’s Journal, February 2005
http://www.ddj.com
and #director_search, as well as a slew of search, which returns fewer properties and
hits for a given search query. If you chose
other, more esoteric searches.
is, therefore, theoretically faster to execute.
a generic search, it could be quite some
For example, if you have an Amazon wish-
Use the Amazon::Search::HEAVY and
time before you get back your results.
list, you can look it up with a line like this:
Amazon::Search::LITE constants to speci-
I now leave searches to concentrate on
fy the weight of the search. The weight is
a different area of AWS where Ruby/Ama-
r.wishlist_search( '18Y9QEW3A4SRY' )
either the second or the third argument to
zon can be of assistance.
The parameter to the method call is the
the method, depending on the particular
If you want to build your own online
alphanumeric wishlist ID.
method call.
store, but don’t wish to invest in any stock,
Or, if you want to get more specific and
Another parameter of interest is the
you can use the gigantic Amazon catalog
you’re looking for a book, the power
page parameter. This specifies which page
as your inventory, have Amazon take care
search may be what you need. The fol-
of up to 10 responses to return for the giv-
of the postage, packing, invoicing, and dis-
lowing would find all books published by
en search. If you play around with the
patch, and simply sit back and earn refer-
O’Reilly & Associates in the Amazon cat-
various search methods without supply-
ral fees on any items sold via your site.
alog and sort them by publishing date:
ing a page number, you’ll quickly notice
One way to achieve this is the so-called
that you are never returned more than 10
“remote shopping cart” feature of AWS. This
r.power_search("publisher:o’reilly", 'books', HEAVY, ALL_PAGES, '+daterank')
products for a given search. This is be-
is one of the areas where Ruby/Amazon
cause you’re only looking at the first page
can take a lot of the work out of your
This can take quite some time to return,
of results, since the default page to re-
hands, as the required tracking of shopping
as it will have to look up in excess of 1000
trieve is the first.
cart IDs and security tokens is handled in-
products.
Instead of using the default of 1 (or any
ternally by the objects you create. You won’t
I won’t go into any of the other eso-
other integer), try passing the constant
even know these elements exist, much less
teric searches here. Read the AWS and
Amazon::Search::ALL_PAGES for a search
need to deal with them manually.
Ruby/Amazon documentation, experi-
that you suspect would yield a high num-
Listing Four takes a list of ASINs from
menting as you go, to get a feel for what
ber of hits, such as the power search. This
the command line, and adds each of the
is possible.
constant signals to Ruby/Amazon that it
corresponding products to a new remote
One thing worth noting is that the AWS
should carry out as many background
shopping cart. Finally, it spits out the pur-
search methods can take many more pa-
AWS operations as required to return all
chase URL of the new cart; see Figure 3.
rameters than illustrated by the programs
I present here. For example, I’ve been per-
http://www.amazon.com/exec/obidos/shopping-basket?cart-id=002-1095509-6210461%26associate-id forming “heavy” searches by default, which
=calibanorg-20%26hmac=%2BWlRcRNiMD6KJ9UuUeNPMvqnVDY=
return all the information available for a
given product. You can also specify a “lite”
Figure 3: Output from Listing Four.
http://www.ddj.com
Dr. Dobb’s Journal, February 2005
33
If that URL were subsequently entered into For example, a CGI (or mod_ruby,
example, Irish customers would be local-
a browser, the contents of the remote cart
http://modruby.net/en/) script might con-
ized to the UK site, Swiss customers to the
would be uploaded to the local shopping
tain something like this:
German site, and Canadians would find
cart of the browser’s user.
themselves localized to the U.S. ama-
require 'amazon/locale'
This feature lets you tie your users
require 'amazon/search'
zon.com site.
more closely to your online store by
What else could you do with Ruby/
seamlessly integrating shopping-cart fa-
include Amazon::Search
Amazon? Perhaps you maintain a database
cilities in your own site. This alleviates
of your book collection, just to keep track
the need to link away to Amazon for the
locale =
of everything. Wouldn’t it be nice if you
actual purchase, which would carry with
Amazon::Locale::get_locale_by_addr
could associate each book in your col-
it the risk of the customer continuing his
( ENV['REMOTE_ADDR']
lection with a picture of its cover? Well,
browsing from there, rather than return-
)
one of the properties you can retrieve for
ing to your site to potentially earn you
books in Amazon’s vast catalog is im-