Currency conversion using Ruby

Well, I am writing an app that requires currency conversion. I searched and got no free one. Luckily in this website http://www.exchangerate-api.com/ruby-currency-api they had given a paid way to convert currency. Then here http://www.google.com/finance/converter Google provides a strip down website to convert one currency to other (for free) and they seem have no API. So? Mix them. Here is the coude for currency conversion in Ruby which you can use it in Sinatra and Rails project

require 'net/http'

def convert_currency(from_curr = "INR", to_curr = "USD", amount = 1000)
	host = "www.google.com"
	http = Net::HTTP.new(host, 80)
	url = "/finance/converter?a=#{amount}&from=#{from_curr}&to=#{to_curr}"
	response = http.get(url)
	# puts response.body
	result = response.body
	regexp = Regexp.new("(\\d+\\.{0,1}\\d*)\\s+#{to_curr}")
	regexp.match result
	return $1.to_f
end

puts convert_currency("INR", "RON", 1000)

Well, thats it, enjoy!

Advertisements
  1. #1 by Binary Options Bully Review on October 4, 2012 - 11:56 pm

    Nice post. Really informative and useful!

  2. #2 by Frederic on October 30, 2012 - 9:39 am

    Hi there, constantly i used to check blog posts here
    early in the break of day, as i love to find out more and more.

  3. #3 by Marc on March 30, 2013 - 8:53 pm

    There are free options, rubygems.org/gems/exchange for example. You example is not really best practice for currency conversion.

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: