class Money::Bank::FixerCurrency

VariableExchange bank that handles fetching exchange rates from fixer.io and storing them in the in memory rates store.

Constants

SERVICE_HOST
SERVICE_PATH

Attributes

rates_expiration[R]

@return [Time] Returns the time when the rates expire.

ttl_in_seconds[R]

@return [Integer] Returns the Time To Live (TTL) in seconds.

access_key[RW]

@return [String] Access key from fixer.io allowing access to API

rates[R]

@return [Hash] Stores the currently known rates.

Public Class Methods

new(access_key) click to toggle source
Calls superclass method
# File lib/money/bank/fixer_currency.rb, line 49
def initialize(access_key)
  super()
  @store.extend Money::RatesStore::RateRemovalSupport
  @access_key = access_key
end
refresh_rates_expiration!() click to toggle source

Set the rates expiration TTL seconds from the current time.

@return [Time] The next expiration.

# File lib/money/bank/fixer_currency.rb, line 44
def refresh_rates_expiration!
  @rates_expiration = Time.now + ttl_in_seconds
end
ttl_in_seconds=(value) click to toggle source

Set the Time To Live (TTL) in seconds.

@param [Integer] the seconds between an expiration and another.

# File lib/money/bank/fixer_currency.rb, line 35
def ttl_in_seconds=(value)
  @ttl_in_seconds = value
  refresh_rates_expiration! if ttl_in_seconds
end

Public Instance Methods

expire_rates() click to toggle source

Flushes all the rates if they are expired.

@return [Boolean]

# File lib/money/bank/fixer_currency.rb, line 115
def expire_rates
  if self.class.ttl_in_seconds && self.class.rates_expiration <= Time.now
    flush_rates
    self.class.refresh_rates_expiration!
    true
  else
    false
  end
end
flush_rate(from, to) click to toggle source

Clears the specified rate stored in @rates.

@param [String, Symbol, Currency] from Currency to convert from (used

for key into @rates).

@param [String, Symbol, Currency] to Currency to convert to (used for

key into @rates).

@return [Float] The flushed rate.

@example

@bank = FixerCurrency.new    #=> <Money::Bank::FixerCurrency...>
@bank.get_rate(:USD, :EUR)    #=> 0.776337241
@bank.flush_rate(:USD, :EUR)  #=> 0.776337241
# File lib/money/bank/fixer_currency.rb, line 82
def flush_rate(from, to)
  store.remove_rate(from, to)
end
flush_rates() click to toggle source

Clears all rates stored in @rates

@return [Hash] The empty @rates Hash.

@example

@bank = FixerCurrency.new  #=> <Money::Bank::FixerCurrency...>
@bank.get_rate(:USD, :EUR)  #=> 0.776337241
@bank.flush_rates           #=> {}
# File lib/money/bank/fixer_currency.rb, line 64
def flush_rates
  store.clear_rates
end
get_rate(from, to) click to toggle source

Returns the requested rate.

It also flushes all the rates when and if they are expired.

@param [String, Symbol, Currency] from Currency to convert from @param [String, Symbol, Currency] to Currency to convert to

@return [Float] The requested rate.

@example

@bank = FixerCurrency.new  #=> <Money::Bank::FixerCurrency...>
@bank.get_rate(:USD, :EUR)  #=> 0.776337241
# File lib/money/bank/fixer_currency.rb, line 99
def get_rate(from, to)
  expire_rates

  fetch_rates if !store.get_rate(from, :EUR) || !store.get_rate(to, :EUR)

  begin
    return store.get_rate(from, :EUR) / store.get_rate(to, :EUR)
  rescue
    raise UnknownRate
  end
end

Private Instance Methods

build_uri() click to toggle source

Build a URI for the given arguments.

@return [URI::HTTP]

# File lib/money/bank/fixer_currency.rb, line 139
def build_uri
  URI::HTTP.build(
    host: SERVICE_HOST,
    path: SERVICE_PATH,
    query: "access_key=#{access_key}"
  )
end
extract_rates(data) click to toggle source

Takes the response from fixer.io and extract the rates and adds them to the rates store.

@param [String] data The hash of rates from fixer to decode.

# File lib/money/bank/fixer_currency.rb, line 152
def extract_rates(data)
  rates = JSON.parse(data)['rates']
  rates.each do |currency, rate|
    store.add_rate(currency, :EUR, 1 / BigDecimal(rate.to_s))
  end
rescue
  raise FixerCurrencyFetchError
end
fetch_rates() click to toggle source

Makes an api call to populate all of the exchange rates in the in memory store.

# File lib/money/bank/fixer_currency.rb, line 130
def fetch_rates
  data = build_uri.read
  extract_rates(data)
end