Class: Exchange::ExternalAPI::Base
- Inherits:
-
Object
- Object
- Exchange::ExternalAPI::Base
- Defined in:
- lib/exchange/external_api/base.rb
Overview
The Base class of all External APIs, handling basic exchange rates and conversion
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (String) base
readonly
The currency which was the base for the rates.
-
- (Exchange::Cache) cache
readonly
The cache subclass.
-
- (Exchange::Helper) helper
readonly
The Exchange Helper.
-
- (Hash) rates
readonly
A Hash which delivers the exchange rate of every available currency to the base currency.
-
- (Integer) timestamp
readonly
A unix timestamp for the rates, delivered by the API.
Instance Method Summary (collapse)
-
- (Float) convert(amount, from, to, opts = {})
Converts an amount of one currency into another.
-
- (Base) initialize(*args)
constructor
Initialize with a convenience accessor for the Cache and the api subclass.
-
- (Float) rate(from, to, opts = {})
Delivers an exchange rate from one currency to another with the option of getting a historical exchange rate.
-
- (Object) test_for_rates_and_raise_if_nil(rate_from, rate_to, time = nil)
Test for a error to be thrown when no rates are present.
-
- (Hash) to_hash!(array)
Converts an array to a hash.
Constructor Details
- (Base) initialize(*args)
Initialize with a convenience accessor for the Cache and the api subclass
109 110 111 112 113 114 |
# File 'lib/exchange/external_api/base.rb', line 109 def initialize *args @cache = Exchange.configuration.cache.subclass @helper = Exchange::Helper.instance super *args end |
Instance Attribute Details
- (String) base (readonly)
The currency which was the base for the rates
86 87 88 |
# File 'lib/exchange/external_api/base.rb', line 86 def base @base end |
- (Exchange::Cache) cache (readonly)
The cache subclass
100 101 102 |
# File 'lib/exchange/external_api/base.rb', line 100 def cache @cache end |
- (Exchange::Helper) helper (readonly)
The Exchange Helper
104 105 106 |
# File 'lib/exchange/external_api/base.rb', line 104 def helper @helper end |
- (Hash) rates (readonly)
A Hash which delivers the exchange rate of every available currency to the base currency
96 97 98 |
# File 'lib/exchange/external_api/base.rb', line 96 def rates @rates end |
- (Integer) timestamp (readonly)
A unix timestamp for the rates, delivered by the API
91 92 93 |
# File 'lib/exchange/external_api/base.rb', line 91 def @timestamp end |
Instance Method Details
- (Float) convert(amount, from, to, opts = {})
Converts an amount of one currency into another
153 154 155 |
# File 'lib/exchange/external_api/base.rb', line 153 def convert amount, from, to, opts={} amount * rate(from, to, opts) end |
- (Float) rate(from, to, opts = {})
Delivers an exchange rate from one currency to another with the option of getting a historical exchange rate. This rate has to be multiplied with the amount of the currency which you define in from
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/exchange/external_api/base.rb', line 127 def rate from, to, opts={} rate = cache.cached(self.class, opts.merge(:key_for => [from, to])) do update(opts) rate_from = rates[from] rate_to = rates[to] test_for_rates_and_raise_if_nil rate_from, rate_to, opts[:at] rate_to / rate_from end rate.is_a?(BigDecimal) ? rate : BigDecimal.new(rate.to_s) end |
- (Object) test_for_rates_and_raise_if_nil(rate_from, rate_to, time = nil)
Test for a error to be thrown when no rates are present
171 172 173 |
# File 'lib/exchange/external_api/base.rb', line 171 def test_for_rates_and_raise_if_nil rate_from, rate_to, time=nil raise NoRateError.new("No rates where found for #{rate_from} to #{rate_to} #{'at ' + time.to_s if time}") unless rate_from && rate_to end |
- (Hash) to_hash!(array)
Converts an array to a hash
161 162 163 |
# File 'lib/exchange/external_api/base.rb', line 161 def to_hash! array Hash[*array] end |