class OXR::Configuration

A container for OXR configuration options.

Stores the API application ID and the base currency for which to request exchange rates.

Also stores the URI endpoints for fetching currencies, the latest exchange ranges, historical exchange rates, and current account usage statistics.

By default, endpoints are generated dynamically, allowing them to automatically adapte to changes to the app_id, but they may be set to a fixed path, including a local file path. This is useful during testing when you might want deterministic results and do not want to waste real API requests.

Constants

CURRENCIES
ENDPOINT
HISTORICAL
LATEST
USAGE

Attributes

app_id[RW]

Get and set the application ID that will be sent to the API server.

base[RW]

Get and set the base currency to be used when fetching exchange rates.

currencies[W]

Set respective API endpoints. Use these if you want to sidestep the API server, e.g., for testing. The endpoint may be provided as a URI, Pathname, or String.

Setting an endpoint to nil will restore the default value.

historical[W]

Set respective API endpoints. Use these if you want to sidestep the API server, e.g., for testing. The endpoint may be provided as a URI, Pathname, or String.

Setting an endpoint to nil will restore the default value.

latest[W]

Set respective API endpoints. Use these if you want to sidestep the API server, e.g., for testing. The endpoint may be provided as a URI, Pathname, or String.

Setting an endpoint to nil will restore the default value.

usage[W]

Set respective API endpoints. Use these if you want to sidestep the API server, e.g., for testing. The endpoint may be provided as a URI, Pathname, or String.

Setting an endpoint to nil will restore the default value.

Public Class Methods

new() click to toggle source
# File lib/oxr/configuration.rb, line 43
def initialize
  reset_sources
end

Public Instance Methods

currencies() click to toggle source

Returns the endpoint for listing known currencies.

# File lib/oxr/configuration.rb, line 49
def currencies
  @currencies || append_query(CURRENCIES)
end
historical(date) click to toggle source

Returns the endpoint for historical currency exchange rates on the given date.

Expects date to respond strftime.

# File lib/oxr/configuration.rb, line 58
def historical(date)
  @historical || append_query(URI.join(HISTORICAL, "#{date.strftime('%F')}.json"), base: base)
end
latest() click to toggle source

Returns the endpoint for the latest currency exchange rates.

# File lib/oxr/configuration.rb, line 64
def latest
  @latest || append_query(LATEST, base: base)
end
reset_sources() click to toggle source

Resets all API endpoints back to their default (dynamic generation).

# File lib/oxr/configuration.rb, line 76
def reset_sources
  @currencies = nil
  @historical = nil
  @latest     = nil
  @usage      = nil
end
usage() click to toggle source

Returns the endpoint for fetch current API usage statistics.

# File lib/oxr/configuration.rb, line 70
def usage
  @usage || append_query(USAGE)
end

Private Instance Methods

append_query(uri, base: nil) click to toggle source
# File lib/oxr/configuration.rb, line 85
def append_query(uri, base: nil)
  uri.dup.tap do |u|
    u.query = "app_id=#{app_id}"
    u.query += "&base=#{base}" if base
  end.to_s
end