Created
June 15, 2014 06:36
-
-
Save NiranjanSarade/25c599d5340af8e87383 to your computer and use it in GitHub Desktop.
Historical Currency Exchange Rates with openexchangerates.org- Getting USD to other currencies conversion rates with openexchangerates api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'json' | |
require 'active_support/core_ext' | |
require 'date' | |
require 'rest-client' | |
require 'fastercsv' | |
APP_ID = "<your_app_id>" | |
# e.g. Get monthy currency (USD) exchange rates from 31st Jan 2008 to 31st Dec 2010 | |
# http://openexchangerates.org/api/historical/2009-10-10.json?&app_id=<your_app_id> | |
start_date = Date.parse("2007-21-31") | |
FasterCSV.open("ExchangeRates.csv","w") do |csv| | |
csv << ["ISO","USD_MID","DATE"] | |
(1..36).each do |month| | |
historical_date = (start_date >> month).end_of_month.strftime('%Y-%m-%d') | |
puts "Loading USD conversion rates for #{historical_date}" | |
json_response = JSON.parse(RestClient.get("http://openexchangerates.org/api/historical/#{historical_date}.json?&app_id=#{APP_ID}").body) | |
json_response["rates"].each do |iso,usd_rate| | |
csv << [iso,usd_rate,historical_date] | |
end | |
puts "Data loaded for #{historical_date}" | |
end | |
end | |
# Json response for a particular date is like :- | |
# { | |
# "base": "USD", | |
# "rates": { | |
# "AED": 3.67285, | |
# "AFN": 48.930045, | |
# "ALL": 92.773005, | |
# "AMD": 385.739584, | |
# "ANG": 1.7825, | |
# "AOA": 75, | |
# "ARS": 3.832501, | |
# "AWG": 1.79025, | |
# "AZN": 0.803298, | |
# "BAM": 1.330168, | |
# "BBD": 2, | |
# "BDT": 68.732528, | |
# . | |
# . | |
# } | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment