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
-
#base ⇒ String
readonly
The currency which was the base for the rates.
-
#cache ⇒ Exchange::Cache
readonly
The cache subclass.
-
#helper ⇒ Exchange::Helper
readonly
The Exchange Helper.
-
#rates ⇒ Hash
readonly
A Hash which delivers the exchange rate of every available currency to the base currency.
-
#timestamp ⇒ Integer
readonly
A unix timestamp for the rates, delivered by the API.
Instance Method Summary collapse
-
#convert(amount, from, to, opts = {}) ⇒ Float
Converts an amount of one currency into another.
-
#initialize(*args) ⇒ Base
constructor
Initialize with a convenience accessor for the Cache and the api subclass.
-
#rate(from, to, opts = {}) ⇒ Float
Delivers an exchange rate from one currency to another with the option of getting a historical exchange rate.
-
#test_for_rates_and_raise_if_nil(rate_from, rate_to, time = nil) ⇒ Object
Test for a error to be thrown when no rates are present.
-
#to_hash!(array) ⇒ Hash
Converts an array to a hash.
Constructor Details
Instance Attribute Details
#base ⇒ String (readonly)
Returns 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 |
#cache ⇒ Exchange::Cache (readonly)
Returns The cache subclass.
100 101 102 |
# File 'lib/exchange/external_api/base.rb', line 100 def cache @cache end |
#helper ⇒ Exchange::Helper (readonly)
Returns The Exchange Helper.
104 105 106 |
# File 'lib/exchange/external_api/base.rb', line 104 def helper @helper end |
#rates ⇒ Hash (readonly)
Returns 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 |
#timestamp ⇒ Integer (readonly)
Returns 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
#convert(amount, from, to, opts = {}) ⇒ Float
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 |
#rate(from, to, opts = {}) ⇒ Float
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 |
#test_for_rates_and_raise_if_nil(rate_from, rate_to, time = nil) ⇒ Object
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 |
#to_hash!(array) ⇒ Hash
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 |