Class: CryptocoinFanboi

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

Overview

# Examples

## Basic usage

c = CryptocoinFanboi.new

puts.to_s limit: 5 # Display the top 5 coins
puts c.this_week   # Display the top 5 coins this week
puts c.last_day    # Display the top 5 coins in the last 24 hours
puts c.last_hour   # Display the top 5 coins in the last hour

## Advance usage

### Display a selection of coins in order of rank

c = CryptocoinFanboi.new watch: %w(btc xrp eth trx dash xmr neo xem)
puts.to_s

### Ignore possibly risky coins (e.g. Asian stock market coins etc.)

c = CryptocoinFanboi.new ignore: %w(xp bts kcs gxs rhoc)
puts c.this_week

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watch: [], ignore: [], colored: true, debug: false) ⇒ CryptocoinFanboi

Returns a new instance of CryptocoinFanboi.



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
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cryptocoin_fanboi.rb', line 46

def initialize(watch: [], ignore: [], 
               colored: true, debug: false)

  @colored, @debug = colored, debug
  
  @watch= watch.map(&:upcase)
  @ignore = ignore.map(&:upcase)
      
  @fields = %w(rank name price_usd price_btc percent_change_1h 
        percent_change_24h percent_change_7d)

  @year = Time.now.year          
  @labels = %w(Rank Name USD BTC) + ['% 1hr:', '% 24hr:', 
                                     '% 1 week:', '% ' + @year.to_s + ':']
  @coins = fetch_coinlist()
  
  # check for the local cache file containing a record of currency 
  # prices from the start of the year
  
  cache_filename = 'cryptocoin_fanboi.yaml'    
  
  if File.exists? cache_filename then
    
    #load the file
    h = Psych.load File.read(cache_filename)
    puts 'h.key.first: ' + h.keys.first.inspect if @debug
    puts '@year: ' + @year.inspect if @debug
    @coin_prices = (h.keys.first == @year) ? h[@year] : \
        fetch_year_start_prices(@all_coins)
    
  else
    
    # fetch the currency prices from the start of the year
    @coin_prices = fetch_year_start_prices(@all_coins)
    File.write cache_filename, {@year => @coin_prices}.to_yaml
    
  end
  
  @growth = fetch_growth(@coins, @coin_prices)
  puts '@growth: ' + @growth.inspect if @debug    
  
end

Instance Attribute Details

#all_coinsObject (readonly)

Returns the value of attribute all_coins.



43
44
45
# File 'lib/cryptocoin_fanboi.rb', line 43

def all_coins
  @all_coins
end

#coinsObject (readonly)

Returns the value of attribute coins.



43
44
45
# File 'lib/cryptocoin_fanboi.rb', line 43

def coins
  @coins
end

#coloredObject

Returns the value of attribute colored.



44
45
46
# File 'lib/cryptocoin_fanboi.rb', line 44

def colored
  @colored
end

Instance Method Details

#coin_abbreviationsObject Also known as: abbreviations



89
90
91
# File 'lib/cryptocoin_fanboi.rb', line 89

def coin_abbreviations()
  @coins.map {|x| "%s (%s)" % [x['name'], x['symbol']] }
end

#find(name) ⇒ Object



106
107
108
109
110
# File 'lib/cryptocoin_fanboi.rb', line 106

def find(name)

  coins.find {|coin| coin.name =~ /#{name}/i }    

end

#inspectObject



99
100
101
102
103
104
# File 'lib/cryptocoin_fanboi.rb', line 99

def inspect()
  
  c = @coins.inspect.length > 50 ? @coins.inspect[0..50] + '...' : @coins.inspect
  "#<%s:%s @coins=\"%s\ @all_coins=\"...\">" % [self.class, 
                                                self.object_id, c]
end

#now(limit: 5, markdown: false) ⇒ Object Also known as: hour, last_hour

View the coins with the largest gains in the past hour



114
115
116
117
118
# File 'lib/cryptocoin_fanboi.rb', line 114

def now(limit: 5, markdown: false)
  
  build_table2 top_coins('1h', limit: limit), markdown: markdown
  
end

#price(coin, raw_date) ⇒ Object

returns closing price in dollars e.g. price(‘tron’, ‘8th September 2017’) #=> 0.001427



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cryptocoin_fanboi.rb', line 126

def price(coin, raw_date)
  
  date = Chronic.parse(raw_date.gsub('-',' ')).strftime("%Y%m%d")
  puts 'date: ' + date.inspect if @debug
  
  begin
    
    a = Coinmarketcap.get_historical_price(coin, date, date)
    puts 'a: ' + a.inspect if @debug
    r = a.first[:close]
    
  rescue
    puts ($!).inspect if @debug
  end
  
end

#this_week(limit: 5, markdown: false) ⇒ Object Also known as: week

View the coins with the largest gains this week (past 7 days)



145
146
147
148
149
150
# File 'lib/cryptocoin_fanboi.rb', line 145

def this_week(limit: 5, markdown: false)    
  
  coins =  top_coins(limit: limit)
  build_table2 coins, markdown: markdown

end

#to_s(limit: 5, markdown: false) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cryptocoin_fanboi.rb', line 165

def to_s(limit: 5, markdown: false)

  coins = fetch_coinlist(limit: limit).map do |coin|

    @fields.map {|x| coin[x] }

  end    

  puts 'coins: ' + coins.inspect if @debug
  
  build_table coins, markdown: markdown

end

#today(limit: 5, markdown: false) ⇒ Object Also known as: last_day, day

View the coins with the largest gains today (past 24 hours)



156
157
158
159
160
# File 'lib/cryptocoin_fanboi.rb', line 156

def today(limit: 5, markdown: false)
  
  build_table2 top_coins('24h', limit: limit), markdown: markdown
  
end