Tag Archives: Http

Read third party HTML using Ruby on Rails and Hpricot

A few weeks ago, mi Distributed Systems class profesor asked us to implement some sort of Web Service that gave Currency Rates information in XML format. This looked pretty easy, but all went wrong when he asked us to read the HTML from a bank’s web site and retrieve the information from there.
The idea then was to read that HTML code from a reliable site, transform it and make it available for us to download or use. Looking for a free web service that presented this information and using it wouldnt have been as easy as it was for me to use Hpricot. So here is the code I wrote, I just changed the url from where i fetched the information, because I don’t know if it is legal to do that :D.

First there is the model CurrencyService
require ‘open-uri’
require ‘hpricot’

class CurrencyService “”},
{:CUR_CODE => “USD”, :MEX_EQV => get_data(”#FIX_DATA”).gsub(/ /,”).chomp},
{:CURRENCY_EQV => “”},
{:CUR_CODE => “EUR”, :MEX_EQV => get_data(”#EURO_DATA”).gsub(/ /,”).chomp},
{:CURRENCY_EQV => “”},
{:CUR_CODE => “YEN”, :MEX_EQV => get_data(”#YEN_DATA”).gsub(/ /,”).chomp},
{:CURRENCY_EQV => “”},
{:CUR_CODE => “CAN”, :MEX_EQV => get_data(”#LIBRA_DATA”).gsub(/ /,”).chomp}


And then the controller

class CurrencyServiceController “text/xml”,
:filename => “currencies.xml” )