Class: CoinTools::CoinMarketCap::CoinData

Inherits:
Listing
  • Object
show all
Defined in:
lib/cointools/coinmarketcap.rb

Instance Attribute Summary collapse

Attributes inherited from Listing

#name, #numeric_id, #symbol, #text_id

Instance Method Summary collapse

Constructor Details

#initialize(json, convert_to = nil) ⇒ CoinData

Returns a new instance of CoinData.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cointools/coinmarketcap.rb', line 38

def initialize(json, convert_to = nil)
  super(json)

  raise ArgumentError.new('Missing rank field') unless json['rank']
  raise ArgumentError.new('Invalid rank field') unless json['rank'].is_a?(Integer) && json['rank'] > 0
  @rank = json['rank']

  quotes = json['quotes']
  raise ArgumentError.new('Missing quotes field') unless quotes
  raise ArgumentError.new('Invalid quotes field') unless quotes.is_a?(Hash)

  usd_quote = quotes['USD']
  raise ArgumentError.new('Missing USD quote info') unless usd_quote
  raise ArgumentError.new('Invalid USD quote info') unless usd_quote.is_a?(Hash)

  @usd_price = usd_quote['price']&.to_f
  @market_cap = usd_quote['market_cap']&.to_f

  if convert_to
    converted_quote = quotes[convert_to.upcase]

    if converted_quote
      raise ArgumentError.new("Invalid #{convert_to.upcase} quote info") unless converted_quote.is_a?(Hash)
      @converted_price = converted_quote['price']&.to_f
    end
  else
    btc_quote = quotes['BTC']

    if btc_quote
      raise ArgumentError.new("Invalid BTC quote info") unless btc_quote.is_a?(Hash)
      @btc_price = btc_quote['price']&.to_f
    end
  end

  timestamp = json['last_updated']
  @last_updated = Time.at(timestamp) if timestamp
end

Instance Attribute Details

#btc_priceObject (readonly)

Returns the value of attribute btc_price.



36
37
38
# File 'lib/cointools/coinmarketcap.rb', line 36

def btc_price
  @btc_price
end

#converted_priceObject (readonly)

Returns the value of attribute converted_price.



36
37
38
# File 'lib/cointools/coinmarketcap.rb', line 36

def converted_price
  @converted_price
end

#last_updatedObject (readonly)

Returns the value of attribute last_updated.



36
37
38
# File 'lib/cointools/coinmarketcap.rb', line 36

def last_updated
  @last_updated
end

#market_capObject (readonly)

Returns the value of attribute market_cap.



36
37
38
# File 'lib/cointools/coinmarketcap.rb', line 36

def market_cap
  @market_cap
end

#rankObject (readonly)

Returns the value of attribute rank.



36
37
38
# File 'lib/cointools/coinmarketcap.rb', line 36

def rank
  @rank
end

#usd_priceObject (readonly)

Returns the value of attribute usd_price.



36
37
38
# File 'lib/cointools/coinmarketcap.rb', line 36

def usd_price
  @usd_price
end