Class: Money::Bank::FixerCurrency

Inherits:
VariableExchange
  • Object
show all
Defined in:
lib/money/bank/fixer_currency.rb

Overview

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

Constant Summary collapse

SERVICE_HOST =
'data.fixer.io'.freeze
SERVICE_PATH =
'/api/latest'.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key) ⇒ FixerCurrency

Returns a new instance of FixerCurrency.



49
50
51
52
53
# File 'lib/money/bank/fixer_currency.rb', line 49

def initialize(access_key)
  super()
  @store.extend Money::RatesStore::RateRemovalSupport
  @access_key = access_key
end

Class Attribute Details

.rates_expirationTime (readonly)

Returns the time when the rates expire.

Returns:

  • (Time)

    Returns the time when the rates expire.



29
30
31
# File 'lib/money/bank/fixer_currency.rb', line 29

def rates_expiration
  @rates_expiration
end

.ttl_in_secondsInteger

Returns the Time To Live (TTL) in seconds.

Returns:

  • (Integer)

    Returns the Time To Live (TTL) in seconds.



26
27
28
# File 'lib/money/bank/fixer_currency.rb', line 26

def ttl_in_seconds
  @ttl_in_seconds
end

Instance Attribute Details

#access_keyString

Returns Access key from fixer.io allowing access to API.

Returns:

  • (String)

    Access key from fixer.io allowing access to API



22
23
24
# File 'lib/money/bank/fixer_currency.rb', line 22

def access_key
  @access_key
end

#ratesHash (readonly)

Returns Stores the currently known rates.

Returns:

  • (Hash)

    Stores the currently known rates.



19
20
21
# File 'lib/money/bank/fixer_currency.rb', line 19

def rates
  @rates
end

Class Method Details

.refresh_rates_expiration!Time

Set the rates expiration TTL seconds from the current time.

Returns:

  • (Time)

    The next expiration.



44
45
46
# File 'lib/money/bank/fixer_currency.rb', line 44

def refresh_rates_expiration!
  @rates_expiration = Time.now + ttl_in_seconds
end

Instance Method Details

#expire_ratesBoolean

Flushes all the rates if they are expired.

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
# 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) ⇒ Float

Clears the specified rate stored in @rates.

Examples:

@bank = FixerCurrency.new    #=> <Money::Bank::FixerCurrency...>
@bank.get_rate(:USD, :EUR)    #=> 0.776337241
@bank.flush_rate(:USD, :EUR)  #=> 0.776337241

Parameters:

  • from (String, Symbol, Currency)

    Currency to convert from (used for key into @rates).

  • to (String, Symbol, Currency)

    Currency to convert to (used for key into @rates).

Returns:

  • (Float)

    The flushed rate.



82
83
84
# File 'lib/money/bank/fixer_currency.rb', line 82

def flush_rate(from, to)
  store.remove_rate(from, to)
end

#flush_ratesHash

Clears all rates stored in @rates

Examples:

@bank = FixerCurrency.new  #=> <Money::Bank::FixerCurrency...>
@bank.get_rate(:USD, :EUR)  #=> 0.776337241
@bank.flush_rates           #=> {}

Returns:

  • (Hash)

    The empty @rates Hash.



64
65
66
# File 'lib/money/bank/fixer_currency.rb', line 64

def flush_rates
  store.clear_rates
end

#get_rate(from, to) ⇒ Float

Returns the requested rate.

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

Examples:

@bank = FixerCurrency.new  #=> <Money::Bank::FixerCurrency...>
@bank.get_rate(:USD, :EUR)  #=> 0.776337241

Parameters:

  • from (String, Symbol, Currency)

    Currency to convert from

  • to (String, Symbol, Currency)

    Currency to convert to

Returns:

  • (Float)

    The requested rate.



99
100
101
102
103
104
105
106
107
108
109
# 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