Class: Money::Bank::OpenExchangeRatesBank
- Inherits:
-
VariableExchange
- Object
- VariableExchange
- Money::Bank::OpenExchangeRatesBank
- 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
-
#app_id ⇒ Object
As of the end of August 2012 all requests to the Open Exchange Rates API must have a valid app_id.
-
#cache ⇒ Object
Cache accessor, can be a String or a Proc.
-
#date ⇒ Object
Date for historical api see openexchangerates.org/documentation#historical-data.
-
#oer_rates ⇒ Object
readonly
Parsed OpenExchangeRates result as Hash.
-
#rates_expiration ⇒ Object
readonly
Rates expiration Time.
-
#secure_connection ⇒ Object
use https to fetch rates from Open Exchange Rates disabled by default to support free-tier users.
-
#ttl_in_seconds ⇒ Object
Seconds after than the current rates are automatically expired.
Instance Method Summary collapse
-
#expire_rates ⇒ Object
Expire rates when expired.
-
#get_rate(from_currency, to_currency, opts = {}) ⇒ Numeric
Override Money ‘get_rate` method for caching.
-
#save_rates ⇒ Proc, File
Save rates on cache Can raise InvalidCache.
-
#source_url ⇒ String
Source url of openexchangerates defined with app_id and secure_connection.
-
#update_rates ⇒ Array
Update all rates from openexchangerates JSON.
Instance Attribute Details
#app_id ⇒ Object
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 |
#cache ⇒ Object
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 |
#date ⇒ Object
Date for historical api see openexchangerates.org/documentation#historical-data
39 40 41 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 39 def date @date end |
#oer_rates ⇒ Object (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_expiration ⇒ Object (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_connection ⇒ Object
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_seconds ⇒ Object
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_rates ⇒ Object
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
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_rates ⇒ Proc, File
Save rates on cache Can raise InvalidCache
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_url ⇒ String
Source url of openexchangerates defined with app_id and secure_connection
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_rates ⇒ Array
Update all rates from openexchangerates JSON
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 |