Class: CryptocoinFanboi
- Inherits:
-
Object
- Object
- CryptocoinFanboi
- Includes:
- Colour
- Defined in:
- lib/cryptocoin_fanboi.rb
Instance Attribute Summary collapse
-
#all_coins ⇒ Object
readonly
Returns the value of attribute all_coins.
-
#coins ⇒ Object
readonly
Returns the value of attribute coins.
-
#colored ⇒ Object
Returns the value of attribute colored.
Instance Method Summary collapse
-
#best_coin(default_list = []) ⇒ Object
best coin this year so far.
- #btc_gain(coin) ⇒ Object
- #btc_price(coin = 'Bitcoin', date = nil) ⇒ Object
- #coin(name) ⇒ Object
- #coin_abbreviations ⇒ Object (also: #abbreviations)
- #coin_names(limit: 10) ⇒ Object
- #date_range(raw_d1, raw_d2) ⇒ Object
- #find(name) ⇒ Object
-
#initialize(watch: [], ignore: [], colored: true, debug: false, filepath: '.', exchangerate_key: nil) ⇒ CryptocoinFanboi
constructor
A new instance of CryptocoinFanboi.
- #inspect ⇒ Object
-
#now(limit: 5, markdown: false, rank: :top) ⇒ Object
(also: #hour, #last_hour)
View the coins with the largest gains in the past hour.
-
#price(raw_coin, raw_date = nil) ⇒ Object
returns closing price in dollars e.g.
- #prices_this_year(coin) ⇒ Object
-
#rates(coin = 'Bitcoin', currencies: %w(EUR GBP)) ⇒ Object
returns an array of the prices of Bitcoin in various currencies.
-
#this_week(limit: 5, markdown: false, rank: :top) ⇒ Object
(also: #week)
View the coins with the largest gains this week (past 7 days).
-
#this_year(limit: 5, markdown: false, rank: :top) ⇒ Object
(also: #year)
View the coins with the largest gains this year (since the start of the year).
- #to_html ⇒ Object
- #to_s(limit: 5, markdown: false, rank: :top) ⇒ Object
-
#today(limit: 5, markdown: false, rank: :top) ⇒ Object
(also: #last_day, #day)
View the coins with the largest gains today (past 24 hours).
Methods included from Colour
Constructor Details
#initialize(watch: [], ignore: [], colored: true, debug: false, filepath: '.', exchangerate_key: nil) ⇒ CryptocoinFanboi
Returns a new instance of CryptocoinFanboi.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/cryptocoin_fanboi.rb', line 66 def initialize(watch: [], ignore: [], colored: true, debug: false, filepath: '.', exchangerate_key: nil) @colored, @debug, @filepath, @exchangerate_key = colored, debug, \ filepath, exchangerate_key @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 percent_change_year) @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 = File.join(@filepath, 'cryptocoin_fanboi.yaml') puts 'cache_filename: ' + cache_filename.inspect if @debug @historic_prices_file = File.join(@filepath, 'ccf_historic.yaml') @history_prices = if File.exists? @historic_prices_file then Psych.load File.read(@historic_prices_file) else {} end if File.exists? cache_filename then #load the file h = Psych.load File.read(cache_filename) if @debug then puts 'h.key.first: ' + h.keys.first.inspect puts '@year: ' + @year.inspect end @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 @coins = add_year_growth coins end |
Instance Attribute Details
#all_coins ⇒ Object (readonly)
Returns the value of attribute all_coins.
63 64 65 |
# File 'lib/cryptocoin_fanboi.rb', line 63 def all_coins @all_coins end |
#coins ⇒ Object (readonly)
Returns the value of attribute coins.
63 64 65 |
# File 'lib/cryptocoin_fanboi.rb', line 63 def coins @coins end |
#colored ⇒ Object
Returns the value of attribute colored.
64 65 66 |
# File 'lib/cryptocoin_fanboi.rb', line 64 def colored @colored end |
Instance Method Details
#best_coin(default_list = []) ⇒ Object
best coin this year so far
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/cryptocoin_fanboi.rb', line 127 def best_coin(default_list=[]) list = if default_list.empty? then self.coin_names(limit: 5) else default_list end a2 = list.map {|x| [x, btc_gain(x)]} a2.max_by {|x| x[1].last} end |
#btc_gain(coin) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/cryptocoin_fanboi.rb', line 139 def btc_gain(coin) a = prices_this_year(coin) r = a.min_by {|x | x[1][:btc_price]} [r, btc_price(coin) - r[1][:btc_price]] end |
#btc_price(coin = 'Bitcoin', date = nil) ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/cryptocoin_fanboi.rb', line 148 def btc_price(coin='Bitcoin', date=nil) usd = self.price(coin, date) return usd if coin == 'Bitcoin' puts 'usd: ' + usd.inspect if @debug btc = self.price('bitcoin', date) (usd / btc) end |
#coin(name) ⇒ Object
168 169 170 |
# File 'lib/cryptocoin_fanboi.rb', line 168 def coin(name) self.find(name) end |
#coin_abbreviations ⇒ Object Also known as: abbreviations
158 159 160 |
# File 'lib/cryptocoin_fanboi.rb', line 158 def coin_abbreviations() @coins.map {|x| "%s (%s)" % [x['name'], x['symbol']] } end |
#coin_names(limit: 10) ⇒ Object
164 165 166 |
# File 'lib/cryptocoin_fanboi.rb', line 164 def coin_names(limit: 10) @coins.take(limit).map {|x| x['name']} end |
#date_range(raw_d1, raw_d2) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/cryptocoin_fanboi.rb', line 176 def date_range(raw_d1, raw_d2) d1, d2 = [raw_d1, raw_d2].map do |x| if x.is_a? Date then x else Chronic.parse(x.gsub('-',' '), :context => :past).to_date end end (d1..d2).each do |date| yield(self, date.strftime("%d %B %Y")) end end |
#find(name) ⇒ Object
200 201 202 203 204 205 206 207 208 |
# File 'lib/cryptocoin_fanboi.rb', line 200 def find(name) if @debug then puts 'inside find: name: ' + name.inspect puts 'coins: ' + @coins.inspect end coins.find {|coin| coin.name =~ /#{name}/i } end |
#inspect ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/cryptocoin_fanboi.rb', line 192 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, rank: :top) ⇒ Object Also known as: hour, last_hour
View the coins with the largest gains in the past hour
212 213 214 215 216 |
# File 'lib/cryptocoin_fanboi.rb', line 212 def now(limit: 5, markdown: false, rank: :top) build_table sort_coins('1h', limit: limit, rank: rank), markdown: markdown end |
#price(raw_coin, raw_date = nil) ⇒ Object
returns closing price in dollars e.g. price(‘tron’, ‘8th September 2017’) #=> 0.001427
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/cryptocoin_fanboi.rb', line 224 def price(raw_coin, raw_date=nil) coin = raw_coin.downcase.split.map(&:capitalize).join(' ') return self.coin(coin).price_usd.to_f if raw_date.nil? puts 'raw_date: ' + raw_date.inspect if @debug date = if raw_date.is_a? Date then raw_date.strftime("%Y%m%d") else Chronic.parse(raw_date.gsub('-',' ')).strftime("%Y%m%d") end puts 'date: ' + date.inspect if @debug # if date is today then return today's price if date == Date.today.strftime("%Y%m%d") puts 'same day' if @debug return self.coin(coin).price_usd.to_f end if @history_prices[coin] and @history_prices[coin][date] then @history_prices[coin][date] else begin if @debug then puts 'coin: ' + coin.inspect puts 'date: ' + date.inspect end a = Coinmarketcap.get_historical_price(coin.gsub(/ /,'-'), date, date) puts 'a: ' + a.inspect if @debug r = a.any? ? a.first[:close] : self.coin(coin).price_usd.to_f @history_prices[coin] ||= {} @history_prices[coin][date] = r File.write @historic_prices_file, @history_prices.to_yaml return r rescue puts ($!).inspect if @debug end end end |
#prices_this_year(coin) ⇒ Object
274 275 276 277 278 279 280 281 |
# File 'lib/cryptocoin_fanboi.rb', line 274 def prices_this_year(coin) (Date.parse('1 Jan')..Date.today).map do |date| [date, btc_price: btc_price(coin,date), usd_price: price(coin,date)] end end |
#rates(coin = 'Bitcoin', currencies: %w(EUR GBP)) ⇒ Object
returns an array of the prices of Bitcoin in various currencies
285 286 287 288 289 290 291 292 293 294 |
# File 'lib/cryptocoin_fanboi.rb', line 285 def rates(coin='Bitcoin', currencies: %w(EUR GBP)) jeg = JustExchangeRates.new(base: 'USD', app_id: @exchangerate_key, debug: @debug) usd = self.price(coin).round(2) ([:USD] + currencies.map(&:to_sym)).zip( [usd, *currencies.map {|currency| (usd * jeg.rate(currency)).round(2) }] ).to_h end |
#this_week(limit: 5, markdown: false, rank: :top) ⇒ Object Also known as: week
View the coins with the largest gains this week (past 7 days)
298 299 300 301 302 303 |
# File 'lib/cryptocoin_fanboi.rb', line 298 def this_week(limit: 5, markdown: false, rank: :top) coins = sort_coins(limit: limit, rank: rank) build_table coins, markdown: markdown end |
#this_year(limit: 5, markdown: false, rank: :top) ⇒ Object Also known as: year
View the coins with the largest gains this year (since the start of the year)
310 311 312 313 314 |
# File 'lib/cryptocoin_fanboi.rb', line 310 def this_year(limit: 5, markdown: false, rank: :top) build_table sort_coins('year', limit: limit, rank: rank), markdown: markdown end |
#to_html ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/cryptocoin_fanboi.rb', line 329 def to_html() xpath = (5..8).map {|x| 'tbody/tr/td[' + x.to_s + ']' }.join(' | ') doc = Rexle.new(Kramdown::Document.new(self.to_s(markdown:true)).to_html) doc.root.xpath(xpath).each do |x| x.attributes['class'] = (x.text[0] == '-' ? 'negative' : 'positive') end doc.root.xml end |
#to_s(limit: 5, markdown: false, rank: :top) ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/cryptocoin_fanboi.rb', line 343 def to_s(limit: 5, markdown: false, rank: :top) coins = (fetch_coinlist(limit: limit)) coins2 = add_year_growth(coins) puts 'coins2: ' + coins2.inspect coins3 = coins2.map do |coin| puts 'coin: ' + coin.inspect if @debug @fields.map {|x| coin[x] } end puts 'coins3: ' + coins3.inspect if @debug build_table coins3, markdown: markdown end |
#today(limit: 5, markdown: false, rank: :top) ⇒ Object Also known as: last_day, day
View the coins with the largest gains today (past 24 hours)
320 321 322 323 324 |
# File 'lib/cryptocoin_fanboi.rb', line 320 def today(limit: 5, markdown: false, rank: :top) build_table sort_coins('24h', limit: limit, rank: rank), markdown: markdown end |