Class: Money::Bank::OpenExchangeRatesBank

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

Overview

OpenExchangeRatesBank base class

Constant Summary collapse

VERSION =
::OpenExchangeRatesBank::VERSION
OER_URL =

OpenExchangeRates urls

'http://openexchangerates.org/latest.json'
OER_HISTORICAL_URL =
'http://openexchangerates.org/historical/%s.json'
SECURE_OER_URL =

OpenExchangeRates secure url

OER_URL.gsub('http:', 'https:')
SECURE_OER_HISTORICAL_URL =
OER_URL.gsub('http:', 'https:')

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_idObject

As of the end of August 2012 all requests to the Open Exchange Rates API must have a valid app_id



32
33
34
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 32

def app_id
  @app_id
end

#cacheObject

Cache accessor, can be a String or a Proc



35
36
37
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 35

def cache
  @cache
end

#dateObject



39
40
41
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 39

def date
  @date
end

#oer_ratesObject (readonly)

Parsed OpenExchangeRates result as Hash



45
46
47
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 45

def oer_rates
  @oer_rates
end

#rates_expirationObject (readonly)

Rates expiration Time



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

def rates_expiration
  @rates_expiration
end

#secure_connectionObject

use https to fetch rates from Open Exchange Rates disabled by default to support free-tier users



28
29
30
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 28

def secure_connection
  @secure_connection
end

#ttl_in_secondsObject

Seconds after than the current rates are automatically expired



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

def ttl_in_seconds
  @ttl_in_seconds
end

Instance Method Details

#expire_ratesObject

Expire rates when expired



100
101
102
103
104
105
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 100

def expire_rates
  return unless ttl_in_seconds
  return if rates_expiration > Time.now
  update_rates
  refresh_rates_expiration
end

#get_rate(from_currency, to_currency, opts = {}) ⇒ Numeric

Override Money ‘get_rate` method for caching

Parameters:

  • from_currency (String)

    Currency ISO code. ex. ‘USD’

  • to_currency (String)

    Currency ISO code. ex. ‘CAD’

Returns:

  • (Numeric)

    rate.



94
95
96
97
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 94

def get_rate(from_currency, to_currency, opts = {})
  expire_rates
  super
end

#save_ratesProc, File

Save rates on cache Can raise InvalidCache

Returns:

  • (Proc, File)


81
82
83
84
85
86
87
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 81

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

#source_urlString

Source url of openexchangerates defined with app_id and secure_connection

Returns:

  • (String)

    URL



110
111
112
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 110

def source_url
  "#{oer_url}?app_id=#{app_id}"
end

#update_ratesArray

Update all rates from openexchangerates JSON

Returns:

  • (Array)

    Array of exchange rates



67
68
69
70
71
72
73
74
75
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 67

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)
    set_rate(currency, 'USD', 1.0 / rate)
  end
end