Class: CurrencyCoinConverter::Base
- Inherits:
-
Object
- Object
- CurrencyCoinConverter::Base
- Includes:
- Singleton
- Defined in:
- lib/currency_coin_converter/base.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
Class Method Summary collapse
- .api_key ⇒ Object
- .api_key=(api_key) ⇒ Object
- .api_version ⇒ Object
- .conversion_rate(from:, to:) ⇒ Object
- .conversion_rates(base_currency) ⇒ Object
- .convert(amount, from:, to:, round: 2) ⇒ Object
Instance Method Summary collapse
- #conversion_rate(from:, to:) ⇒ Object
- #conversion_rates(base_currency) ⇒ Object
- #convert(amount, from:, to:, round: 2) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
12 13 14 15 |
# File 'lib/currency_coin_converter/base.rb', line 12 def initialize self.api_version = 'v6' @cache = {} end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
48 49 50 |
# File 'lib/currency_coin_converter/base.rb', line 48 def api_key @api_key end |
#api_version ⇒ Object
Returns the value of attribute api_version.
49 50 51 |
# File 'lib/currency_coin_converter/base.rb', line 49 def api_version @api_version end |
Class Method Details
.api_key ⇒ Object
97 98 99 |
# File 'lib/currency_coin_converter/base.rb', line 97 def api_key instance.api_key end |
.api_key=(api_key) ⇒ Object
101 102 103 |
# File 'lib/currency_coin_converter/base.rb', line 101 def api_key=(api_key) instance.api_key = api_key end |
.api_version ⇒ Object
105 106 107 |
# File 'lib/currency_coin_converter/base.rb', line 105 def api_version instance.api_version end |
.conversion_rate(from:, to:) ⇒ Object
113 114 115 |
# File 'lib/currency_coin_converter/base.rb', line 113 def conversion_rate(from:, to:) instance.conversion_rate(from: from, to: to) end |
.conversion_rates(base_currency) ⇒ Object
117 118 119 |
# File 'lib/currency_coin_converter/base.rb', line 117 def conversion_rates(base_currency) instance.conversion_rates(base_currency) end |
.convert(amount, from:, to:, round: 2) ⇒ Object
109 110 111 |
# File 'lib/currency_coin_converter/base.rb', line 109 def convert(amount, from:, to:, round: 2) instance.convert(amount, from: from, to: to, round: round) end |
Instance Method Details
#conversion_rate(from:, to:) ⇒ Object
21 22 23 24 25 |
# File 'lib/currency_coin_converter/base.rb', line 21 def conversion_rate(from:, to:) conversion_rates(from).fetch(to) do raise Error, "currency code is invalid: #{to}" end end |
#conversion_rates(base_currency) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/currency_coin_converter/base.rb', line 27 def conversion_rates(base_currency) cached = @cache[base_currency] return cached[:rates] if cached && !cache_expired?(cached) begin rates = get(path: base_currency)['conversion_rates'] @cache[base_currency] = { rates: rates, created_at: Process.clock_gettime(Process::CLOCK_MONOTONIC) } rates rescue Error return cached[:rates] if cached raise end end |
#convert(amount, from:, to:, round: 2) ⇒ Object
17 18 19 |
# File 'lib/currency_coin_converter/base.rb', line 17 def convert(amount, from:, to:, round: 2) (amount * conversion_rate(from: from, to: to)).round(round) end |