Class: Money::Bank::OpenexchangeratesBank
- Inherits:
-
VariableExchange
- Object
- VariableExchange
- Money::Bank::OpenexchangeratesBank
- Defined in:
- lib/money/bank/openexchangerates_bank.rb
Overview
CurrencylayerBank base class
Constant Summary collapse
- CL_URL =
CurrencylayerBank url
'http://openexchangerates.org/api/latest.json'.freeze
- CL_SECURE_URL =
CurrencylayerBank secure url
CL_URL.sub('http:', 'https:')
- CL_SOURCE =
Default base currency
'USD'.freeze
Instance Attribute Summary collapse
-
#access_key ⇒ String
API must have a valid access_key.
-
#cache ⇒ String, ...
Cache accessor, can be a String or a Proc.
-
#rates ⇒ Object
readonly
Parsed CurrencylayerBank result as Hash.
-
#rates_mem_timestamp ⇒ Time
readonly
Get the timestamp of rates in memory.
-
#secure_connection ⇒ Boolean
Use https to fetch rates from CurrencylayerBank CurrencylayerBank only allows http as connection for the free plan users.
-
#ttl_in_seconds ⇒ Integer
Get the seconds after than the current rates are automatically expired by default, they never expire.
Instance Method Summary collapse
-
#add_rate(from_currency, to_currency, rate) ⇒ Numeric
Override Money ‘add_rate` method for caching.
-
#alternatives ⇒ String?
Get alternative system.
-
#alternatives=(system) ⇒ String?
Set alternative system, to request alternative rates.
-
#expire_rates! ⇒ Boolean
Fetch new rates if cached rates are expired or stale.
-
#expired? ⇒ Boolean
Check if rates are expired.
-
#get_rate(from_currency, to_currency, opts = {}) ⇒ Numeric
Override Money ‘get_rate` method for caching.
-
#rates_expiration ⇒ Time
Get rates expiration time based on ttl.
-
#rates_timestamp ⇒ Time
Get the timestamp of rates.
-
#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 CurrencylayerBank defined with access_key and secure_connection.
-
#stale? ⇒ Boolean
Check if rates are stale Stale is true if rates are updated straight by another thread.
-
#super_get_rate ⇒ Object
Alias super method.
-
#update_rates(straight = false) ⇒ Array
Update all rates from CurrencylayerBank JSON.
Instance Attribute Details
#access_key ⇒ String
API must have a valid access_key
52 53 54 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 52 def access_key @access_key end |
#cache ⇒ String, ...
Cache accessor, can be a String or a Proc
58 59 60 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 58 def cache @cache end |
#rates ⇒ Object (readonly)
Parsed CurrencylayerBank result as Hash
61 62 63 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 61 def rates @rates end |
#rates_mem_timestamp ⇒ Time (readonly)
Get the timestamp of rates in memory
65 66 67 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 65 def @rates_mem_timestamp end |
#secure_connection ⇒ Boolean
Use https to fetch rates from CurrencylayerBank CurrencylayerBank only allows http as connection for the free plan users.
46 47 48 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 46 def secure_connection @secure_connection end |
#ttl_in_seconds ⇒ Integer
Get the seconds after than the current rates are automatically expired by default, they never expire.
123 124 125 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 123 def ttl_in_seconds @ttl_in_seconds ||= 0 end |
Instance Method Details
#add_rate(from_currency, to_currency, rate) ⇒ Numeric
Override Money ‘add_rate` method for caching
147 148 149 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 147 def add_rate(from_currency, to_currency, rate) super end |
#alternatives ⇒ String?
Get alternative system. Default is nil
116 117 118 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 116 def alternatives @alternatives || nil end |
#alternatives=(system) ⇒ String?
Set alternative system, to request alternative rates
105 106 107 108 109 110 111 112 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 105 def alternatives=(system) @alternatives = case system.to_sym when :all 'show_alternative' when :only 'only_alternative' end end |
#expire_rates! ⇒ Boolean
Fetch new rates if cached rates are expired or stale
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 167 def expire_rates! if expired? update_rates(true) true elsif stale? update_rates true else false end end |
#expired? ⇒ Boolean
Check if rates are expired
181 182 183 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 181 def expired? Time.now > rates_expiration end |
#get_rate(from_currency, to_currency, opts = {}) ⇒ Numeric
Override Money ‘get_rate` method for caching
159 160 161 162 163 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 159 def get_rate(from_currency, to_currency, opts = {}) 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_expiration ⇒ Time
Get rates expiration time based on ttl
207 208 209 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 207 def rates_expiration + ttl_in_seconds end |
#rates_timestamp ⇒ Time
Get the timestamp of rates
213 214 215 216 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 213 def raw = raw_rates_careful raw.key?('timestamp') ? Time.at(raw['timestamp']) : Time.at(0) end |
#source ⇒ String
Get the base currency for all rates. By default, USD is used.
92 93 94 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 92 def source @source ||= CL_SOURCE end |
#source=(value) ⇒ String
Set the base currency for all rates. By default, USD is used. CurrencylayerBank only allows USD as base currency for the free plan users.
86 87 88 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 86 def source=(value) @source = Money::Currency.find(value.to_s).try(:iso_code) || CL_SOURCE end |
#source_url ⇒ String
Source url of CurrencylayerBank defined with access_key and secure_connection
196 197 198 199 200 201 202 203 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 196 def source_url raise NoAccessKey if access_key.nil? || access_key.empty? cl_url = CL_URL cl_url = CL_SECURE_URL if secure_connection params = "#{cl_url}?base=#{source}&app_id=#{access_key}&prettyprint=0" params += "&#{alternatives}=1" unless alternatives.nil? params end |
#stale? ⇒ Boolean
Check if rates are stale Stale is true if rates are updated straight by another thread. The actual thread has always old rates in memory store.
189 190 191 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 189 def stale? != end |
#super_get_rate ⇒ Object
Alias super method
152 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 152 alias super_get_rate get_rate |
#update_rates(straight = false) ⇒ Array
Update all rates from CurrencylayerBank JSON
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/money/bank/openexchangerates_bank.rb', line 129 def update_rates(straight = false) store.reset! rates = exchange_rates(straight).each do |exchange_rate| currency = exchange_rate.first rate = exchange_rate.last next unless Money::Currency.find(currency) add_rate(source, currency, rate) add_rate(currency, source, 1.0 / rate) end @rates_mem_timestamp = rates end |