Module: Money::Bank::OpenExchangeRatesLoader

Included in:
HistoricalBank
Defined in:
lib/money/bank/open_exchange_rates_loader.rb

Constant Summary collapse

HIST_URL =
'https://openexchangerates.org/api/historical/'
OER_URL =
'https://openexchangerates.org/api/latest.json'

Instance Method Summary collapse

Instance Method Details

#load_data(date) ⇒ Object

Tries to load data from OpenExchangeRates for the given rate. Won’t do anything if there’s no data available for that date in OpenExchangeRates (short) history.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/money/bank/open_exchange_rates_loader.rb', line 16

def load_data(date)
  rates_source = if date == Date.today
                   OER_URL.dup
                 else
                   HIST_URL + date.strftime('%Y-%m-%d') + '.json'
                 end
  rates_source << "?app_id=#{ENV['OPENEXCHANGERATES_APP_ID']}" if ENV['OPENEXCHANGERATES_APP_ID']
  doc = Yajl::Parser.parse(open(rates_source).read)

  base_currency = doc['base'] || 'USD'

  doc['rates'].each do |currency, rate|
    # Don't use set_rate here, since this method can only be called from
    # get_rate, which already aquired a mutex.
    internal_set_rate(date, base_currency, currency, rate)
  end
end