Class: Cns::Paymium

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

Overview

classe para processar transacoes ledger do paymium

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dad, pop) ⇒ Paymium

Returns API paymium - 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/paymium.rb', line 22

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

Instance Attribute Details

#apiApius (readonly)

Returns API paymium.

Returns:

  • (Apius)

    API paymium



10
11
12
# File 'lib/cns/paymium.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/paymium.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/paymium.rb', line 14

def ops
  @ops
end

Instance Method Details

#exdHash

Returns dados exchange paymium - saldos & transacoes ledger.

Returns:

  • (Hash)

    dados exchange paymium - saldos & transacoes ledger



47
48
49
50
51
52
# File 'lib/cns/paymium.rb', line 47

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

#formata_ledger(hlx) ⇒ String

Returns texto formatado ledger.

Examples:

ledger_fr

[
  {
    uuid: '50551e61-4e74-4ae7-85fd-9c2040542818',
    currency_amount: nil,
    state: 'executed',
    btc_fee: '0.0',
    currency_fee: '0.0',
    created_at: '2014-03-04T09:00Z',
    updated_at: '2014-03-04T09:00Z',
    currency: 'EUR',
    comment: '5723',
    amount: '100.0',
    type: 'WireDeposit',
    account_operations: [{
      uuid: 'b5058a68-cf99-4438-86d3-e773eba418ec',
      name: 'wire_deposit',
      amount: '100.0',
      currency: 'EUR',
      created_at: '2014-03-04T09:00Z',
      created_at_int: 1_393_923_644,
      is_trading_account: false
    }, {}]
  },
  {}
]

Parameters:

  • hlx (Hash)

    ledger paymium

Returns:

  • (String)

    texto formatado ledger



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

def formata_ledger(hlx)
  format(
    '%<ky>-18.18s %<dt>19.19s %<ty>-17.17s %<mo>-4.4s %<vl>18.7f',
    ky: formata_uuid(hlx[:uuid], 18),
    dt: Time.at(hlx[:created_at_int]),
    ty: hlx[:name],
    mo: hlx[:currency].upcase,
    vl: hlx[:amount].to_d
  )
end

#formata_saldos(bqm) ⇒ String

Returns texto formatado saldos.

Examples:

account_fr

{
  name: '...',
  email: '...',
  locale: 'en',
  channel_id: '...',
  meta_state: 'approved',
  balance_eur: '0.0',
  locked_eur: '0.0',
  balance_btc: '0.0',
  locked_btc: '0.0',
  balance_lbtc: '0.0',
  locked_lbtc: '0.0'
}

Parameters:

  • bqm (Symbol)

    symbol paymium da moeda

Returns:

  • (String)

    texto formatado saldos



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

def formata_saldos(bqm)
  vbq = bqd[:sl][bqm].to_d
  vkr = exd[:sl]["balance_#{bqm}".to_sym].to_d
  format(
    '%<mo>-5.5s %<kr>21.9f %<bq>21.9f %<ok>3.3s',
    mo: bqm.upcase,
    kr: vkr,
    bq: vbq,
    ok: vkr == vbq ? 'OK' : 'NOK'
  )
end

#formata_uuid(uid, max) ⇒ String

Returns texto formatado identificacor da ledger.

Parameters:

  • uid (String)

    identificacor da ledger

  • max (Integer)

    chars a mostrar

Returns:

  • (String)

    texto formatado identificacor da ledger



92
93
94
95
# File 'lib/cns/paymium.rb', line 92

def formata_uuid(uid, max)
  int = Integer(max / 2)
  max < 7 ? 'erro' : "#{uid[0, int]}#{uid[-int..]}"
end

#kylArray<String>

Returns lista txid dos ledger novos.

Returns:

  • (Array<String>)

    lista txid dos ledger novos



55
56
57
58
# File 'lib/cns/paymium.rb', line 55

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

#ledgerArray<Hash>

Returns lista ledger paymium novos.

Returns:

  • (Array<Hash>)

    lista ledger paymium novos



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

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

#mostra_ledgerString

Returns texto transacoes ledger.

Returns:

  • (String)

    texto transacoes ledger



106
107
108
109
110
111
# File 'lib/cns/paymium.rb', line 106

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

  puts("\nledger             data       hora     tipo              moeda        quantidade")
  ledger.sort { |ant, prx| prx[:created_at_int] <=> ant[:created_at_int] }.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
44
# File 'lib/cns/paymium.rb', line 34

def mostra_resumo
  puts("\nPAYMIUM\ntipo                paymium              bigquery")
  puts(formata_saldos(:btc))
  puts(formata_saldos(:eur))
  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



98
99
100
101
102
103
# File 'lib/cns/paymium.rb', line 98

def mostra_totais
  vkl = exd[:kl].map { |obj| obj[:account_operations].count }.flatten.inject(:+)
  vnl = bqd[:nl].count

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