Class: Cns::TheRock

Inherits:
Object
  • Object
show all
Defined in:
lib/cns/therock.rb

Overview

classe para processar transacoes ledger do therock

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dad, pop) ⇒ TheRock

Returns API therock - obter saldos & transacoes ledger.

Parameters:

  • dad (Hash)

    todos os dados bigquery

  • pop (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho

Options Hash (pop):

  • :h (Hash) — default: {}

    configuracao dias ajuste reposicionamento temporal

  • :v (Boolean) — default: false

    mostra dados transacoes trades & ledger?

  • :t (Boolean) — default: false

    mostra transacoes todas ou somente novas?



22
23
24
25
26
# File 'lib/cns/therock.rb', line 22

def initialize(dad, pop)
  @api = Apice.new
  @bqd = dad
  @ops = pop
end

Instance Attribute Details

#apiApius (readonly)

Returns API therock.

Returns:

  • (Apius)

    API therock



10
11
12
# File 'lib/cns/therock.rb', line 10

def api
  @api
end

#bqdArray<Hash> (readonly)

Returns todos os dados bigquery.

Returns:

  • (Array<Hash>)

    todos os dados bigquery



12
13
14
# File 'lib/cns/therock.rb', line 12

def bqd
  @bqd
end

#opsThor::CoreExt::HashWithIndifferentAccess (readonly)

Returns opcoes trabalho.

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho



14
15
16
# File 'lib/cns/therock.rb', line 14

def ops
  @ops
end

Instance Method Details

#exdHash

Returns dados exchange therock - saldos & transacoes ledger.

Returns:

  • (Hash)

    dados exchange therock - saldos & transacoes ledger



46
47
48
49
50
51
# File 'lib/cns/therock.rb', line 46

def exd
  @exd ||= {
    sl: api.,
    kl: api.ledger_mt
  }
end

#formata_ledger(hlx) ⇒ String

Returns texto formatado ledger.

Examples:

ledger_mt

{
  transactions: [
    {
      id: 305_445,
      date: '2014-03-06T10:59:13.000Z',
      type: 'withdraw',
      price: 97.47,
      currency: 'EUR',
      fund_id: nil,
      order_id: nil,
      trade_id: nil,
      note: 'BOV withdraw',
      transfer_detail: nil
    },
    {}
  ],
  meta: {
    total_count: nil,
    first: { page: 1, href: 'https://api.therocktrading.com/v1/transactions?page=1' },
    previous: nil,
    current: { page: 1, href: 'https://api.therocktrading.com/v1/transactions?page=1' },
    next: { page: 2, href: 'https://api.therocktrading.com/v1/transactions?page=2' },
    last: nil
  }
}

Returns:

  • (String)

    texto formatado ledger



77
78
79
80
81
82
83
84
85
86
# File 'lib/cns/therock.rb', line 77

def formata_ledger(hlx)
  format(
    '%<ky>6i %<dt>19.19s %<ty>-27.27s %<mo>-4.4s %<vl>20.7f',
    ky: hlx[:id],
    dt: Time.parse(hlx[:date]),
    ty: hlx[:type],
    mo: hlx[:currency].upcase,
    vl: hlx[:price].to_d
  )
end

#formata_saldos(hsl) ⇒ String

Returns texto formatado saldos.

Examples:

account_mt

{
  balances: [
    { currency: 'BTC', balance: 0.0, trading_balance: 0.0 },
    { currency: 'ETH', balance: 0.0, trading_balance: 0.0 },
    { currency: 'EUR', balance: 0.0, trading_balance: 0.0 },
    { currency: 'DAI', balance: 0.0, trading_balance: 0.0 },
  ]
}

Parameters:

  • hsl (Hash)

    saldo therock da moeda

Returns:

  • (String)

    texto formatado saldos



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cns/therock.rb', line 61

def formata_saldos(hsl)
  cur = hsl[:currency]
  vbq = bqd[:sl][cur.downcase.to_sym].to_d
  vkr = hsl[:balance].to_d
  format(
    '%<mo>-5.5s %<kr>21.9f %<bq>21.9f %<ok>3.3s',
    mo: cur.upcase,
    kr: vkr,
    bq: vbq,
    ok: vkr == vbq ? 'OK' : 'NOK'
  )
end

#kylArray<String>

Returns lista txid dos ledger novos.

Returns:

  • (Array<String>)

    lista txid dos ledger novos



54
55
56
# File 'lib/cns/therock.rb', line 54

def kyl
  @kyl ||= exd[:kl].map { |oex| oex[:id] } - (ops[:t] ? [] : bqd[:nl].map { |obq| obq[:txid] })
end

#ledgerArray<Hash>

Returns lista ledger therock novos.

Returns:

  • (Array<Hash>)

    lista ledger therock novos



29
30
31
# File 'lib/cns/therock.rb', line 29

def ledger
  @ledger ||= exd[:kl].select { |obj| kyl.include?(obj[:id]) }
end

#mostra_ledgerString

Returns texto transacoes ledger.

Returns:

  • (String)

    texto transacoes ledger



97
98
99
100
101
102
# File 'lib/cns/therock.rb', line 97

def mostra_ledger
  return unless ops[:v] && ledger.count.positive?

  puts("\nledger data       hora     tipo                        moeda          quantidade")
  ledger.sort { |ant, prx| prx[:id] <=> ant[:id] }.each { |obj| puts(formata_ledger(obj)) }
end

#mostra_resumoString

Returns texto saldos & transacoes & ajuste dias.

Returns:

  • (String)

    texto saldos & transacoes & ajuste dias



34
35
36
37
38
39
40
41
42
43
# File 'lib/cns/therock.rb', line 34

def mostra_resumo
  puts("\nTHEROCK\ntipo                therock              bigquery")
  exd[:sl].each { |obj| puts(formata_saldos(obj)) }
  mostra_totais

  mostra_ledger
  return unless ledger.count.positive?

  puts("\nstring ajuste dias da ledger\n-h=#{kyl.map { |obj| "#{obj}:0" }.join(' ')}")
end

#mostra_totaisString

Returns texto totais numero de transacoes.

Returns:

  • (String)

    texto totais numero de transacoes



89
90
91
92
93
94
# File 'lib/cns/therock.rb', line 89

def mostra_totais
  vkl = exd[:kl].count
  vnl = bqd[:nl].count

  puts("LEDGER #{format('%<c>20i %<d>21i %<o>3.3s', c: vkl, d: vnl, o: vkl == vnl ? 'OK' : 'NOK')}")
end