Module: ExchangeRate

Defined in:
lib/exchange_rate.rb,
lib/exchange_rate/version.rb,
lib/exchange_rate/rate_sources.rb,
lib/exchange_rate/configuration.rb,
lib/exchange_rate/currency_rate.rb,
lib/exchange_rate/module_methods.rb,
lib/exchange_rate/currency_converter.rb,
lib/exchange_rate/missing_rate_error.rb,
lib/exchange_rate/database_connection.rb,
lib/exchange_rate/retrieval_failed_error.rb,
lib/exchange_rate/rate_sources/e_c_b_rate_retriever.rb

Overview

Module Methods for ExchangeRate.

– These methods are here because the configuration methods are needed in other classes which are required above the ExchangeRate module definition. It felt cleaner to move the module methods here than shift the module definition above the require statements.

Note: this defines methods on the module rather than providing a mixin because the latter still required the mixin to be included in the module of any class which referenced it. That might be cleaner in the long run. ++

Defined Under Namespace

Modules: RateSources Classes: Configuration, CurrencyConverter, CurrencyRate, DatabaseConnection, MissingRateError, RetrievalFailedError

Constant Summary collapse

VERSION =

The module version. Uses Semantic Versioning

'1.0.0'

Class Method Summary collapse

Class Method Details

.at(date_of_rate, from_currency, to_currency) ⇒ Object

Converts from from_currency to to_currency using the FX rate valid on the date_of_rate.

date_of_rate - The Date to use for conversion from_currency - The source currency code String from_currency - The target currency code String

Examples

ExchangeRate.at(Date.today,'GBP','USD')
# => 0.12883019198912e1

Returns the value of 1 unit of from_currency in to_currency.

Raises ExchangeRate::MissingRateError if the local cache cannot be accessed or the value has not been cached.



48
49
50
51
52
# File 'lib/exchange_rate/module_methods.rb', line 48

def self.at(date_of_rate, from_currency, to_currency)
  ExchangeRate::CurrencyConverter.new(date_of_rate, from_currency, to_currency).convert!
rescue StandardError
  raise ExchangeRate::MissingRateError
end

.configurationObject

The configuration object used by this module.

See ExchangeRate::Configuration.

Returns a singleton ExchangeRate::Configuration global configuration object.



60
61
62
# File 'lib/exchange_rate/module_methods.rb', line 60

def self.configuration
  @configuration ||= Configuration.new
end

.configuration=(configuration) ⇒ Object

Set the configuration object used by this module.

See ExchangeRate::Configuration.



68
69
70
# File 'lib/exchange_rate/module_methods.rb', line 68

def self.configuration=(configuration)
  @configuration = configuration
end

.configure {|configuration| ... } ⇒ Object

Configures the ExchangeRate module with the provided block.

See ExchangeRate::Configuration.

Examples

ExchangeRate.configure do |configuration|
  configuration.rate_retriever = CustomRateRetriever.new
end

Returns nothing

Yields:



84
85
86
# File 'lib/exchange_rate/module_methods.rb', line 84

def self.configure
  yield(configuration)
end

.retrieveObject

Retrieve the remote FX rate feed and cache locally.

Returns nothing.

Raises ExchangeRate::RetrievalFailedError if the retrieval fails or the cache cannot be updated.



22
23
24
25
26
27
28
29
30
# File 'lib/exchange_rate/module_methods.rb', line 22

def self.retrieve
  configuration.rate_retriever.save!
  nil
# We expect all rate retrievers to be nice and return ExchangeRate::RetrievalFailed
# but we'll catch everything here to safeguard against custom providers' errors
# bubbling up to the caller
rescue StandardError
  raise ExchangeRate::RetrievalFailedError
end