Extending Web Services Using Other Web Services
The above program works just fine, and it provides an easier way to query the Skokie library catalog than the standard Web pages. But, I'm interested in knowing how much the book would cost if I were to buy it from Amazon, as well as whether it's available from the library. With all of this information, I can then decide if I want to buy the book, check it out of the library or neither.
Last month, we saw how we could use a REST-style request (that is, HTTP GET with arguments) to retrieve information from Amazon. Now we will write a program that performs that retrieval and then pulls out the relevant XML data.
As you might remember, we can retrieve Web services information from Amazon by sending an HTTP request to webservices.amazon.com, asking for the document /onca/xml, and then specifying the Service, Operation and AWSAccessKeyId name-value pairs. If we are interested in learning about new and used prices for that ISBN, we then pass the ItemId parameter, and indicate that we want the ResponseGroup known as OfferSummary.
Because Amazon returns XML in all of its responses, including those invoked with REST, we can parse through the XML to find the lowest prices for our book. Ruby comes with the REXML-parsing library, which works with XML in a number of different ways; we will use it to scan through Amazon's response for the appropriate code.
Finally, we can rework our existing code, such that it will search the Skokie library for the ISBN and produce a textual summary. Listing 2 contains a program (combined-lookup.rb) that produces such combined output.
Listing 2. combined-lookup.rb
#!/usr/bin/ruby
require 'net/http'
require 'rexml/document'
if ARGV.length == 0
puts "#{$0}: You must enter at least one argument."
exit
end
output = ""
# Set up our regular expressions
not_in_collection_re = /class="yourEntryWouldBeHereData"/ix
on_shelf_re = /CHECK\s+SHELF/ix
checked_out_re = /DUE /ix
# Iterate through each of our arguments
ARGV.each do |isbn|
# Ignore non-ISBN arguments
if not isbn.match(/[0-9xX]{10}/)
output << "ISBN #{isbn} is invalid.\n"
next
end
output << "ISBN: #{isbn}\n"
# ------------------------------------------------------------
# Amazon
# ------------------------------------------------------------
# Put together an Amazon parameter string
amazon_params = {'Service' => 'AWSECommerceService',
'Operation' => 'ItemLookup',
'AWSAccessKeyId' => 'XXX',
'ItemId' => isbn,
'ResponseGroup' => 'Medium,OfferFull',
'MerchantId' => 'All'}.map {|key,value| "#{key}=#{value}"}.join("&")
# Ask Amazon what it knows about our ISBN
amazon_response = Net::HTTP.get_response('webservices.amazon.com',
'/onca/xml?' << amazon_params)
xml = REXML::Document.new(amazon_response.body)
# Get the lowest new, used, and collectible prices
new_price =
xml.root.elements["Items/Item/OfferSummary/LowestNewPrice/FormattedPrice"]
if new_price.nil?
output << "\tNew: None available\n"
else
output << "\tNew: #{new_price.text}\n"
end
used_price =
xml.root.elements["Items/Item/OfferSummary/LowestUsedPrice/FormattedPrice"]
if used_price.nil?
output << "\tUsed: None available\n"
else
output << "\tUsed: #{used_price.text}\n"
end
collectible_price =
xml.root.elements["Items/Item/OfferSummary/LowestCollectiblePrice/FormattedPrice"]
if collectible_price.nil?
output << "\tCollectible: None available\n"
else
output << "\tCollectible: #{collectible_price.text}\n"
end
# ------------------------------------------------------------
# Library
# ------------------------------------------------------------
# Ask the library what it knows about our ISBN
library_response = Net::HTTP.get_response('catalog.skokie.lib.il.us',
"/search~S4/i?SEARCH=#{isbn}")
# Check our regular expressions against the HTML response
if not_in_collection_re.match(library_response.body)
output << "\tLibrary: Not in the Skokie collection.\n"
elsif checked_out_re.match(library_response.body)
output << "\tLibrary: Checked out.\n"
elsif on_shelf_re.match(library_response.body)
output << "\tLibrary: On the shelf.\n"
else
output << "\tLibrary: Unparseable response\n"
end
end
# Show everyone what we've learned
puts output
combined-lookup.rb begins in almost the same way as skokie-lookup.rb, although it imports the rexml/document module along with the net/http module. It then iterates through ISBNs that were passed on the command line, ignoring those that don't fit the strict definition.
The main addition to this program begins with the creation of a string named amazon_params. In theory, we could have built this string in a number of different ways, many of them less complicated than the combination of methods I chose. But, I felt that using a hash in this way would make it easier to modify the code later on, even if it requires a bit more time to understand at first.
The basic idea is as follows: we create a hash, in which the keys are the AWS REST parameter names, and the values are the corresponding parameter values. In order to get these parameters into the standard format of param1=value1¶m2=value2, we use map to create an array from the keys and values of the hash. Our array will contain strings, each of which is in the param=value format, joined together with an equal sign. Finally, we use join to combine all of those pairs with & signs between them, producing a string that we assign to amazon_params.
With our parameters in place, we use Net::HTTP.get_response, just as we did before in skokie-lookup.rb. The hostname will be different, and the requested URL on that host will also be quite different, incorporating the parameters that we just assigned to amazon_params. But, the request is sent in the same way, and we retrieve the response in the same way as well.
However, whereas the Skokie library sends its response in HTML, Amazon replies using XML. So, we fire up REXML, creating a new instance of REXML::Document with the contents of the Amazon response. We then use the elements method on the response's root node to find the lowest new, used and collectible prices. (Amazon provides each of these prices separately, which I admit is a bit annoying.) If the text within that node is nil, no such price exists, and we indicate that to the user. Otherwise, we can assume we got a price back—and a price formatted with a dollar sign and decimal point, at that—and we display it for the user.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- UX Designer
- Senior Perl Developer
- Technical Support Rep
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- RSS Feeds
- Using an SMS Server to Provide a Robust Alerting Service for Nagios
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




9 hours 9 min ago
19 hours 50 min ago
1 day 1 hour ago
1 day 1 hour ago
1 day 3 hours ago
1 day 5 hours ago
1 day 12 hours ago
1 day 12 hours ago
1 day 14 hours ago
1 day 20 hours ago