class CryptoVal::Latest

Constants

DEFAULT_CRYPTO

Gets the latest value of a cryptocurrency

DEFAULT_CURRENCY

Public Class Methods

fetch(opts={}) click to toggle source
# File lib/crypto_val/latest.rb, line 22
def self.fetch opts={}
  new(opts).fetch
end
new(opts={}) click to toggle source
# File lib/crypto_val/latest.rb, line 7
def initialize opts={}
  @symbol       = opts[:symbol] || DEFAULT_CRYPTO
  @currency     = opts[:currency] || DEFAULT_CURRENCY
  @crypto_klass = opts[:crypto_klass] || CryptoVal::CryptoExchange::Cryptonator
  @fiat_klass   = opts[:fiat_klass] || CryptoVal::FiatExchange
end

Public Instance Methods

fetch() click to toggle source
# File lib/crypto_val/latest.rb, line 14
def fetch
  { 
    symbol: @symbol,
    currency: @currency,
    value: currency_value
  } 
end

Private Instance Methods

crypto() click to toggle source
# File lib/crypto_val/latest.rb, line 28
def crypto
  @crypto ||= @crypto_klass.fetch({ symbol: @symbol })
end
crypto_value() click to toggle source
# File lib/crypto_val/latest.rb, line 36
def crypto_value
  @crypto_value ||= crypto[:price]
end
currency_value() click to toggle source
# File lib/crypto_val/latest.rb, line 44
def currency_value
  @currency != DEFAULT_CURRENCY ? (crypto_value * exchange_rate).to_f.round(2) : crypto_value.to_f.round(2)
end
exchange_rate() click to toggle source
# File lib/crypto_val/latest.rb, line 40
def exchange_rate
  exchange_rate ||= fiat[:rate]
end
fiat() click to toggle source
# File lib/crypto_val/latest.rb, line 32
def fiat
  @fiat ||= @fiat_klass.fetch({ target_currency: @currency })
end