Skip to content

Instantly share code, notes, and snippets.

@NiranjanSarade
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NiranjanSarade/2af5f396ede088b89e74 to your computer and use it in GitHub Desktop.
Save NiranjanSarade/2af5f396ede088b89e74 to your computer and use it in GitHub Desktop.
Historical Exchange Rates with exchange-rates.org - Getting USD to any currency(e.g. INR) historical rates
require 'rubygems'
require 'active_support/core_ext'
require 'date'
require 'rest-client'
# e.g. Get monthy exchange rates - USD to INR from 2008 to 2013
# http://www.exchange-rates.org/Rate/USD/INR/06-13-2014
start_date = Date.parse("2007-21-31")
(1..72).each do |month|
historical_date = (start_date >> month).end_of_month.strftime('%m-%d-%Y')
response = RestClient.get("http://www.exchange-rates.org/Rate/USD/INR/#{historical_date}")
puts response.match(/1 USD = [0-9]*.[0-9]* INR on [0-9]*\/[0-9]*\/[0-9]*/)
end
# The above was a quick one. For more precise regular expression, please follow http://www.regular-expressions.info/dates.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment