mycoins 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +1 -2
- data/lib/mycoins.rb +99 -32
- data.tar.gz.sig +0 -0
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea88002b9657502cadc020c5b79b6fe502afe54f
|
4
|
+
data.tar.gz: a1c6d6a6052a58145716e88fedf219d56e1d40b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aec15d42a4b70c89f1f1d96afeb3fd32f2d9820814fe2a8f075736318facdb2a40f11b554f3ec8cfb09bde16513b1e6db1f9d6a9768040fd63ff644fa947e5a1
|
7
|
+
data.tar.gz: 00fefa8ebf679cf303bea290ec5c56901d96f779bbdfd830f63053281e611721a7b93ead0a064be84eb766b5f80fba650eb4c1c6336c2a75285cb70b1066e477
|
checksums.yaml.gz.sig
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
E�8�v�N�v��q�l������RЮ���{(�Go�f�.�:X�l�����G�>�i��7��3Hh�!���?ݝ�y&B��U�a�u�T��=� �q��r�Wֻ֥��t��Xkm��nR�RdJ�����?4�As#�6&W���*�$}�r(H�� v�(����D�P�M��������n��ד���z>�J�f��ҲM�{�)��?�Q�"���#��]�m�q�o�Hy�x����{��@�R���FL/���
|
data/lib/mycoins.rb
CHANGED
@@ -8,10 +8,13 @@ require 'cryptocoin_fanboi'
|
|
8
8
|
|
9
9
|
|
10
10
|
class MyCoins
|
11
|
+
|
12
|
+
attr_accessor :mycurrency
|
11
13
|
|
12
|
-
def initialize(source, date: nil, debug: false,
|
14
|
+
def initialize(source, date: nil, debug: false,
|
15
|
+
mycurrency: 'USD', filepath: 'mycoins')
|
13
16
|
|
14
|
-
@debug = debug
|
17
|
+
@debug, @filepath = debug, filepath
|
15
18
|
|
16
19
|
@jer = JustExchangeRates.new(base: 'USD')
|
17
20
|
|
@@ -42,15 +45,86 @@ class MyCoins
|
|
42
45
|
@ccf = CryptocoinFanboi.new(watch: mycoins)
|
43
46
|
|
44
47
|
end
|
48
|
+
|
49
|
+
def archive()
|
50
|
+
|
51
|
+
filepath = File.join(@filepath, Time.now.year.to_s,
|
52
|
+
Time.now.strftime("c%d%m%Y.xml"))
|
53
|
+
FileUtils.mkdir_p File.dirname(filepath)
|
54
|
+
File.write filepath, to_xml
|
55
|
+
|
56
|
+
end
|
45
57
|
|
46
58
|
def to_s()
|
47
59
|
@ccf.to_s
|
48
60
|
end
|
49
61
|
|
62
|
+
# return the value of a coin given either the qty or purchase amount in BTC
|
63
|
+
#
|
64
|
+
def price(coin_name, qty, btc: nil, date: nil)
|
65
|
+
coin = @ccf.find(coin_name)
|
66
|
+
"%.2f %s" % [(coin.price_usd.to_f * qty) * @jer.rate(@mycurrency),
|
67
|
+
@mycurrency]
|
68
|
+
end
|
69
|
+
|
50
70
|
def portfolio()
|
51
71
|
|
72
|
+
r = build_portfolio(@dx.title)
|
73
|
+
format_portfolio(r)
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
def to_xml()
|
78
|
+
|
79
|
+
r = build_portfolio(@dx.title)
|
80
|
+
dx = Dynarex.new
|
81
|
+
dx.import r.records
|
82
|
+
|
83
|
+
h = r.to_h
|
84
|
+
h.delete :records
|
85
|
+
dx.summary.merge!(h)
|
86
|
+
|
87
|
+
dx.to_xml pretty: true
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
def build_portfolio(title)
|
94
|
+
|
95
|
+
if @portfolio and \
|
96
|
+
DateTime.parse(@portfolio.datetime) + 60 > DateTime.now then
|
97
|
+
return @portfolio
|
98
|
+
end
|
99
|
+
|
100
|
+
a = build_records()
|
101
|
+
|
102
|
+
invested = sum(a, :paid)
|
103
|
+
gross_profit_list, losses_list = a.partition {|x| x[:profit].to_f > 0}
|
104
|
+
gross_profit, losses = [gross_profit_list, losses_list]\
|
105
|
+
.map{|x| sum(x, :profit)}
|
106
|
+
|
107
|
+
h = {
|
108
|
+
title: title,
|
109
|
+
mycurrency: @mycurrency,
|
110
|
+
records: a,
|
111
|
+
datetime: Time.now.strftime("%d/%m/%Y at %H:%M%p"),
|
112
|
+
invested: invested,
|
113
|
+
revenue: sum(a, ('value_' + @mycurrency.downcase).to_sym),
|
114
|
+
gross_profit: gross_profit.round(2), losses: losses.round(2),
|
115
|
+
pct_gross_profit: (100 / (invested / gross_profit)).round(2),
|
116
|
+
pct_losses: (100 / (invested / losses)).round(2),
|
117
|
+
net_profit: sum(a, :profit).round(2)
|
118
|
+
}
|
119
|
+
|
120
|
+
@portfolio = OpenStruct.new(h).freeze
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
def build_records()
|
125
|
+
|
52
126
|
# print out the coin name, qty,value in USD,value in Sterling, and BTC
|
53
|
-
|
127
|
+
@dx.all.inject([]) do |r, x|
|
54
128
|
|
55
129
|
puts 'x: ' + x.inspect if @debug
|
56
130
|
coin = @ccf.find(x.title)
|
@@ -63,19 +137,20 @@ class MyCoins
|
|
63
137
|
value_usd = (usd_rate * x.qty.to_f).round(2)
|
64
138
|
|
65
139
|
h = {
|
140
|
+
title: x.title,
|
66
141
|
rank: coin.rank.to_i,
|
67
|
-
name: x.title,
|
68
142
|
qty: x.qty,
|
69
143
|
btc_price: x.btc_price,
|
70
144
|
paid: "%.2f" % paid,
|
71
145
|
value_usd: "%.2f" % value_usd
|
72
146
|
}
|
73
147
|
|
74
|
-
|
75
148
|
mycurrency = if @mycurrency and @mycurrency != 'USD' then
|
76
149
|
|
77
|
-
local_value = ((usd_rate * x.qty.to_f)
|
78
|
-
|
150
|
+
local_value = ((usd_rate * x.qty.to_f) \
|
151
|
+
* @jer.rate(@mycurrency)).round(2)
|
152
|
+
h.merge!(('value_' \
|
153
|
+
+ @mycurrency.downcase).to_sym => "%.2f" % local_value)
|
79
154
|
|
80
155
|
@mycurrency
|
81
156
|
|
@@ -95,43 +170,35 @@ class MyCoins
|
|
95
170
|
r << h.merge!(h2)
|
96
171
|
|
97
172
|
end
|
98
|
-
|
99
|
-
coins = a.sort_by{|x| x[:rank]}.map {|x| x.values}
|
100
173
|
|
101
|
-
|
174
|
+
end
|
175
|
+
|
176
|
+
def format_portfolio(r)
|
177
|
+
|
178
|
+
coins = r.records.sort_by{|x| x[:rank]}.map {|x| x.values}
|
179
|
+
|
180
|
+
labels = %w(Rank Name Qty btc_price) \
|
181
|
+
+ ["paid(#{@mycurrency}):", 'value(USD):']
|
102
182
|
labels << "value(#{@mycurrency}):" if @mycurrency
|
103
183
|
labels += ['Profit:', 'Profit (%):']
|
104
184
|
|
105
185
|
puts 'labels: ' + labels.inspect if @debug
|
186
|
+
|
106
187
|
tf = TableFormatter.new(source: coins, labels: labels)
|
107
188
|
out = "# " + @dx.title + "\n\n"
|
108
|
-
out << "last_updated:
|
189
|
+
out << "last_updated: %s\n\n" % r.datetime
|
109
190
|
out << tf.display
|
110
|
-
|
111
|
-
invested = sum(a, :paid)
|
112
|
-
total = sum(a, ('value_' + @mycurrency.downcase).to_sym)
|
113
|
-
net_profit = sum(a, :profit)
|
114
|
-
|
115
|
-
gross_profit_list, losses_list = a.partition {|x| x[:profit].to_f > 0}
|
116
|
-
|
117
|
-
gross_profit = sum(gross_profit_list, :profit)
|
118
|
-
losses = sum(losses_list, :profit)
|
119
|
-
|
120
|
-
pct_gross_profit = 100 / (invested / gross_profit)
|
121
|
-
pct_losses = 100 / (invested / losses)
|
122
191
|
|
123
|
-
|
124
|
-
out << "\
|
125
|
-
out << "\
|
126
|
-
|
127
|
-
out << "\nLosses: %.2f %s (%%%.2f)" % [losses, @mycurrency, pct_losses]
|
128
|
-
out << "\n\nNet profit: %.2f %s" % [net_profit, @mycurrency]
|
192
|
+
out << "\n\nInvested: %.2f %s" % [r.invested, @mycurrency]
|
193
|
+
out << "\nRevenue: %.2f %s" % [r.revenue, @mycurrency]
|
194
|
+
out << "\n\nGross profit: %.2f %s (%%%.2f)" % \
|
195
|
+
[r.gross_profit, @mycurrency, r.pct_gross_profit]
|
196
|
+
out << "\nLosses: %.2f %s (%%%.2f)" % [r.losses, @mycurrency, r.pct_losses]
|
197
|
+
out << "\n\nNet profit: %.2f %s" % [r.net_profit, @mycurrency]
|
129
198
|
out
|
130
|
-
|
199
|
+
|
131
200
|
end
|
132
201
|
|
133
|
-
private
|
134
|
-
|
135
202
|
def sum(a, field)
|
136
203
|
a.inject(0) {|r, x| r + x[field].to_f }
|
137
204
|
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mycoins
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Robertson
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
sCz5KsiHA9vj8OfrxhYrEb+ML4+J28Umdki7V1vgnQWmnI+Ok2SWlkScgzcmGXF6
|
31
31
|
TJ4=
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2018-01-
|
33
|
+
date: 2018-01-07 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: dynarex
|
metadata.gz.sig
CHANGED
Binary file
|