Class: OmniExchange::Xe

Inherits:
Provider show all
Defined in:
lib/omni_exchange/providers/xe.rb

Constant Summary collapse

ENDPOINT_URL =
'https://xecd-api-ssl.xe.com/'

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 xe.com'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 xe.com'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
28
29
# File 'lib/omni_exchange/providers/xe.rb', line 19

def get_exchange_rate(base_currency:, target_currency:)
  body = api_get do |req|
    req.url 'v1/convert_from.json'

    req.params['from'] = base_currency
    req.params['to'] = target_currency
    req.params['amount'] = 1
  end

  body[:to][0][:mid].to_d
end

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

This method returns the historic exchange rate for multiple currencies for a given date.

Parameters:

  • base_currency: (String)

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

  • target_currencies: (Array)

    an array of ISO Currency Codes of the currencies that you’re exchanging to. ie. [“EUR”, “KRW”]

  • date: (Date)

    the date for which you want the historic exchange rate.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/omni_exchange/providers/xe.rb', line 38

def get_historic_rate(base_currency:, target_currencies:, date:)
  body = api_get do |req|
    req.url 'v1/historic_rate.json'

    req.params['from'] = base_currency
    req.params['to'] = target_currencies.join(',')
    req.params['amount'] = 1
    req.params['date'] = date.strftime('%Y-%m-%d')
  end

  rates = {}
  body[:to].each do |rate|
    rates[rate[:quotecurrency]] = rate[:mid].to_d
  end

  rates
end