module ExchangeRate

Retrieve and cache historical foreign exchange rates.

ExchangeRate uses a cache for FX rates so you are not dependent on a constant connection to your FX rate provider of choice, and allow custom FX rate providers to be added with ease.

Examples

# Retrieve the latest feed data & update the cache
ExchangeRate.retrieve

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

Module Methods for ExchangeRate.

Constants

VERSION

The module version. Uses Semantic Versioning

Public Class Methods

at(date_of_rate, from_currency, to_currency) click to toggle source

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.

# 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
configuration() click to toggle source

The configuration object used by this module.

See ExchangeRate::Configuration.

Returns a singleton ExchangeRate::Configuration global configuration object.

# File lib/exchange_rate/module_methods.rb, line 60
def self.configuration
  @configuration ||= Configuration.new
end
configuration=(configuration) click to toggle source

Set the configuration object used by this module.

See ExchangeRate::Configuration.

# File lib/exchange_rate/module_methods.rb, line 68
def self.configuration=(configuration)
  @configuration = configuration
end
configure() { |configuration| ... } click to toggle source

Configures the ExchangeRate module with the provided block.

See ExchangeRate::Configuration.

Examples

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

Returns nothing

# File lib/exchange_rate/module_methods.rb, line 84
def self.configure
  yield(configuration)
end
retrieve() click to toggle source

Retrieve the remote FX rate feed and cache locally.

Returns nothing.

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

# 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