Class: CoinMarket::Ticker

Inherits:
Object
  • Object
show all
Defined in:
lib/coin_market/ticker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Ticker

Returns a new instance of Ticker


17
18
19
# File 'lib/coin_market/ticker.rb', line 17

def initialize(obj)
  @attributes = obj
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


15
16
17
# File 'lib/coin_market/ticker.rb', line 15

def attributes
  @attributes
end

Class Method Details

.allObject


10
11
12
# File 'lib/coin_market/ticker.rb', line 10

def all
  get(nil)
end

.get(id) ⇒ Object


4
5
6
7
8
# File 'lib/coin_market/ticker.rb', line 4

def get(id)
  response = CoinMarket::Client.new.request('ticker', id)
  tickers = response.map { |obj| CoinMarket::Ticker.new(obj) }
  tickers.length > 1 ? tickers : tickers[0]
end

Instance Method Details

#available_supplyObject


25
# File 'lib/coin_market/ticker.rb', line 25

def available_supply; attributes['available_supply'].to_f; end

#idObject


21
# File 'lib/coin_market/ticker.rb', line 21

def id; attributes['id']; end

#last_updatedObject


57
58
59
# File 'lib/coin_market/ticker.rb', line 57

def last_updated
  Time.at(attributes['last_updated'].to_f)
end

#market_cap(options = {}) ⇒ Object


43
44
45
46
47
48
# File 'lib/coin_market/ticker.rb', line 43

def market_cap(options={})
  currency = options[:currency] || 'usd'
  key = "market_cap_#{currency}"

  attributes.has_key?(key) ? attributes[key].to_f : nil
end

#nameObject


22
# File 'lib/coin_market/ticker.rb', line 22

def name; attributes['name']; end

#percent_change(options = {}) ⇒ Object


50
51
52
53
54
55
# File 'lib/coin_market/ticker.rb', line 50

def percent_change(options={})
  period = options[:period] || '24h'
  key = "percent_change_#{period}"

  attributes.has_key?(key) ? attributes[key].to_f : nil
end

#price(options = {}) ⇒ Object


28
29
30
31
32
33
# File 'lib/coin_market/ticker.rb', line 28

def price(options={})
  currency = options[:currency] || 'usd'
  key = "price_#{currency}"

  attributes.has_key?(key) ? attributes[key].to_f : nil
end

#rankObject


24
# File 'lib/coin_market/ticker.rb', line 24

def rank; attributes['rank'].to_f; end

#symbolObject


23
# File 'lib/coin_market/ticker.rb', line 23

def symbol; attributes['symbol']; end

#total_supplyObject


26
# File 'lib/coin_market/ticker.rb', line 26

def total_supply; attributes['total_supply'].to_f; end

#volume(options = {}) ⇒ Object


35
36
37
38
39
40
41
# File 'lib/coin_market/ticker.rb', line 35

def volume(options={})
  period = options[:period] || '24h'
  currency = options[:currency] || 'usd'
  key = "#{period}_volume_#{currency}"

  attributes.has_key?(key) ? attributes[key].to_f : nil
end