Program Yahoo! with Ruby

Use a Yahoo! Ruby library to include Yahoo! data in your Ruby scripts.

The Yahoo! Developer Network web site (http://developer.yahoo.net) provides a number of tools to help developers build applications with Yahoo! data. Their Yahoo! Search Web Services software development kit (SDK) includes libraries for various programming languages; Ruby, unfortunately, is not one of them. However, I have developed a Ruby library, which is available at http://premshree.seacrow.com/code/ruby/yahoo-ruby.

The library is easy to use; there’s no “installation” as such. Just copy the contents and place it in any suitable directory. The library is just one file, yahoo-ruby.rb, which you need to place in the lib/ruby/site_ruby directory of your Ruby installation. Once it is in place, whenever you want to use the Ruby API for Yahoo! Search Web Services, simply include require yahoo-ruby in the script. Using this Ruby library means you’ll never have to know how to construct Yahoo! Search Web Services URLs, or even know what the XML looks like. The library handles all of the work of communicating with Yahoo!’s server.

The Code

This simple Ruby script uses the Ruby library to return Yahoo! Web Search responses. Save this code to a file called yahoo_search.rb and be sure to add your own unique application ID:

#!/usr/bin/ruby
# yahoo_search.rb
# A simple Yahoo! search script using the
# Ruby API (http://premshree.seacrow.com/code/ruby/yahoo-ruby)
# Usage ruby yahoo_search.rb <query>

# include the yahoo-ruby API
require 'yahoo-ruby'

# get the query parameter
query = ARGV[0]? ARGV[0] : exit

##
# create a web search object:
# Arguments:
# 1. App ID (You can get one at http://developer.yahoo.net)
# 2. The query
# 3. type can be one of: 'all', 'any' or 'phrase'
# 4. The no. of results
##
obj = WebSearch.new('insert app ID', query, 'all', 3)

# store the results -- returns an array of hashes
results = obj.parse_results

# now loop over each item in results, and display the title, summary and URL
results.each { |result|
		print "Title:\t#{result['Title']}\n"
		print "Summary:\t#{result['Summary']}\n"
		print "URL:\t#{result['Url']}\n"
		print "=====================================\n\n"

}

The first thing this script looks for is the yahoo-ruby API. Then the script looks for the query argument; if an argument is not present, the script exits. After fetching the query from the command line, the script creates a WebSearch object and then calls the parse_results function to get the results as an array of hashes.

To use a different Yahoo! Search service—Image search, for example—you need to create a different object.

Running the Hack

Run the script on the command line:

               ruby yahoo_search.rb insert query
               

So a search for the term ruby would look like this:

               
                  ruby yahoo_search.rb ruby

Figure 4-11. Yahoo! Search results for “ruby”

shows the results of the search.

Figure 4-11. shows the results of the search.

As you can see, using the yahoo-ruby library means you can integrate Yahoo! data within your Ruby applications with a few lines of code.

Premshree Pillai

Get Yahoo! Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.