Class: Cex::TheRock

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

Overview

classe para processar saldos & transacoes ledger

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
27
# File 'lib/cex/therock.rb', line 22

def initialize(dad, pop)
  # API therock base
  @api = Apimt.new
  @dbq = dad
  @ops = pop
end

Instance Attribute Details

#apiApius (readonly)

Returns API therock.

Returns:

  • (Apius)

    API therock



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

def api
  @api
end

#dbqArray<Hash> (readonly)

Returns todos os dados bigquery.

Returns:

  • (Array<Hash>)

    todos os dados bigquery



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

def dbq
  @dbq
end

#opsThor::CoreExt::HashWithIndifferentAccess (readonly)

Returns opcoes trabalho.

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho



14
15
16
# File 'lib/cex/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



30
31
32
33
34
35
# File 'lib/cex/therock.rb', line 30

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

#formata_ledger(hlx) ⇒ String

Returns texto formatado transacao ledger.

Examples:

{
  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
  }
}

Parameters:

  • hlx (Hash)

    transacao ledger apimt

Returns:

  • (String)

    texto formatado transacao ledger



65
66
67
68
69
70
71
72
73
74
# File 'lib/cex/therock.rb', line 65

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 (therock/bigquery) & iguais/ok/nok?.

Examples:

{
  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 (therock/bigquery) & iguais/ok/nok?



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cex/therock.rb', line 50

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

#kylArray<String>

Returns lista txid de transacoes ledger.

Returns:

  • (Array<String>)

    lista txid de transacoes ledger



38
39
40
# File 'lib/cex/therock.rb', line 38

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

#ledgerHash

Returns transacoes ledger.

Returns:

  • (Hash)

    transacoes ledger



43
44
45
# File 'lib/cex/therock.rb', line 43

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

#mostra_ledgerString

Returns texto transacoes ledger.

Returns:

  • (String)

    texto transacoes ledger



88
89
90
91
92
93
# File 'lib/cex/therock.rb', line 88

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

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

#mostra_resumoString

Returns texto saldos & transacoes & ajuste dias.

Returns:

  • (String)

    texto saldos & transacoes & ajuste dias



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

def mostra_resumo
  puts("\nTHEROCK\nmoeda         saldo therock        saldo bigquery")
  exd[:sl].each { |h| puts(formata_saldos(h)) }

  mostra_ledger
  return unless ledger.count.positive?

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