Class: Money::Bank::OpenExchangeRatesBank
- Inherits:
-
VariableExchange
- Object
- VariableExchange
- Money::Bank::OpenExchangeRatesBank
- Defined in:
- lib/money/bank/open_exchange_rates_bank.rb
Constant Summary collapse
- OER_URL =
'http://openexchangerates.org/latest.json'
Instance Attribute Summary collapse
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#oer_rates ⇒ Object
readonly
Returns the value of attribute oer_rates.
-
#rates_expiration ⇒ Object
readonly
Returns the value of attribute rates_expiration.
-
#ttl_in_seconds ⇒ Object
Returns the value of attribute ttl_in_seconds.
Instance Method Summary collapse
- #expire_rates ⇒ Object
- #get_rate(from_currency, to_currency, opts = {}) ⇒ Object
- #save_rates ⇒ Object
- #update_rates ⇒ Object
Instance Attribute Details
#app_id ⇒ Object
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 |
#cache ⇒ Object
Returns the value of attribute cache.
16 17 18 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 16 def cache @cache end |
#doc ⇒ Object (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_rates ⇒ Object (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_expiration ⇒ Object (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_seconds ⇒ Object
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
#expire_rates ⇒ Object
49 50 51 52 53 54 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 49 def expire_rates if ttl_in_seconds && rates_expiration <= Time.now update_rates refresh_rates_expiration end end |
#get_rate(from_currency, to_currency, opts = {}) ⇒ Object
44 45 46 47 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 44 def get_rate(from_currency, to_currency, opts = {}) expire_rates super end |
#save_rates ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 34 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_rates ⇒ Object
24 25 26 27 28 29 30 31 32 |
# 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) set_rate(currency, 'USD', 1.0/rate) end end |