Class: ExchangeRatesGenerator::Sources::Base
- Inherits:
-
Object
- Object
- ExchangeRatesGenerator::Sources::Base
- Defined in:
- lib/exchange-rates-generator/sources/base.rb
Direct Known Subclasses
Class Method Summary collapse
-
.inherited(source) ⇒ Object
Records all new Sources.
-
.rates_for(currency_code) ⇒ Hash
Retrieve a Hash of currency codes and exchange rates, they rates will be relative to
currency_code
. -
.sources ⇒ Array
Returns all Sources.
Class Method Details
.inherited(source) ⇒ Object
Records all new Sources
23 24 25 |
# File 'lib/exchange-rates-generator/sources/base.rb', line 23 def inherited(source) self.sources << source end |
.rates_for(currency_code) ⇒ Hash
Retrieve a Hash of currency codes and exchange rates, they rates will be relative to currency_code
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/exchange-rates-generator/sources/base.rb', line 31 def rates_for(currency_code) # Retrieve the actual rates from the external source rates = all_rates adjusted_currency = currency_code.to_s.upcase.to_sym unless rates.include?(adjusted_currency) raise Errors::CurrencyNotAvailable, "#{adjusted_currency.to_s} was not available in this Source (#{self.to_s}), please use a different Source" end adjusted_rates = {} # Add the currency we are converting to... adjusted_rates[adjusted_currency] = 1.0 # Work out the exchange from our desired currency to our base currency. So if our base was EUR and we wanted USD this would # be how many Euros are in one US dollar. adjustment_factor = 1.0 / rates[adjusted_currency] adjusted_rates[base_currency] = adjustment_factor # Now remove it, since we've already done it. rates.delete(base_currency) # Now convert the rest rates.each do |currency, rate| adjusted_rates[currency] = rate * adjustment_factor end adjusted_rates end |
.sources ⇒ Array
Returns all Sources
18 19 20 |
# File 'lib/exchange-rates-generator/sources/base.rb', line 18 def sources @sources ||= [] end |