cns 0.1.1 → 0.1.2

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.
@@ -4,12 +4,12 @@ require('bigdecimal/util')
4
4
 
5
5
  # @author Hernani Rodrigues Vaz
6
6
  module Cns
7
- # classe para processar saldos & transacoes ledger
7
+ # classe para processar transacoes ledger do paymium
8
8
  class Paymium
9
9
  # @return [Apius] API paymium
10
10
  attr_reader :api
11
11
  # @return [Array<Hash>] todos os dados bigquery
12
- attr_reader :dbq
12
+ attr_reader :bqd
13
13
  # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
14
14
  attr_reader :ops
15
15
 
@@ -20,36 +20,48 @@ module Cns
20
20
  # @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas?
21
21
  # @return [Paymium] API paymium - obter saldos & transacoes ledger
22
22
  def initialize(dad, pop)
23
- # API paymium base
24
- @api = Apifr.new
25
- @dbq = dad
23
+ @api = Apice.new
24
+ @bqd = dad
26
25
  @ops = pop
27
26
  end
28
27
 
28
+ # @return [Array<Hash>] lista ledger paymium novos
29
+ def ledger
30
+ @ledger ||= exd[:kl].map { |h| h[:account_operations].select { |o| kyl.include?(o[:uuid]) } }.flatten
31
+ end
32
+
33
+ # @return [String] texto saldos & transacoes & ajuste dias
34
+ def mostra_resumo
35
+ puts("\nPAYMIUM\ntipo paymium bigquery")
36
+ puts(formata_saldos(:btc))
37
+ puts(formata_saldos(:eur))
38
+ mostra_totais
39
+
40
+ mostra_ledger
41
+ return unless ledger.count.positive?
42
+
43
+ puts("\nstring ajuste dias da ledger\n-h=#{kyl.map { |e| "#{e}:0" }.join(' ')}")
44
+ end
45
+
29
46
  # @return [Hash] dados exchange paymium - saldos & transacoes ledger
30
47
  def exd
31
48
  @exd ||= {
32
- sl: api.account,
33
- kl: api.ledger
49
+ sl: api.account_fr,
50
+ kl: api.ledger_fr
34
51
  }
35
52
  end
36
53
 
37
- # @return [Array<String>] lista txid de transacoes ledger
54
+ # @return [Array<String>] lista txid dos ledger novos
38
55
  def kyl
39
56
  @kyl ||= exd[:kl].map { |h| h[:account_operations].map { |o| o[:uuid] } }.flatten -
40
- (ops[:t] ? [] : dbq[:nl].map { |e| e[:txid] })
57
+ (ops[:t] ? [] : bqd[:nl].map { |e| e[:txid] })
41
58
  end
42
59
 
43
- # @return [Hash] transacoes ledger
44
- def ledger
45
- @ledger ||= exd[:kl].map { |h| h[:account_operations].select { |o| kyl.include?(o[:uuid]) } }.flatten
46
- end
47
-
48
- # @example (see Apifr#account)
60
+ # @example (see Apice#account_fr)
49
61
  # @param [Symbol] bqm symbol paymium da moeda
50
- # @return [String] texto formatado saldos (paymium/bigquery) & iguais/ok/nok?
62
+ # @return [String] texto formatado saldos
51
63
  def formata_saldos(bqm)
52
- b = dbq[:sl][bqm].to_d
64
+ b = bqd[:sl][bqm].to_d
53
65
  t = exd[:sl]["balance_#{bqm}".to_sym].to_d
54
66
  format(
55
67
  '%<mo>-5.5s %<kr>21.9f %<bq>21.9f %<ok>3.3s',
@@ -60,9 +72,9 @@ module Cns
60
72
  )
61
73
  end
62
74
 
63
- # @example (see Apifr#ledger)
75
+ # @example (see Apice#ledger_fr)
64
76
  # @param (see Bigquery#frl_val)
65
- # @return [String] texto formatado transacao ledger
77
+ # @return [String] texto formatado ledger
66
78
  def formata_ledger(hlx)
67
79
  format(
68
80
  '%<ky>-18.18s %<dt>19.19s %<ty>-17.17s %<mo>-4.4s %<vl>18.7f',
@@ -74,32 +86,27 @@ module Cns
74
86
  )
75
87
  end
76
88
 
77
- # @example (see Apifr#ledger)
78
- # @param [String] uid identificacor da ledger apifr
89
+ # @param [String] uid identificacor da ledger
79
90
  # @param [Integer] max chars a mostrar
80
- # @return [String] texto formatado identificacor da ledger apifr
91
+ # @return [String] texto formatado identificacor da ledger
81
92
  def formata_uuid(uid, max)
82
93
  i = Integer(max / 2)
83
94
  max < 7 ? 'erro' : "#{uid[0, i]}#{uid[-i..]}"
84
95
  end
85
96
 
86
- # @return [String] texto saldos & transacoes & ajuste dias
87
- def mostra_resumo
88
- puts("\nPAYMIUM\nmoeda saldo paymium saldo bigquery")
89
- puts(formata_saldos(:btc))
90
- puts(formata_saldos(:eur))
91
-
92
- mostra_ledger
93
- return unless ledger.count.positive?
97
+ # @return [String] texto totais numero de transacoes
98
+ def mostra_totais
99
+ c = exd[:kl].map { |h| h[:account_operations].count }.flatten.inject(:+)
100
+ d = bqd[:nl].count
94
101
 
95
- puts("\nstring ajuste dias da ledger\n-h=#{kyl.map { |e| "#{e}:0" }.join(' ')}")
102
+ puts("LEDGER #{format('%<c>20i %<d>21i %<o>3.3s', c: c, d: d, o: c == d ? 'OK' : 'NOK')}")
96
103
  end
97
104
 
98
105
  # @return [String] texto transacoes ledger
99
106
  def mostra_ledger
100
107
  return unless ops[:v] && ledger.count.positive?
101
108
 
102
- puts("\nledger data hora tipo moeda -------quantidade")
109
+ puts("\nledger data hora tipo moeda quantidade")
103
110
  ledger.sort { |a, b| b[:created_at_int] <=> a[:created_at_int] }.each { |o| puts(formata_ledger(o)) }
104
111
  end
105
112
  end
@@ -4,12 +4,12 @@ require('bigdecimal/util')
4
4
 
5
5
  # @author Hernani Rodrigues Vaz
6
6
  module Cns
7
- # classe para processar saldos & transacoes ledger
7
+ # classe para processar transacoes ledger do therock
8
8
  class TheRock
9
9
  # @return [Apius] API therock
10
10
  attr_reader :api
11
11
  # @return [Array<Hash>] todos os dados bigquery
12
- attr_reader :dbq
12
+ attr_reader :bqd
13
13
  # @return [Thor::CoreExt::HashWithIndifferentAccess] opcoes trabalho
14
14
  attr_reader :ops
15
15
 
@@ -20,35 +20,46 @@ module Cns
20
20
  # @option pop [Boolean] :t (false) mostra transacoes todas ou somente novas?
21
21
  # @return [TheRock] API therock - obter saldos & transacoes ledger
22
22
  def initialize(dad, pop)
23
- # API therock base
24
- @api = Apimt.new
25
- @dbq = dad
23
+ @api = Apice.new
24
+ @bqd = dad
26
25
  @ops = pop
27
26
  end
28
27
 
28
+ # @return [Array<Hash>] lista ledger therock novos
29
+ def ledger
30
+ @ledger ||= exd[:kl].select { |o| kyl.include?(o[:id]) }
31
+ end
32
+
33
+ # @return [String] texto saldos & transacoes & ajuste dias
34
+ def mostra_resumo
35
+ puts("\nTHEROCK\ntipo therock bigquery")
36
+ exd[:sl].each { |h| puts(formata_saldos(h)) }
37
+ mostra_totais
38
+
39
+ mostra_ledger
40
+ return unless ledger.count.positive?
41
+
42
+ puts("\nstring ajuste dias da ledger\n-h=#{kyl.map { |e| "#{e}:0" }.join(' ')}")
43
+ end
44
+
29
45
  # @return [Hash] dados exchange therock - saldos & transacoes ledger
30
46
  def exd
31
47
  @exd ||= {
32
- sl: api.account,
33
- kl: api.ledger
48
+ sl: api.account_mt,
49
+ kl: api.ledger_mt
34
50
  }
35
51
  end
36
52
 
37
- # @return [Array<String>] lista txid de transacoes ledger
53
+ # @return [Array<String>] lista txid dos ledger novos
38
54
  def kyl
39
- @kyl ||= exd[:kl].map { |h| h[:id] } - (ops[:t] ? [] : dbq[:nl].map { |e| e[:txid] })
40
- end
41
-
42
- # @return [Hash] transacoes ledger
43
- def ledger
44
- @ledger ||= exd[:kl].select { |o| kyl.include?(o[:id]) }
55
+ @kyl ||= exd[:kl].map { |h| h[:id] } - (ops[:t] ? [] : bqd[:nl].map { |e| e[:txid] })
45
56
  end
46
57
 
47
- # @example (see Apimt#account)
58
+ # @example (see Apice#account_mt)
48
59
  # @param [Hash] hsl saldo therock da moeda
49
- # @return [String] texto formatado saldos (therock/bigquery) & iguais/ok/nok?
60
+ # @return [String] texto formatado saldos
50
61
  def formata_saldos(hsl)
51
- b = dbq[:sl][hsl[:currency].downcase.to_sym].to_d
62
+ b = bqd[:sl][hsl[:currency].downcase.to_sym].to_d
52
63
  k = hsl[:balance].to_d
53
64
  format(
54
65
  '%<mo>-5.5s %<kr>21.9f %<bq>21.9f %<ok>3.3s',
@@ -59,9 +70,9 @@ module Cns
59
70
  )
60
71
  end
61
72
 
62
- # @example (see Apimt#ledger)
73
+ # @example (see Apice#ledger_mt)
63
74
  # @param (see Bigquery#mtl_val1)
64
- # @return [String] texto formatado transacao ledger
75
+ # @return [String] texto formatado ledger
65
76
  def formata_ledger(hlx)
66
77
  format(
67
78
  '%<ky>6i %<dt>19.19s %<ty>-27.27s %<mo>-4.4s %<vl>20.7f',
@@ -73,15 +84,12 @@ module Cns
73
84
  )
74
85
  end
75
86
 
76
- # @return [String] texto saldos & transacoes & ajuste dias
77
- def mostra_resumo
78
- puts("\nTHEROCK\nmoeda saldo therock saldo bigquery")
79
- exd[:sl].each { |h| puts(formata_saldos(h)) }
80
-
81
- mostra_ledger
82
- return unless ledger.count.positive?
87
+ # @return [String] texto totais numero de transacoes
88
+ def mostra_totais
89
+ c = exd[:kl].count
90
+ d = bqd[:nl].count
83
91
 
84
- puts("\nstring ajuste dias da ledger\n-h=#{kyl.map { |e| "#{e}:0" }.join(' ')}")
92
+ puts("LEDGER #{format('%<c>20i %<d>21i %<o>3.3s', c: c, d: d, o: c == d ? 'OK' : 'NOK')}")
85
93
  end
86
94
 
87
95
  # @return [String] texto transacoes ledger
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cns
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hernâni Rodrigues Vaz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-17 00:00:00.000000000 Z
11
+ date: 2020-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -131,12 +131,9 @@ files:
131
131
  - cns.gemspec
132
132
  - exe/cns
133
133
  - lib/cns.rb
134
- - lib/cns/apide.rb
135
- - lib/cns/apies.rb
136
- - lib/cns/apifr.rb
137
- - lib/cns/apigm.rb
138
- - lib/cns/apimt.rb
139
- - lib/cns/apius.rb
134
+ - lib/cns/apibc.rb
135
+ - lib/cns/apice1.rb
136
+ - lib/cns/apice2.rb
140
137
  - lib/cns/bigquery1.rb
141
138
  - lib/cns/bigquery2.rb
142
139
  - lib/cns/bigquery3.rb
@@ -1,240 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require('openssl')
4
- require('base64')
5
- require('curb')
6
- require('json')
7
-
8
- # @author Hernani Rodrigues Vaz
9
- module Cns
10
- # classe para processar dados no bitcoinde
11
- class Apide
12
- # @return [String] API key
13
- attr_reader :aky
14
- # @return [String] API secret
15
- attr_reader :asc
16
- # @return [String] API url base
17
- attr_reader :urb
18
-
19
- # @param [String] pky API key
20
- # @param [String] psc API secret
21
- # @param [Hash] ops parametrizacao base da API
22
- # @return [Apide] API bitcoinde base
23
- def initialize(
24
- pky: ENV['BITCOINDE_API_KEY'],
25
- psc: ENV['BITCOINDE_API_SECRET'],
26
- ops: { www: 'https://api.bitcoin.de', ver: 4 }
27
- )
28
- @aky = pky
29
- @asc = psc
30
- @urb = "#{ops[:www]}/v#{ops[:ver]}"
31
- end
32
-
33
- # @example
34
- # {
35
- # data: {
36
- # balances: {
37
- # btc: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
38
- # bch: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
39
- # btg: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
40
- # eth: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
41
- # bsv: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' },
42
- # ltc: { total_amount: '0.00000000000000000000', available_amount: '0', reserved_amount: '0' }
43
- # },
44
- # encrypted_information: { uid: '0y...', bic_short: '0y...', bic_full: '0y...' }
45
- # },
46
- # errors: [],
47
- # credits: 23
48
- # }
49
- # @return [Hash] saldos no bitcoinde
50
- def account
51
- api_get('account')[:data][:balances]
52
- end
53
-
54
- # @example
55
- # {
56
- # trades: [{
57
- # trade_id: 'XUWWD3',
58
- # trading_pair: 'btceur',
59
- # is_external_wallet_trade: false,
60
- # type: 'sell',
61
- # amount_currency_to_trade: '0.1',
62
- # price: 430,
63
- # volume_currency_to_pay: 43,
64
- # volume_currency_to_pay_after_fee: 42.79,
65
- # amount_currency_to_trade_after_fee: 0.099,
66
- # fee_currency_to_pay: 0.22,
67
- # fee_currency_to_trade: '0.00100000',
68
- # created_at: '2014-03-22T08:14:48+01:00',
69
- # successfully_finished_at: '2014-03-25T14:03:22+01:00',
70
- # state: 1,
71
- # is_trade_marked_as_paid: true,
72
- # trade_marked_as_paid_at: '2014-03-22T08:20:01+01:00',
73
- # payment_method: 1,
74
- # my_rating_for_trading_partner: 'positive',
75
- # trading_partner_information: {
76
- # username: 'emax2000',
77
- # is_kyc_full: false,
78
- # trust_level: 'bronze',
79
- # amount_trades: 4,
80
- # rating: 100,
81
- # bank_name: 'CASSA DI RISPARMIO DI CIVITAVECCHIA SPA',
82
- # bic: 'CRFIIT2CXXX',
83
- # seat_of_bank: 'IT'
84
- # }
85
- # }, {}],
86
- # page: { current: 1, last: 2 },
87
- # errors: [],
88
- # credits: 22
89
- # }
90
- # @param [Integer] pag pagina dos dados a obter
91
- # @param [Array] ary lista acumuladora dos dados a obter
92
- # @return [Array<Hash>] lista trades no bitcoinde
93
- def trades(pag = 0, ary = [])
94
- r = api_get('trades', state: 1, page: pag + 1)
95
- ary += r[:trades]
96
- r[:page][:current] < r[:page][:last] ? trades(pag + 1, ary) : ary
97
- rescue StandardError
98
- ary
99
- end
100
-
101
- # @example
102
- # {
103
- # deposits: [{}, {}],
104
- # page: { current: 1, last: 1 },
105
- # errors: [],
106
- # credits: 23
107
- # }
108
- # @param (see trades)
109
- # @return [Array<Hash>] lista depositos no bitcoinde
110
- def deposits(pag = 0, ary = [])
111
- r = api_get('btc/deposits', state: 2, page: pag + 1)
112
- ary += r[:deposits].map { |h| deposit_hash(h) }
113
- r[:page][:current] < r[:page][:last] ? deposits(pag + 1, ary) : ary
114
- rescue StandardError
115
- ary
116
- end
117
-
118
- # @example
119
- # {
120
- # withdrawals: [{}, {}],
121
- # page: { current: 1, last: 2 },
122
- # errors: [],
123
- # credits: 23
124
- # }
125
- # @param (see trades)
126
- # @return [Array<Hash>] lista withdrawals no bitcoinde
127
- def withdrawals(pag = 0, ary = [])
128
- r = api_get('btc/withdrawals', state: 1, page: pag + 1)
129
- ary += r[:withdrawals].map { |h| withdrawal_hash(h) }
130
- r[:page][:current] < r[:page][:last] ? withdrawals(pag + 1, ary) : ary
131
- rescue StandardError
132
- ary
133
- end
134
-
135
- # @example
136
- # {
137
- # withdrawal_id: '136605',
138
- # address: '1K9YMDDrmMV25EoYNqi7KUEK57Kn3TCNUJ',
139
- # amount: '0.120087',
140
- # network_fee: '0',
141
- # comment: '',
142
- # created_at: '2014-02-05T13:01:09+01:00',
143
- # txid: '6264fe528116fcb87c812a306ca8409eecfec8fa941546c86f98984b882c8042',
144
- # transferred_at: '2014-02-05T13:05:17+01:00',
145
- # state: 1
146
- # }
147
- # @param [Hash] hwi dados duma withdrawal
148
- # @return [Hash] withdrawal unifirmizada
149
- def withdrawal_hash(hwi)
150
- {
151
- id: hwi[:address],
152
- tp: 'out',
153
- qtxt: 'btc',
154
- fee: hwi[:network_fee],
155
- time: Time.parse(hwi[:transferred_at]),
156
- qt: hwi[:amount],
157
- lgid: Integer(hwi[:withdrawal_id])
158
- }
159
- end
160
-
161
- # @example
162
- # {
163
- # deposit_id: '177245',
164
- # txid: '84f9e85bc5709cd471e3d58a7d0f42d2c4a7bbd888cabf844e200efbf0a7fda2',
165
- # address: '1KK6HhG3quojFS4CY1mPcbyrjQ8BMDQxmT',
166
- # amount: '0.13283',
167
- # confirmations: 6,
168
- # state: 2,
169
- # created_at: '2014-01-31T22:01:30+01:00'
170
- # }
171
- # @param [Hash] hde dados dum deposit
172
- # @return [Hash] deposit uniformizado
173
- def deposit_hash(hde)
174
- {
175
- id: hde[:address],
176
- tp: 'deposit',
177
- qtxt: 'btc',
178
- fee: '0',
179
- time: Time.parse(hde[:created_at]),
180
- qt: hde[:amount],
181
- lgid: Integer(hde[:deposit_id])
182
- }
183
- end
184
-
185
- private
186
-
187
- # HTTP GET request for public bitcoinde API queries.
188
- def api_get(uri, **ops)
189
- t = url("#{urb}/#{uri}", ops)
190
- resposta(Curl.get(t) { |r| r.headers = hdrs('GET', t, nonce, {}) })
191
- end
192
-
193
- # HTTP POST request for private bitcoinde API queries involving user credentials.
194
- def api_post(uri, **ops)
195
- # pedidos POST HTTP nao levam parametros no URL - somente no header e ordenados
196
- resposta(Curl.post("#{urb}/#{uri}") { |r| r.headers = hdrs('POST', "#{urb}/#{uri}", nonce, ops.sort) })
197
- end
198
-
199
- # @return [String] URL do pedido formatado com todos os parametros
200
- def url(uri, ops)
201
- ops.empty? ? uri : "#{uri}?#{URI.encode_www_form(ops)}"
202
- end
203
-
204
- # @return [Hash] headers necessarios para pedido HTTP
205
- def hdrs(typ, qry, non, ops)
206
- {
207
- 'X-API-KEY': aky,
208
- 'X-API-NONCE': non,
209
- 'X-API-SIGNATURE': auth(typ, qry, non, URI.encode_www_form(ops))
210
- }
211
- end
212
-
213
- # @return [String] assinarura codificada dos pedidos HTTP
214
- def auth(typ, qry, non, par)
215
- raise(ArgumentError, 'API Key is not set') unless aky
216
- raise(ArgumentError, 'API Secret is not set') unless asc
217
-
218
- OpenSSL::HMAC.hexdigest('sha256', asc, [typ, qry, aky, non, Digest::MD5.hexdigest(par)].join('#'))
219
- end
220
-
221
- # @return [Integer] continually-increasing unsigned integer nonce from the current Unix Time
222
- def nonce
223
- Integer(Float(Time.now) * 1e6)
224
- end
225
-
226
- # @return [Hash] resposta do pedido HTTP
227
- def resposta(http)
228
- http.response_code == 200 ? JSON.parse(http.body, symbolize_names: true) : http.status
229
- rescue JSON::ParserError,
230
- EOFError,
231
- Errno::ECONNRESET,
232
- Errno::EINVAL,
233
- Net::HTTPBadResponse,
234
- Net::HTTPHeaderSyntaxError,
235
- Net::ProtocolError,
236
- Timeout::Error => e
237
- "Erro da API bitcoinde #{e.inspect}"
238
- end
239
- end
240
- end