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😀.

First there is the model CurrencyService
currency_service.rb
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}
}.to_xml
end

end

And then the controller

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

end

Enjoy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: