Class: Money::Bank::OpenExchangeRatesBank

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

Constant Summary collapse

OER_URL =
'http://openexchangerates.org/latest.json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_idObject

Returns the value of attribute app_id.



16
17
18
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 16

def app_id
  @app_id
end

#cacheObject

Returns the value of attribute cache.



16
17
18
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 16

def cache
  @cache
end

#docObject (readonly)

Returns the value of attribute doc.



17
18
19
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 17

def doc
  @doc
end

#oer_ratesObject (readonly)

Returns the value of attribute oer_rates.



17
18
19
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 17

def oer_rates
  @oer_rates
end

#rates_expirationObject (readonly)

Returns the value of attribute rates_expiration.



17
18
19
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 17

def rates_expiration
  @rates_expiration
end

#ttl_in_secondsObject

Returns the value of attribute ttl_in_seconds.



17
18
19
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 17

def ttl_in_seconds
  @ttl_in_seconds
end

Instance Method Details

#exchange(cents, from_currency, to_currency) ⇒ Object



43
44
45
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 43

def exchange(cents, from_currency, to_currency)
  exchange_with(Money.new(cents, from_currency), to_currency)
end

#exchange_with(from, to_currency) ⇒ Object



47
48
49
50
51
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 47

def exchange_with(from, to_currency)
  return from if same_currency?(from.currency, to_currency)
  rate = get_rate(from.currency, to_currency)
  Money.new(((Money::Currency.wrap(to_currency).subunit_to_unit.to_f / from.currency.subunit_to_unit.to_f) * from.cents * rate).round, to_currency)
end

#expire_ratesObject



63
64
65
66
67
68
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 63

def expire_rates
  if ttl_in_seconds && rates_expiration <= Time.now
    update_rates
    refresh_rates_expiration
  end
end

#get_rate(from_currency, to_currency) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 53

def get_rate(from_currency, to_currency)
  expire_rates
  super(from_currency, to_currency) || begin
    from_base_rate = super("USD", from_currency)
    to_base_rate = super("USD", to_currency)
    raise(Money::Bank::UnknownRateFormat, "No conversion rate known for '#{from_currency}' -> '#{to_currency}'") if from_base_rate.nil? || to_base_rate.nil?
    to_base_rate.to_f / from_base_rate.to_f
  end
end

#save_ratesObject



33
34
35
36
37
38
39
40
41
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 33

def save_rates
  raise InvalidCache unless cache
  text = read_from_url
  if has_valid_rates?(text)
    store_in_cache(text)
  end
rescue Errno::ENOENT
  raise InvalidCache
end

#update_ratesObject



24
25
26
27
28
29
30
31
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 24

def update_rates
  exchange_rates.each do |exchange_rate|
    rate = exchange_rate.last
    currency = exchange_rate.first
    next unless Money::Currency.find(currency)
    set_rate('USD', currency, rate)
  end
end