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/'.freeze
- 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'.freeze
- RATES_KEY =
'rates'.freeze
- TIMESTAMP_KEY =
'timestamp'.freeze
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
43 44 45 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 43 def app_id @app_id end |
#cache ⇒ String, Proc
Cache accessor
52 53 54 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 52 def cache @cache end |
#date ⇒ String
Date for historical api see docs.openexchangerates.org/docs/historical-json
62 63 64 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 62 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
69 70 71 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 69 def force_refresh_rate_on_expire @force_refresh_rate_on_expire end |
#json_response ⇒ String (readonly)
Unparsed OpenExchangeRates response as String
84 85 86 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 84 def json_response @json_response end |
#oer_rates ⇒ Hash (readonly)
Parsed OpenExchangeRates result as Hash
79 80 81 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 79 def oer_rates @oer_rates end |
#prettyprint ⇒ Boolean
Get prettyprint option
235 236 237 238 239 240 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 235 def prettyprint return true unless defined? @prettyprint return true if @prettyprint.nil? @prettyprint end |
#rates_expiration ⇒ Time (readonly)
Rates expiration Time
74 75 76 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 74 def rates_expiration @rates_expiration end |
#show_alternative ⇒ Boolean
Get show alternative
228 229 230 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 228 def show_alternative @show_alternative ||= false end |
#symbols ⇒ Array
Get symbols
245 246 247 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 245 def symbols @symbols ||= nil end |
#ttl_in_seconds ⇒ Integer
Seconds after than the current rates are automatically expired
89 90 91 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 89 def ttl_in_seconds @ttl_in_seconds end |
Instance Method Details
#expire_rates ⇒ NilClass, Time
Expire rates when expired
216 217 218 219 220 221 222 223 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 216 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
196 197 198 199 200 201 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 196 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
128 129 130 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 128 def || Time.now end |
#rates_timestamp=(at) ⇒ Time
Set current rates timestamp
121 122 123 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 121 def (at) = Time.at(at) end |
#refresh_rates ⇒ Array Also known as: save_rates
Fetch from url and save cache
206 207 208 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 206 def refresh_rates read_from_url end |
#source ⇒ String
Get the base currency for all rates. By default, USD is used.
169 170 171 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 169 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.
157 158 159 160 161 162 163 164 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 157 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
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 253 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}" if symbols && symbols.is_a?(Array) str = "#{str}&symbols=#{symbols.join(',')}" end str end |
#super_get_rate ⇒ Object
Alias super method
188 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 188 alias super_get_rate get_rate |
#update_rates ⇒ Array
Update all rates from openexchangerates JSON
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/money/bank/open_exchange_rates_bank.rb', line 176 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(source, currency, rate) set_rate(currency, source, 1.0 / rate) end end |