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
- BASE_URL =
'https://openexchangerates.org/api/'
- OER_URL =
OpenExchangeRates urls
URI.join(BASE_URL, 'latest.json')
- OER_HISTORICAL_URL =
URI.join(BASE_URL, 'historical/')
- OE_SOURCE =
Default base currency “base”: “USD”
'USD'
- RATES_KEY =
'rates'
- TIMESTAMP_KEY =
'timestamp'
Instance Attribute Summary collapse
-
#app_id ⇒ String
As of the end of August 2012 all requests to the Open Exchange Rates API must have a valid app_id see docs.openexchangerates.org/docs/authentication.
-
#cache ⇒ String, Proc
Cache accessor.
-
#date ⇒ String
Date for historical api see docs.openexchangerates.org/docs/historical-json.
-
#force_refresh_rate_on_expire ⇒ Object
Force refresh rates cache and store on the fly when ttl is expired This will slow down request on get_rate, so use at your on risk, if you don’t want to setup crontab/worker/scheduler for your application.
-
#json_response ⇒ String
readonly
Unparsed OpenExchangeRates response as String.
-
#oer_rates ⇒ Hash
readonly
Parsed OpenExchangeRates result as Hash.
-
#prettyprint ⇒ Boolean
Get prettyprint option.
-
#rates_expiration ⇒ Time
readonly
Rates expiration Time.
-
#show_alternative ⇒ Boolean
Get show alternative.
-
#symbols ⇒ Array
Get symbols.
-
#ttl_in_seconds ⇒ Integer
Seconds after than the current rates are automatically expired.
Instance Method Summary collapse
-
#expire_rates ⇒ NilClass, Time
Expire rates when expired.
-
#get_rate(from_currency, to_currency, opts = {}) ⇒ Numeric
Override Money ‘get_rate` method for caching.
-
#rates_timestamp ⇒ Time
Current rates timestamp.
-
#rates_timestamp=(at) ⇒ Time
Set current rates timestamp.
-
#refresh_rates ⇒ Array
(also: #save_rates)
Fetch from url and save cache.
-
#source ⇒ String
Get the base currency for all rates.
-
#source=(value) ⇒ String
Set the base currency for all rates.
-
#source_url ⇒ String
Source url of openexchangerates defined with app_id.
-
#super_get_rate ⇒ Object
Alias super method.
-
#update_rates ⇒ Array
Update all rates from openexchangerates JSON.
Instance Attribute Details
#app_id ⇒ String
As of the end of August 2012 all requests to the Open Exchange Rates API must have a valid app_id see docs.openexchangerates.org/docs/authentication
55 56 57 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 55 def app_id @app_id end |
#cache ⇒ String, Proc
Cache accessor
64 65 66 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 64 def cache @cache end |
#date ⇒ String
Date for historical api see docs.openexchangerates.org/docs/historical-json
74 75 76 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 74 def date @date end |
#force_refresh_rate_on_expire ⇒ Object
Force refresh rates cache and store on the fly when ttl is expired This will slow down request on get_rate, so use at your on risk, if you don’t want to setup crontab/worker/scheduler for your application
81 82 83 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 81 def force_refresh_rate_on_expire @force_refresh_rate_on_expire end |
#json_response ⇒ String (readonly)
Unparsed OpenExchangeRates response as String
96 97 98 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 96 def json_response @json_response end |
#oer_rates ⇒ Hash (readonly)
Parsed OpenExchangeRates result as Hash
91 92 93 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 91 def oer_rates @oer_rates end |
#prettyprint ⇒ Boolean
Get prettyprint option
250 251 252 253 254 255 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 250 def prettyprint return true unless defined? @prettyprint return true if @prettyprint.nil? @prettyprint end |
#rates_expiration ⇒ Time (readonly)
Rates expiration Time
86 87 88 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 86 def rates_expiration @rates_expiration end |
#show_alternative ⇒ Boolean
Get show alternative
243 244 245 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 243 def show_alternative @show_alternative ||= false end |
#symbols ⇒ Array
Get symbols
260 261 262 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 260 def symbols @symbols ||= nil end |
#ttl_in_seconds ⇒ Integer
Seconds after than the current rates are automatically expired
101 102 103 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 101 def ttl_in_seconds @ttl_in_seconds end |
Instance Method Details
#expire_rates ⇒ NilClass, Time
Expire rates when expired
231 232 233 234 235 236 237 238 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 231 def expire_rates return unless ttl_in_seconds return if rates_expiration > Time.now refresh_rates if force_refresh_rate_on_expire update_rates refresh_rates_expiration end |
#get_rate(from_currency, to_currency, opts = {}) ⇒ Numeric
Override Money ‘get_rate` method for caching
211 212 213 214 215 216 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 211 def get_rate(from_currency, to_currency, opts = {}) super if opts[:call_super] expire_rates rate = get_rate_or_calc_inverse(from_currency, to_currency, opts) rate || calc_pair_rate_using_base(from_currency, to_currency, opts) end |
#rates_timestamp ⇒ Time
Current rates timestamp
140 141 142 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 140 def @rates_timestamp || Time.now end |
#rates_timestamp=(at) ⇒ Time
Set current rates timestamp
133 134 135 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 133 def (at) @rates_timestamp = Time.at(at) end |
#refresh_rates ⇒ Array Also known as: save_rates
Fetch from url and save cache
221 222 223 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 221 def refresh_rates read_from_url end |
#source ⇒ String
Get the base currency for all rates. By default, USD is used.
181 182 183 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 181 def source @source ||= OE_SOURCE end |
#source=(value) ⇒ String
Set the base currency for all rates. By default, USD is used. OpenExchangeRates only allows USD as base currency for the free plan users.
169 170 171 172 173 174 175 176 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 169 def source=(value) scurrency = Money::Currency.find(value.to_s) @source = if scurrency scurrency.iso_code else OE_SOURCE end end |
#source_url ⇒ String
Source url of openexchangerates defined with app_id
268 269 270 271 272 273 274 275 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 268 def source_url str = "#{oer_url}?app_id=#{app_id}" str = "#{str}&base=#{source}" unless source == OE_SOURCE str = "#{str}&show_alternative=#{show_alternative}" str = "#{str}&prettyprint=#{prettyprint}" str = "#{str}&symbols=#{symbols.join(',')}" if symbols&.is_a?(Array) str end |
#super_get_rate ⇒ Object
Alias super method
203 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 203 alias super_get_rate get_rate |
#update_rates ⇒ Array
Update all rates from openexchangerates JSON
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 188 def update_rates store.transaction do clear_rates! exchange_rates.each do |exchange_rate| rate = exchange_rate.last currency = exchange_rate.first next unless Money::Currency.find(currency) set_rate(source, currency, rate) set_rate(currency, source, 1.0 / rate) end end end |