Class: Money::Bank::XeCurrency
- Inherits:
-
VariableExchange
- Object
- VariableExchange
- Money::Bank::XeCurrency
- Defined in:
- lib/money/bank/xe_currency.rb
Defined Under Namespace
Classes: XeCurrencyFetchError
Constant Summary collapse
- SERVICE_HOST =
"www.xe.com"- SERVICE_PATH =
"/currencyconverter/convert"
Class Attribute Summary collapse
-
.rates_expiration ⇒ Time
readonly
Returns the time when the rates expire.
-
.ttl_in_seconds ⇒ Integer
Returns the Time To Live (TTL) in seconds.
Instance Attribute Summary collapse
-
#rates ⇒ Hash
readonly
Stores the currently known rates.
Class Method Summary collapse
-
.refresh_rates_expiration! ⇒ Time
Set the rates expiration TTL seconds from the current time.
Instance Method Summary collapse
-
#expire_rates ⇒ Boolean
Flushes all the rates if they are expired.
-
#flush_rate(from, to) ⇒ Float
Clears the specified rate stored in @rates.
-
#flush_rates ⇒ Hash
Clears all rates stored in @rates.
-
#get_rate(from, to) ⇒ Float
Returns the requested rate.
-
#initialize ⇒ XeCurrency
constructor
A new instance of XeCurrency.
Constructor Details
#initialize ⇒ XeCurrency
Returns a new instance of XeCurrency.
46 47 48 49 |
# File 'lib/money/bank/xe_currency.rb', line 46 def initialize(*) super @store.extend Money::RatesStore::RateRemovalSupport end |
Class Attribute Details
.rates_expiration ⇒ Time (readonly)
Returns the time when the rates expire.
26 27 28 |
# File 'lib/money/bank/xe_currency.rb', line 26 def rates_expiration @rates_expiration end |
.ttl_in_seconds ⇒ Integer
Returns the Time To Live (TTL) in seconds.
23 24 25 |
# File 'lib/money/bank/xe_currency.rb', line 23 def ttl_in_seconds @ttl_in_seconds end |
Instance Attribute Details
#rates ⇒ Hash (readonly)
Returns Stores the currently known rates.
18 19 20 |
# File 'lib/money/bank/xe_currency.rb', line 18 def rates @rates end |
Class Method Details
.refresh_rates_expiration! ⇒ Time
Set the rates expiration TTL seconds from the current time.
41 42 43 |
# File 'lib/money/bank/xe_currency.rb', line 41 def refresh_rates_expiration! @rates_expiration = Time.now + ttl_in_seconds end |
Instance Method Details
#expire_rates ⇒ Boolean
Flushes all the rates if they are expired.
104 105 106 107 108 109 110 111 112 |
# File 'lib/money/bank/xe_currency.rb', line 104 def expire_rates if self.class.ttl_in_seconds && self.class.rates_expiration <= Time.now flush_rates self.class.refresh_rates_expiration! true else false end end |
#flush_rate(from, to) ⇒ Float
Clears the specified rate stored in @rates.
78 79 80 |
# File 'lib/money/bank/xe_currency.rb', line 78 def flush_rate(from, to) store.remove_rate(from, to) end |
#flush_rates ⇒ Hash
Clears all rates stored in @rates
60 61 62 |
# File 'lib/money/bank/xe_currency.rb', line 60 def flush_rates store.clear_rates end |
#get_rate(from, to) ⇒ Float
Returns the requested rate.
It also flushes all the rates when and if they are expired.
95 96 97 98 |
# File 'lib/money/bank/xe_currency.rb', line 95 def get_rate(from, to) expire_rates store.get_rate(from, to) || store.add_rate(from, to, fetch_rate(from, to)) end |