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
# 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.to_s          
  @labels = %w(Rank Name USD BTC) + ['% 1hr:', '% 24hr:', 
                                     '% 1 week:', '% ' + @year + ':']
  @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
    @growth = (h.keys.first == @year) ? h[@year] : fetch_growth(@all_coins)
    puts '@growth: ' + @growth.inspect if @debug
    
  else
    
    # fetch the currency prices from the start of the year
    @growth = fetch_growth(@all_coins)      
    File.write cache_filename, {@year => @growth}.to_yaml
    
  end
  
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



86
87
88
# File 'lib/cryptocoin_fanboi.rb', line 86

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

#find(name) ⇒ Object



103
104
105
106
107
# File 'lib/cryptocoin_fanboi.rb', line 103

def find(name)

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

end

#inspectObject



96
97
98
99
100
101
# File 'lib/cryptocoin_fanboi.rb', line 96

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



111
112
113
114
115
# File 'lib/cryptocoin_fanboi.rb', line 111

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

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

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



122
123
124
125
126
127
# File 'lib/cryptocoin_fanboi.rb', line 122

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



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cryptocoin_fanboi.rb', line 142

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)



133
134
135
136
137
# File 'lib/cryptocoin_fanboi.rb', line 133

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