Ruby Bikini - How to Process XML in Ruby

Continuing in the series of Brazilian bikini Web development tutorials, here is an experiment with the Yahoo Search API, Ruby and Brazilian bikinis.

The script uses Ruby to convert the XML from the Yahoo Image Search API into XHTML Strict as shown in the image below:



Ruby Bikini

Please download the attached Ruby file to follow along.

Brazilian Bikini Photo Gallery With Yahoo!, XML and Ruby

Pick up a copy of Hpricot if you don't already have it. If you have gems you should be able to do sudo gem install hpricot.

require 'rubygems'
require 'hpricot'
require 'net/http'

The first part defines a function called show_bikinis and reads the XML passed to it into Hpricot. The function is called at the bottom of the script, so if it doesn't make sense at first read through it all the way:

def show_bikinis(xml) 
  # Parse the XML in the file
  doc = Hpricot.XML(xml)

Then begin creating the output string that will be written to an HTML file:

  # Begin the output file
  output = '<html><head>Ruby Bikini</head>
            <body><h1>Ruby Bikini</h1>'

(The HTML in the attached file is a little different, but this snippet is enough for the tutorial.)

Next, use Hpricot to extract the text from the XML elements created by the Yahoo Search API. The elements that we want to extract from the Yahoo Image Search API XML are <result>, <title>, <url>, <summary>, <thumbnail> and the <url> that is the child of <thumbnail>. For each <result>, extract the text of the desired elements:

  (doc/:result).each do |el|
    title = (el/:title).text
    url =
    summary = (el/:summary).text
    thumbnail = (el/:thumbnail).text
    thumbnail_url = (el/:thumbnail/:url).text

Then insert the elements into some HTML that is appended to the output variable that will be written to a file later. Double quotes are used for the alt text because the <summary> sometimes contains single quotes.

    output += "<a href='#{url}'><img src='#{thumbnail_url}' alt=\"#{summary}\" /></a>"

After the script finishes looping through the XML results, add the closing HTML to the output variable, write the file, and end the show_bikinis function:

output += "</body></html>"

  # write it to a file   
  outfile ="ruby_bikini.html", "w")
  outfile.puts output


Now to the Yahoo API section — you need to enter your own Yahoo API key which you can get for free at the Yahoo Developer site.

The following code builds a POST request to Yahoo Images to fetch 50 results for brazilian bikinis:

url = URI.parse('')
appid = 'your-API-key-goes-here'  # use your Yahoo API key here
query = 'brazilian bikinis'
results = 50

Add the post arguments:

post_args = {
    'appid'=> appid,
    'results' => results,
    'query' => query

Fetch the Brazilian bikini photos from Yahoo Images, making an attempt to catch errors:

    resp, data = Net::HTTP.post_form(url, post_args)
    print "Connection error."

To build your Brazilian bikini photo gallery, feed the Yahoo XML results into the show_bikinis function that was defined at the beginning of the script:

# Run script

The final result should be an HTML bikini photo gallery called ruby_bikini.html.

Ruby Bikini

Other Hpricot and Bikini Resources

rubybikini.rb1.41 KB


Webmaster Tips's picture

Hpricot XML

Thanks for that — I updated the script.

Yahoo Image Search API XML

Yahoo Image Search API XML are result>, title>, url>, summary>, thumbnail> and the url> ... not anymore just capp the first letters in the (doc/:result).each do |el| section ...


Syndicate content