Class: APIClients::CurrencyAPI

Inherits:
APIClient show all
Defined in:
lib/ynab_convert/api_clients/currency_api.rb

Overview

Client for currency-api (github.com/fawazahmed0/currency-api#readme)

Constant Summary collapse

MISSING_DAYS =

The days that are missing from the API’s otherwise normally available range

{ '2021-09-14' => true, '2022-05-01' => true }.freeze

Instance Method Summary collapse

Constructor Details

#initializeCurrencyAPI

Returns a new instance of CurrencyAPI.



13
14
15
16
17
18
19
20
21
# File 'lib/ynab_convert/api_clients/currency_api.rb', line 13

def initialize
  api_base_path = 'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/'
  @available_date_range = {
    min: Date.parse('2020-11-22'),
    max: Date.today - 1 # yesterday
  }

  super(api_base_path: api_base_path)
end

Instance Method Details

#historical(base_currency:, date:) ⇒ Hash<Symbol, Numeric>

Returns The rates for that day in base_currency.

Parameters:

  • base_currency (Symbol)

    ISO symbol for base currency

  • date (Date, String)

    The date on which to get the rates for

Returns:

  • (Hash<Symbol, Numeric>)

    The rates for that day in base_currency



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ynab_convert/api_clients/currency_api.rb', line 26

def historical(base_currency:, date:)
  parsed_date = date.is_a?(Date) ? date : Date.parse(date)
  handle_date_out_of_bounds(parsed_date) if out_of_bounds?(parsed_date)
  # Some days are missing from the API, use the previous day's rate if
  # a missing day is requested
  parsed_date -= 1 if missing_day?(date)
  currency = base_currency.downcase
  endpoint = "#{parsed_date}/currencies/#{currency}.min.json"
  rates = make_request(endpoint: endpoint)

  rates[currency]
end