Class: OmniExchange::OpenExchangeRates

Inherits:
Provider
  • Object
show all
Defined in:
lib/omni_exchange/providers/open_exchange_rates.rb

Constant Summary collapse

ENDPOINT_URL =
'https://openexchangerates.org/api/'

Class Method Summary collapse

Methods inherited from Provider

all, get_currency_unit, load_provider, register_provider

Class Method Details

.get_exchange_rate(base_currency:, target_currency:) ⇒ Object

This method returns the exchange rate, the rate at which the smallest unit of one currency (the base currency)

will be exchanged for another currency (the target currency), from Open Exchange Rate's API.
This method is called in the OmniExchange.exchange_currency method.

@ return [BigDecimal] an exchange rate is returned as a BigDecimal for precise calculation since this exchange

rate will be used to calculate an convert an exchange of currencies. However, an exception will be raised
if there is a timeout while connecting to xe.com or a timeout while reading Open Exchange Rate's API.

Parameters:

  • base_currency: (String)

    the ISO Currency Code of the currency that you’re exchanging from. ie. “USD”, “JPY”

  • target_currency: (String)

    the ISO Currency Code of the currency that you’re exchanging to. ie. “EUR”, “KRW”



19
20
21
22
23
24
25
26
27
# File 'lib/omni_exchange/providers/open_exchange_rates.rb', line 19

def get_exchange_rate(base_currency:, target_currency:)
  body = api_get do |req|
    req.url 'latest.json'
    req.params['base'] = base_currency
    req.params['symbols'] = target_currency
  end

  body['rates'][target_currency].to_d
end

.get_historic_rate(base_currency:, target_currencies:, date:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/omni_exchange/providers/open_exchange_rates.rb', line 29

def get_historic_rate(base_currency:, target_currencies:, date:)
  body = api_get do |req|
    req.url "historical/#{date.strftime('%Y-%m-%d')}.json"

    req.params['base'] = base_currency
    req.params['symbols'] = target_currencies.join(',')
  end

  body['rates']
end