class CoinSync::PriceLoaders::Base
Public Class Methods
new(options)
click to toggle source
# File lib/coinsync/price_loaders/base.rb, line 21 def initialize(options) @options = options @currency = currency @cache = Cache.new(cache_name) end
register_price_loader(key)
click to toggle source
# File lib/coinsync/price_loaders/base.rb, line 13 def self.register_price_loader(key) if PriceLoaders.registered[key.to_sym] raise "Price loader has already been registered at '#{key}'" else PriceLoaders.registered[key.to_sym] = self end end
Public Instance Methods
cache_name()
click to toggle source
# File lib/coinsync/price_loaders/base.rb, line 27 def cache_name self.class.name.downcase.split('::').last end
convert_price(price)
click to toggle source
# File lib/coinsync/price_loaders/base.rb, line 47 def convert_price(price) case price when BigDecimal then price when String, Integer then BigDecimal.new(price) when Float then BigDecimal.new(price, 0) else raise "Unexpected price value: #{price.inspect}" end end
finalize()
click to toggle source
# File lib/coinsync/price_loaders/base.rb, line 56 def finalize @cache.save end
get_price(coin, time)
click to toggle source
# File lib/coinsync/price_loaders/base.rb, line 31 def get_price(coin, time) (coin.is_a?(CryptoCurrency)) or raise "#{self.class}: 'coin' should be a CryptoCurrency" (time.is_a?(Time)) or raise "#{self.class}: 'time' should be a Time" data = @cache[coin, time] if data.nil? data = fetch_price(coin, time) @cache[coin, time] = data end price = data.is_a?(Array) ? data.first : data [convert_price(price), @currency] end