Extending Web Services Using Other Web Services
Now that we have created a combined lookup tool, how can we turn it into a Web service? (For the purposes of simplicity, I'm going to use XML-RPC. It would be equally valid to use SOAP or even to look for REST parameters.)
The answer is easier than you might think. We will need to modify the program to take its inputs from the Web instead of ARGV. We also will need to send the output over the XML-RPC, back to the client that sent the original request.
But the end result, as you can see in Listing 3, is not terribly different from what we had in Listing 2. And because it operates as a Web service, we can now incorporate its results into new programs that we might write. Better yet, we can create new Web services that use this service as an underlying foundation, thus stacking the functions even deeper, into even more useful libraries.
Listing 3. xmlrpc-lookup.rb
#!/usr/bin/ruby
require 'net/http'
require 'rexml/document'
require 'xmlrpc/server'
# Set our regular expressions
not_in_collection_re = /class="yourEntryWouldBeHereData"/ix
on_shelf_re = /CHECK\s+SHELF/ix
checked_out_re = /DUE /ix
# ------------------------------------------------------------
# XML-RPC
# ------------------------------------------------------------
# Start an HTTP server on port 8080, to listen for clients
server = XMLRPC::Server.new(8080)
server.add_handler(name="atf.books",
signature=['array', 'array']) do |isbns|
output = [ ]
# Iterate through each of our arguments
isbns.each do |isbn|
isbn_output = {'ISBN' => isbn}
# Ignore non-ISBN arguments
if not isbn.match(/^[0-9xX]{10}$/)
isbn_output['message'] = "ISBN #{isbn} is invalid."
output << isbn_output
next
end
# ------------------------------------------------------------
# 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?
isbn_output['New'] = "None available"
else
isbn_output['New'] = new_price.text
end
used_price =
xml.root.elements["Items/Item/OfferSummary/LowestUsedPrice/FormattedPrice"]
if used_price.nil?
isbn_output['Used'] = "None available"
else
isbn_output['Used'] = used_price.text
end
collectible_price =
xml.root.elements["Items/Item/OfferSummary/LowestCollectiblePrice/FormattedPrice"]
if collectible_price.nil?
isbn_output['Collectible'] = "None available"
else
isbn_output['Collectible'] = collectible_price.text
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)
isbn_output['Library'] = "Library: Not in the Skokie collection."
elsif checked_out_re.match(library_response.body)
isbn_output['Library'] = "Checked out."
elsif on_shelf_re.match(library_response.body)
isbn_output['Library'] = "On the shelf."
else
isbn_output['Library'] = "Unparseable response."
end
output << isbn_output
end
output
end
server.serve
Listing 3 begins by creating a new instance of XMLRPC::Server on port 8080. It then adds a new handler, which we call atf.books, and which both accepts an array as input and returns one as output. Using Ruby's block notation, the handler then iterates over each ISBN that it receives via the XML-RPC method call.
The rest of the program is largely the same as combined-lookup.rb, with the exception of the output. Output to an XML-RPC method call, at least in this Ruby library, is accomplished by placing the output in the final line of the block. Because we plan to return an array, we need to create and populate the array. We thus define output variable as an empty array and add one element to it for each ISBN we check. Each element of that array then will be a hash (known as a struct in XML-RPC jargon), with the ISBN key pointing to the book's ISBN, and the New, Used and Collectible keys pointing to the prices retrieved from Amazon.
The server program then concludes with a call to server.serve, starting an infinite listener loop for a simple HTTP server.
To test this program, you need an RPC client; a simple one is shown in Listing 4 and takes its arguments from the command line. You'll notice that we use Ruby's exception-handling mechanism to watch for potential problems. If there is an error on the server, we can trap it and print a useful debugging message.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Readers' Choice Awards
- Home, My Backup Data Center
- What's the tweeting protocol?
- New Products
- RSS Feeds
- Dart: a New Web Programming Experience
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




7 hours 48 min ago
10 hours 21 min ago
11 hours 38 min ago
12 hours 13 min ago
12 hours 35 min ago
17 hours 24 min ago
18 hours 11 min ago
19 hours 44 min ago
21 hours 21 min ago
23 hours 19 min ago