Skip to content

Instantly share code, notes, and snippets.

@xxswingxx
Last active August 29, 2015 14:12
Show Gist options
  • Save xxswingxx/f941848f0834c4f8c8ac to your computer and use it in GitHub Desktop.
Save xxswingxx/f941848f0834c4f8c8ac to your computer and use it in GitHub Desktop.
get_average_historical.rb
require 'csv'
require 'httparty'
require 'byebug'
OANDA_URL = 'https://www.oanda.com/rates/api/v1/rates/'
OANDA_KEY = ENV['OANDA_API_KEY']
OUTPUT_FILE = './rates.json'
year = {
month_1: [],
month_2: [],
month_3: [],
month_4: [],
month_5: [],
month_6: [],
month_7: [],
month_8: [],
month_9: [],
month_10: [],
month_11: [],
month_12: []
}
average_rates = {
month_1: {},
month_2: {},
month_3: {},
month_4: {},
month_5: {},
month_6: {},
month_7: {},
month_8: {},
month_9: {},
month_10: {},
month_11: {},
month_12: {}
}
csv = CSV.parse( File.open('./exchanges.csv') { |f| f.read } )
csv.each do |exchange|
year["month_#{exchange[2]}".to_sym] << exchange[0..1]
end
iteration = 0
year.each do |month, exchanges|
exchanges.each do |exchange|
month_number = month.to_s.gsub('month_', '').to_i
start_date = Date.new(2014, month_number, 1).to_s
end_date = month_number == 12 ? (Date.today - 1).to_s : Date.civil(2014, month_number, -1).to_s
response = HTTParty.get("#{OANDA_URL}#{exchange[0]}.json?quote=#{exchange[1]}&start=#{start_date}&end=#{end_date}&api_key=#{OANDA_KEY}").parsed_response
iteration += 1
key = "#{exchange[0]}_to_#{exchange[1]}"
puts "#{iteration} - #{key}"
average_rates[month][key.to_sym] = response['quotes'][exchange[1]]['ask'].to_f
end
end
output = File.open(OUTPUT_FILE, 'w+') { |f| f.write average_rates.to_json }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment