cryptocoin_fanboi 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b01a628c230f7ecb7ba8d3a55a2f972ef0afcacb
4
- data.tar.gz: 00b0c8d8f6d6c223d3b9749bcd708aad41290771
3
+ metadata.gz: 561d34afff8a3d49fe5687ffb831ae060c42d677
4
+ data.tar.gz: e25930d3382809ef3c945942b862758f54340438
5
5
  SHA512:
6
- metadata.gz: 19d0075ef5991cbf4153fd4d8d5fb400fb7fcde152d0fc604214f31fa13ddb8848d21f49bdb34a160772a6d016968f7c926b7bc4022f09f4076ec6194a179499
7
- data.tar.gz: 07ccb00344da653e5fccbf41522eef5679a6093bc621ae9e8fc65ce110f1d463e5738029678aea32d7403ac8a350c7096b04057a7a7895158e543ce4e40d1a5a
6
+ metadata.gz: 6dc393507855992feee85f77da2fb96ac3f1a35aca21d7a1f925d6e616a795b69b95db5c7b16ce7f0807cc0f3552752c1696d8cd083693025a53be0beccaa281
7
+ data.tar.gz: d1050779993531dc93e946dc9718c9481d3a35c9b644cf6bd582c721008afdbd314ec4689c1d891a1ffec06956d5cb775f7a3ea88922317afb5792702a52a418
Binary file
data.tar.gz.sig CHANGED
@@ -1,3 +1,3 @@
1
- qy�IJǛ���gk��[c�;E�,t|n��V X�0=+p*��V�oO����;Ta'[�%B�Q����0!EV�%��8��pnM�����KC7��&f�s�#�8
2
- �g�o�x�pK��=�3��si�G�i���ճ�n{ ������ᑃD)!��V�է7�J��..�
3
- BJB�����IK�~�H[�F�-�C��ni�S�5�GD�b�?�]�q$�=�ۉ���]��� W��H�ma�xQ
1
+ 3O1F�d?����T4Oj�^�mX��.�R��:i4�#N�Qob���c>4sXUX���m�LԺf���w
2
+ p����*
3
+ 7a��
@@ -3,6 +3,7 @@
3
3
  # file: cryptocoin_fanboi.rb
4
4
 
5
5
 
6
+ require 'psych'
6
7
  require 'colored'
7
8
  require 'coinmarketcap'
8
9
  require 'table-formatter'
@@ -10,20 +11,44 @@ require 'table-formatter'
10
11
 
11
12
  class CryptocoinFanboi
12
13
 
13
- attr_reader :coins
14
+ attr_reader :coins, :all_coins
14
15
  attr_accessor :colored
15
16
 
16
- def initialize(watch: [], colored: true)
17
+ def initialize(watch: [], ignore: [], colored: true, debug: false)
17
18
 
18
- @colored = colored
19
- @watch = watch.map(&:upcase)
19
+ @colored, @debug = colored, debug
20
+ @watch= watch.map(&:upcase)
21
+ @ignore = ignore.map(&:upcase)
20
22
 
21
23
  @fields = %w(rank name price_usd price_btc percent_change_1h
22
24
  percent_change_24h percent_change_7d)
23
-
24
- @labels = %w(Rank Name USD BTC) + ['% 1hr:', '% 24hr:', '% 1 week:']
25
- @coins = fetch_coinlist(watch: @watch)
26
25
 
26
+ @year = Time.now.year.to_s
27
+ @labels = %w(Rank Name USD BTC) + ['% 1hr:', '% 24hr:',
28
+ '% 1 week:', '% ' + @year + ':']
29
+ @coins = fetch_coinlist()
30
+
31
+
32
+ # check for the local cache file containing a record of currency
33
+ # prices from the start of the year
34
+
35
+ cache_filename = 'cryptocoin_fanboi.yaml'
36
+
37
+ if File.exists? cache_filename then
38
+
39
+ #load the file
40
+ h = Psych.load File.read(cache_filename)
41
+
42
+ @growth = (h.keys.first == @year.to_i) ? h[@year.to_i] : fetch_growth(@all_coins)
43
+
44
+ else
45
+
46
+ # fetch the currency prices from the start of the year
47
+ @growth = fetch_growth(@all_coins)
48
+ File.write cache_filename, {@year => @growth}.to_yaml
49
+
50
+ end
51
+
27
52
  end
28
53
 
29
54
  def coin_abbreviations()
@@ -59,9 +84,9 @@ class CryptocoinFanboi
59
84
 
60
85
  end
61
86
 
62
- def to_s(limit: nil, markdown: false, watch: @watch)
87
+ def to_s(limit: nil, markdown: false)
63
88
 
64
- coins = fetch_coinlist(limit: limit, watch: watch).map do |coin|
89
+ coins = fetch_coinlist(limit: limit).map do |coin|
65
90
 
66
91
  @fields.map {|x| coin[x] }
67
92
 
@@ -73,8 +98,13 @@ class CryptocoinFanboi
73
98
 
74
99
  private
75
100
 
76
- def build_table(coins, markdown: markdown)
77
-
101
+ def build_table(a, markdown: markdown)
102
+
103
+ coins = a.map do |x|
104
+ @growth.has_key?(x[1]) ? x + [@growth[x[1]].to_s] : x
105
+ end
106
+
107
+ puts 'coins: ' + coins.inspect if @debug
78
108
  s = TableFormatter.new(source: coins, labels: @labels, markdown: markdown)\
79
109
  .display
80
110
 
@@ -94,11 +124,46 @@ class CryptocoinFanboi
94
124
 
95
125
  end
96
126
 
97
- def fetch_coinlist(limit: nil, watch: [])
127
+ def fetch_coinlist(limit: nil)
128
+
129
+ @all_coins = JSON.parse(Coinmarketcap.coins.body)
130
+
131
+ a = if @watch.any? then
132
+ @all_coins.select {|x| @watch.include? x['symbol']}
133
+ elsif @ignore.any?
134
+ @all_coins.reject {|x| @ignore.include? x['symbol']}
135
+ else
136
+ @all_coins
137
+ end
138
+
139
+ limit ? a.take(limit) : a
140
+
141
+ end
142
+
143
+ # fetch the currency prices from the start of the year
144
+ #
145
+ def fetch_growth(coins)
98
146
 
99
- a = JSON.parse(Coinmarketcap.coins.body)
100
- coins = watch.any? ? a.select {|x| watch.include? x['symbol']} : a
101
- limit ? coins.take(limit) : coins
147
+ puts 'fetching growth ...' if @debug
148
+
149
+ coins.inject({}) do |r, x|
150
+
151
+ day1 = @year + '0101'
152
+ puts 'x: ' + x['name'].inspect if @debug
153
+ begin
154
+ a = Coinmarketcap.get_historical_price(x['name'].gsub(/ /,'-'), day1, day1)
155
+ rescue
156
+ puts 'warning : ' + x['name'].inspect + ' ' + ($!).inspect
157
+ end
158
+
159
+ if a and a.any? then
160
+ latest_day, year_start = x['price_usd'].to_f, a[0][:close]
161
+ r.merge({x['name'] => (latest_day / year_start).round(2)})
162
+ else
163
+ r
164
+ end
165
+
166
+ end
102
167
 
103
168
  end
104
169
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cryptocoin_fanboi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -30,7 +30,7 @@ cert_chain:
30
30
  NgmsFfw10oaD3PWQ7UPxWJP14au2LUL+NGHkLrGHOMPphsZhYeXKugGO8NfcKukq
31
31
  fps=
32
32
  -----END CERTIFICATE-----
33
- date: 2018-01-02 00:00:00.000000000 Z
33
+ date: 2018-01-03 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: colored
metadata.gz.sig CHANGED
Binary file