Module: BankApi::Clients::BancoSecurity::Withdrawals

Included in:
CompanyClient
Defined in:
lib/bank_api/clients/banco_security/concerns/withdrawals.rb

Constant Summary collapse

WITHDRAWALS_URL =
'https://www.bancosecurity.cl/ConvivenciaEmpresas/CartolasTEF/Home/ConsultarEnviadasEmpresas'

Instance Method Summary collapse

Instance Method Details

#account_number_variableObject



94
95
96
97
98
99
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 94

def 
  selenium_browser.manage.logs.get(:browser).last
  selenium_browser.execute_script("console.log(nCuenta)")
  log = selenium_browser.manage.logs.get(:browser).last
  /\"(.*)\"/.match(log.message).captures.first
end

#any_withdrawals?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 87

def any_withdrawals?
  browser.search(
    "#gridPrincipalEnviadasE " \
    ".k-label:contains('No se han encontrado transacciones para la búsqueda seleccionada.')"
  ).none?
end

#fill_withdrawal_date_inputsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 15

def fill_withdrawal_date_inputs
  start_element = browser.search('#datePickerInicioEnviadoE').elements.first
  start_element.send_key "-"
  withdrawal_range[:start].strftime('%d%m%Y').chars.each do |c|
    start_element.send_key c
    sleep 0.1
  end
  end_element = browser.search('#datePickerFinEnviadoE').elements.first
  end_element.send_key "-"
  withdrawal_range[:end].strftime('%d%m%Y').chars.each do |c|
    end_element.send_key c
    sleep 0.1
  end
end

#format_withdrawal_transactions(transactions) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 47

def format_withdrawal_transactions(transactions)
  transactions.map do |t|
    datetime = Time.at(t["Fecha"].match(/(\d+)/)[0].to_i / 1000).utc
    {
      client: t["NombreDestino"],
      account_bank: t["BancoDestino"],
      account_number: t["CuentaDestino"],
      rut: Utils::BancoSecurity.format_rut(t["RutDestino"]),
      email: t["MailDestino"],
      date: datetime.to_date,
      time: datetime,
      amount: t["Monto"],
      trx_id: t["NumeroTransaccion"]
    }
  end
end

#select_withdrawals_rangeObject



7
8
9
10
11
12
13
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 7

def select_withdrawals_range
  browser.search('.BusquedaPorDefectoEnv a:contains("búsqueda avanzada")').click
  browser.search('#RadioEntreFechasEnviadoE').click
  fill_withdrawal_date_inputs
  wait('.ContenedorSubmitEnviadas .btn_buscar').click
  wait_for_withdrawals_fetch
end

#total_withdrawalsObject



81
82
83
84
85
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 81

def total_withdrawals
  pages_info = wait(".k-pager-info")
  matches = pages_info.text.match(/(\d+)[a-z\s-]+(\d+)[a-z\s-]+(\d+)/)
  matches[3].to_i
end

#validate_withdrawals(withdrawals) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 101

def validate_withdrawals(withdrawals)
  total_withdrawals_ = total_withdrawals
  unless withdrawals.count == total_withdrawals_
    raise BankApi::Withdrawal::QuantityError, "Expected #{total_withdrawals_} withdrawals," +
      " got #{withdrawals.count}."
  end
end

#wait_for_withdrawals_fetchObject



30
31
32
33
34
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 30

def wait_for_withdrawals_fetch
  goto_frame query: '#mainFrame'
  goto_frame query: 'iframe[name="central"]', should_reset: false
  wait('.k-loading-image') { browser.search('.k-loading-image').none? }
end

#withdrawal_rangeObject



73
74
75
76
77
78
79
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 73

def withdrawal_range
  @withdrawal_range ||= begin
    timezone = Timezone['America/Santiago']
    today = timezone.utc_to_local(Time.now).to_date
    { start: (today - @days_to_check), end: today }
  end
end

#withdrawals_from_jsonObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 36

def withdrawals_from_json
  raise BankApi::Withdrawal::FetchError, "Couldn't fetch withdrawals" unless any_withdrawals?
  setup_authentication
  response = RestClient::Request.execute(
    url: WITHDRAWALS_URL, method: :post, headers: session_headers,
    payload: withdrawals_payload(deposit_range[:start], deposit_range[:end])
  )
  transactions = JSON.parse(response.body)["Items"]
  format_withdrawal_transactions(transactions)
end

#withdrawals_payload(start_date, end_date) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/bank_api/clients/banco_security/concerns/withdrawals.rb', line 64

def withdrawals_payload(start_date, end_date)
  {
    "parametro" => "", "numeroCuenta" => , "monto" => 0, "opcion" => "",
    "fechaInicio" => "#{start_date} 0:00:00", "fechaFin" => "#{end_date} 0:00:00",
    "estado" => 1, "tipoTransaccion" => "", "take" => 1000, "skip" => 0, "page" => 1,
    "pageSize" => 1000, "sort[0][field]" => "Fecha", "sort[0][dir]" => "desc"
  }
end