Class: Money::Bank::JsonRates
- Inherits:
-
VariableExchange
- Object
- VariableExchange
- Money::Bank::JsonRates
- Defined in:
- lib/money/bank/json_rates.rb
Overview
Money::Bank implementation that gives access to the current exchange rates using jsonrates.com api.
Constant Summary collapse
- SERVICE_HOST =
Host of service jsonrates
"jsonrates.com"
- SERVICE_PATH =
Relative path of jsonrates api
"/get"
Class Attribute Summary collapse
-
.rates_careful ⇒ Boolean
Returns is Rates Careful mode set.
-
.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
-
#api_key ⇒ Object
accessor of api_key of jsonrates.com service.
-
#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
-
#add_rate(from, to, rate) ⇒ Numeric
Registers a conversion rate and returns it (uses
#set_rate
). -
#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.
-
#rate_key_for(from, to) ⇒ String
Return the rate hashkey for the given currencies.
-
#set_rate(from, to, rate) ⇒ Numeric
Set the rate for the given currencies.
Class Attribute Details
.rates_careful ⇒ Boolean
Returns is Rates Careful mode set.
46 47 48 |
# File 'lib/money/bank/json_rates.rb', line 46 def rates_careful @rates_careful end |
.rates_expiration ⇒ Time (readonly)
Returns the time when the rates expire.
43 44 45 |
# File 'lib/money/bank/json_rates.rb', line 43 def rates_expiration @rates_expiration end |
.ttl_in_seconds ⇒ Integer
Returns the Time To Live (TTL) in seconds.
40 41 42 |
# File 'lib/money/bank/json_rates.rb', line 40 def ttl_in_seconds @ttl_in_seconds end |
Instance Attribute Details
#api_key ⇒ Object
accessor of api_key of jsonrates.com service
36 37 38 |
# File 'lib/money/bank/json_rates.rb', line 36 def api_key @api_key end |
#rates ⇒ Hash (readonly)
Returns Stores the currently known rates.
33 34 35 |
# File 'lib/money/bank/json_rates.rb', line 33 def rates @rates end |
Class Method Details
.refresh_rates_expiration! ⇒ Time
Set the rates expiration TTL seconds from the current time.
69 70 71 |
# File 'lib/money/bank/json_rates.rb', line 69 def refresh_rates_expiration! @rates_expiration = Time.now + ttl_in_seconds end |
Instance Method Details
#add_rate(from, to, rate) ⇒ Numeric
Registers a conversion rate and returns it (uses #set_rate
).
143 144 145 |
# File 'lib/money/bank/json_rates.rb', line 143 def add_rate from, to, rate set_rate from, to, rate end |
#expire_rates ⇒ Boolean
Flushes all the rates if they are expired.
194 195 196 197 198 199 200 201 202 |
# File 'lib/money/bank/json_rates.rb', line 194 def expire_rates if expired? flush_rates self.class.refresh_rates_expiration! true else false end end |
#flush_rate(from, to) ⇒ Float
Clears the specified rate stored in @rates.
103 104 105 106 107 108 |
# File 'lib/money/bank/json_rates.rb', line 103 def flush_rate(from, to) key = rate_key_for(from, to) @mutex.synchronize{ @rates.delete(key) } end |
#flush_rates ⇒ Hash
Clears all rates stored in @rates
83 84 85 86 87 |
# File 'lib/money/bank/json_rates.rb', line 83 def flush_rates @mutex.synchronize{ @rates = {} } end |
#get_rate(from, to) ⇒ Float
Returns the requested rate.
It uses #get_rate_careful
or #get_rate_straight
respect of @rates_careful value
123 124 125 126 127 128 129 |
# File 'lib/money/bank/json_rates.rb', line 123 def get_rate(from, to) if self.class.rates_careful get_rate_careful(from, to) else get_rate_straight(from, to) end end |
#rate_key_for(from, to) ⇒ String
Return the rate hashkey for the given currencies.
182 183 184 185 186 187 188 |
# File 'lib/money/bank/json_rates.rb', line 182 def rate_key_for(from, to) if self.class.rates_careful "#{Currency.wrap(from).iso_code}_TO_#{Currency.wrap(to).iso_code}_C".upcase else super end end |
#set_rate(from, to, rate) ⇒ Numeric
Set the rate for the given currencies. Uses Mutex
to synchronize data access.
162 163 164 165 166 167 168 |
# File 'lib/money/bank/json_rates.rb', line 162 def set_rate from, to, rate if self.class.rates_careful set_rate_with_time(from, to, rate) else super end end |