Class: CoinSync::CurrencyConverters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/coinsync/currency_converters/base.rb

Direct Known Subclasses

ExchangeRatesAPI, NBP

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



21
22
23
24
# File 'lib/coinsync/currency_converters/base.rb', line 21

def initialize(options)
  @options = options
  @cache = Cache.new(self.class.name.downcase.split('::').last)
end

Class Method Details

.register_converter(key) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/coinsync/currency_converters/base.rb', line 13

def self.register_converter(key)
  if CurrencyConverters.registered[key]
    raise "Currency converter has already been registered at '#{key}'"
  else
    CurrencyConverters.registered[key] = self
  end
end

Instance Method Details

#convert(amount, from:, to:, time:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/coinsync/currency_converters/base.rb', line 26

def convert(amount, from:, to:, time:)
  (amount > 0) or raise "#{self.class}: amount should be positive"
  (amount.is_a?(BigDecimal)) or raise "#{self.class}: 'amount' should be a BigDecimal"

  rate = get_conversion_rate(from: from, to: to, time: time)

  rate * amount
end

#finalizeObject



39
40
41
# File 'lib/coinsync/currency_converters/base.rb', line 39

def finalize
  @cache.save
end

#get_conversion_rate(from:, to:, time:) ⇒ Object



35
36
37
# File 'lib/coinsync/currency_converters/base.rb', line 35

def get_conversion_rate(from:, to:, time:)
  raise "not implemented"
end