Class: CoinSync::PriceLoaders::Base
- Inherits:
-
Object
- Object
- CoinSync::PriceLoaders::Base
- Defined in:
- lib/coinsync/price_loaders/base.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #cache_name ⇒ Object
- #convert_price(price) ⇒ Object
- #finalize ⇒ Object
- #get_price(coin, time) ⇒ Object
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
Constructor Details
Class Method Details
.register_price_loader(key) ⇒ Object
13 14 15 16 17 18 19 |
# 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 |
Instance Method Details
#cache_name ⇒ Object
27 28 29 |
# File 'lib/coinsync/price_loaders/base.rb', line 27 def cache_name self.class.name.downcase.split('::').last end |
#convert_price(price) ⇒ Object
47 48 49 50 51 52 53 54 |
# 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 ⇒ Object
56 57 58 |
# File 'lib/coinsync/price_loaders/base.rb', line 56 def finalize @cache.save end |
#get_price(coin, time) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 |