Concurrency Gem Historical Rate

117 Views Asked by At

Is it possible to get an historical exchange rate using the Concurrency Gem in Ruby on Rails? The current documentation only describe converting as at this moment: Concurrency Gem but it also uses data from Currency Converter API which has historical data available.

The current way of requesting a current exchange rate:

Concurrency.conversion_rate("NZD", "INR")
1

There are 1 best solutions below

1
hendrixfan On

No, the Concurrency Gem doesn't implement the APIs historical data. It only sets q and not date/endDate as described in the Currency Converter API.

concurrency.rb:

url = "https://free.currencyconverterapi.com/api/v6/convert?q=#{from}_#{to}&compact=ultra&apiKey=#{Concurrency.configuration.api_key}"

You could use Money Historical Bank instead. Here you can use a Timestamp:

require 'money/bank/historical_bank'
mh = Money::Bank::HistoricalBank.new

# Exchanges 1000 EUR to USD using Date.today (default if no date has been entered).
# Will download today's rates if none have been entered
mh.exchange_with(1000.to_money('EUR'), 'USD')

# Exchanges 1000 EUR to USD using historical rates
date = Date.new(2009,9,9)
mh.set_rate(date, 'USD', 'EUR', 0.7634)
mh.exchange_with(date, 1000.to_money('USD'), 'EUR') # => 763.4 EUR

Money.default_bank = mh