Class: OXR::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/oxr/configuration.rb

Overview

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.

Constant Summary collapse

ENDPOINT =
'https://openexchangerates.org/api/'
LATEST =
URI.join(ENDPOINT, 'latest.json').freeze
HISTORICAL =
URI.join(ENDPOINT, 'historical/').freeze
USAGE =
URI.join(ENDPOINT, 'usage.json').freeze
CURRENCIES =
URI.join(ENDPOINT, 'currencies.json').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



43
44
45
# File 'lib/oxr/configuration.rb', line 43

def initialize
  reset_sources
end

Instance Attribute Details

#app_idObject

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



29
30
31
# File 'lib/oxr/configuration.rb', line 29

def app_id
  @app_id
end

#baseObject

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



33
34
35
# File 'lib/oxr/configuration.rb', line 33

def base
  @base
end

#currenciesObject

Returns the endpoint for listing known currencies.



49
50
51
# File 'lib/oxr/configuration.rb', line 49

def currencies
  @currencies || append_query(CURRENCIES)
end

#historical(date) ⇒ Object

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

Expects date to respond #strftime.



58
59
60
# File 'lib/oxr/configuration.rb', line 58

def historical(date)
  @historical || append_query(URI.join(HISTORICAL, "#{date.strftime('%F')}.json"), base: base)
end

#latestObject

Returns the endpoint for the latest currency exchange rates.



64
65
66
# File 'lib/oxr/configuration.rb', line 64

def latest
  @latest || append_query(LATEST, base: base)
end

#usageObject

Returns the endpoint for fetch current API usage statistics.



70
71
72
# File 'lib/oxr/configuration.rb', line 70

def usage
  @usage || append_query(USAGE)
end

Instance Method Details

#reset_sourcesObject

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



76
77
78
79
80
81
# File 'lib/oxr/configuration.rb', line 76

def reset_sources
  @currencies = nil
  @historical = nil
  @latest     = nil
  @usage      = nil
end