Class: CryptocoinFanboi
- Inherits:
-
Object
- Object
- CryptocoinFanboi
- Includes:
- Colour
- Defined in:
- lib/cryptocoin_fanboi.rb
Direct Known Subclasses
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, cmc_apikey: 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.
- #past_60d(limit: 5, markdown: false, rank: :top) ⇒ Object
- #past_90d(limit: 5, markdown: false, rank: :top) ⇒ Object
-
#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_month(limit: 5, markdown: false, rank: :top) ⇒ Object
(also: #month)
View the coins with the largest gains this week (past 7 days).
-
#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).
- #total_market_cap ⇒ Object
Methods included from Colour
Constructor Details
#initialize(watch: [], ignore: [], colored: true, debug: false, filepath: '.', exchangerate_key: nil, cmc_apikey: nil) ⇒ CryptocoinFanboi
Returns a new instance of CryptocoinFanboi.
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cryptocoin_fanboi.rb', line 84 def initialize(watch: [], ignore: [], colored: true, debug: false, filepath: '.', exchangerate_key: nil, cmc_apikey: nil) @colored, @debug, @filepath = colored, debug, filepath @exchangerate_key, @cmc_apikey = exchangerate_key, cmc_apikey puts 'before coinquery' @cq = CoinQuery.new(dym: false, timeout: 7, debug: 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 percent_change_year) pct_fields = %w(1h 24h 7d 30d 60d 90d).map {|x| 'percent_change_' + x} @fields = %w(price) + pct_fields @year = Time.now.year #@labels = %w(Rank Name USD BTC) + ['% 1hr:', '% 24hr:', # '% 1 week:', '% ' + @year.to_s + ':'] @labels = %w(Rank Name USD) + ['% 1hr:', '% 24hr:', '% 7d:','% 30d:','% 60d:','% 90d:'] puts 'about to fetch coinlist'.info if @debug @coins = coins = fetch_coinlist() puts 'coinlist fetched'.info if @debug # The following code is commented out because it's non-essential to # returning the current coin prices. It was intended to show yearly # percentage returns =begin # 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 end |
Instance Attribute Details
#all_coins ⇒ Object (readonly)
Returns the value of attribute all_coins.
81 82 83 |
# File 'lib/cryptocoin_fanboi.rb', line 81 def all_coins @all_coins end |
#coins ⇒ Object (readonly)
Returns the value of attribute coins.
81 82 83 |
# File 'lib/cryptocoin_fanboi.rb', line 81 def coins @coins end |
#colored ⇒ Object
Returns the value of attribute colored.
82 83 84 |
# File 'lib/cryptocoin_fanboi.rb', line 82 def colored @colored end |
Instance Method Details
#best_coin(default_list = []) ⇒ Object
best coin this year so far
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/cryptocoin_fanboi.rb', line 161 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
173 174 175 176 177 178 179 |
# File 'lib/cryptocoin_fanboi.rb', line 173 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
182 183 184 185 186 187 188 189 190 |
# File 'lib/cryptocoin_fanboi.rb', line 182 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
202 203 204 |
# File 'lib/cryptocoin_fanboi.rb', line 202 def coin(name) self.find(name) end |
#coin_abbreviations ⇒ Object Also known as: abbreviations
192 193 194 |
# File 'lib/cryptocoin_fanboi.rb', line 192 def coin_abbreviations() @coins.map {|x| "%s (%s)" % [x['name'], x['symbol']] } end |
#coin_names(limit: 10) ⇒ Object
198 199 200 |
# File 'lib/cryptocoin_fanboi.rb', line 198 def coin_names(limit: 10) @coins.take(limit).map {|x| x['name']} end |
#date_range(raw_d1, raw_d2) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/cryptocoin_fanboi.rb', line 210 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
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/cryptocoin_fanboi.rb', line 234 def find(name) if @debug then puts 'inside find: name: ' + name.inspect puts 'coins: ' + @coins.inspect end coins.find {|coin| coin.name =~ /#{name}/i or coin.symbol =~ /#{name}/i} end |
#inspect ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/cryptocoin_fanboi.rb', line 226 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
247 248 249 250 251 |
# File 'lib/cryptocoin_fanboi.rb', line 247 def now(limit: 5, markdown: false, rank: :top) build_table sort_coins('1h', limit: limit, rank: rank), markdown: markdown end |
#past_60d(limit: 5, markdown: false, rank: :top) ⇒ Object
310 311 312 313 314 315 |
# File 'lib/cryptocoin_fanboi.rb', line 310 def past_60d(limit: 5, markdown: false, rank: :top) coins = sort_coins('60d', limit: limit, rank: rank) build_table coins, markdown: markdown end |
#past_90d(limit: 5, markdown: false, rank: :top) ⇒ Object
317 318 319 320 321 322 |
# File 'lib/cryptocoin_fanboi.rb', line 317 def past_90d(limit: 5, markdown: false, rank: :top) coins = sort_coins('90d', limit: limit, rank: rank) build_table coins, markdown: markdown end |
#price(raw_coin, raw_date = nil) ⇒ Object
returns closing price in dollars e.g. price(‘tron’, ‘8th September 2017’) #=> 0.001427
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/cryptocoin_fanboi.rb', line 259 def price(raw_coin, raw_date=nil) coin = raw_coin.downcase.split.map(&:capitalize).join(' ') return self.coin(coin).quote['USD']['price'].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("%d%m%Y") end puts 'date: ' + date.inspect if @debug # if date is today then return today's price if date == Date.today.strftime("%d%m%Y") puts 'same day' if @debug return self.coin(coin).quote['USD']['price'].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).quote['USD']['price'].to_f price = @cq.historical_price coin, date r = price ? price.to_f : self.coin(coin).quote['USD']['price'].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
325 326 327 328 329 330 331 332 |
# File 'lib/cryptocoin_fanboi.rb', line 325 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
336 337 338 339 340 341 342 343 344 345 |
# File 'lib/cryptocoin_fanboi.rb', line 336 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_month(limit: 5, markdown: false, rank: :top) ⇒ Object Also known as: month
View the coins with the largest gains this week (past 7 days)
360 361 362 363 364 365 366 |
# File 'lib/cryptocoin_fanboi.rb', line 360 def this_month(limit: 5, markdown: false, rank: :top) puts 'inside this_mponth'.info if @debug coins = sort_coins('30d', limit: limit, rank: rank) build_table coins, markdown: markdown 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)
349 350 351 352 353 354 |
# File 'lib/cryptocoin_fanboi.rb', line 349 def this_week(limit: 5, markdown: false, rank: :top) coins = sort_coins('7d', 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)
374 375 376 377 378 |
# File 'lib/cryptocoin_fanboi.rb', line 374 def this_year(limit: 5, markdown: false, rank: :top) build_table sort_coins('year', limit: limit, rank: rank), markdown: markdown end |
#to_html ⇒ Object
400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/cryptocoin_fanboi.rb', line 400 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
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/cryptocoin_fanboi.rb', line 414 def to_s(limit: 5, markdown: false, rank: :top) coins = (fetch_coinlist(limit: limit)) coins2 = add_year_growth(coins) puts ('coins2: ' + coins2.inspect).debug if @debg coins3 = coins2.map do |coin| puts ('coin: ' + coin.inspect).debug if @debug a2 = %w(cmc_rank name).map {|x| coin[x]} puts 'a2: ' + a2.inspect a3 = @fields.map {|x| coin['quote']['USD'][x].to_f.round(2) } puts 'a3: ' + a3.inspect a2 + a3 end puts ('coins3: ' + coins3.inspect).debug 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)
384 385 386 387 388 |
# File 'lib/cryptocoin_fanboi.rb', line 384 def today(limit: 5, markdown: false, rank: :top) build_table sort_coins('24h', limit: limit, rank: rank), markdown: markdown end |
#total_market_cap ⇒ Object
393 394 395 396 397 398 |
# File 'lib/cryptocoin_fanboi.rb', line 393 def total_market_cap() '$' + Coin360Api21::Global.new.latest.total_market_cap.round.to_s\ .reverse.gsub(/...(?=.)/,'\&,').reverse end |