Module: BankApi::Clients::BancoSecurity::Deposits

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

Constant Summary collapse

SESSION_VALIDATION =
"https://www.bancosecurity.cl/empresas/SessionValidation.asp"

Instance Method Summary collapse

Instance Method Details

#any_deposits?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 150

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

#cookiesObject



122
123
124
125
126
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 122

def cookies
  selenium_browser.manage.all_cookies.map do |cookie|
    "#{cookie[:name]}=#{cookie[:value]}"
  end.join("; ")
end

#deposit_rangeObject



106
107
108
109
110
111
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 106

def deposit_range
  @deposit_range ||= begin
    today = timezone.utc_to_local(Time.now).to_date
    { start: (today - @days_to_check), end: today }
  end
end

#deposits_account_details_urlObject



134
135
136
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 134

def 
  browser.search("a:contains('Descargar TXT')").first.attribute("href")
end

#deposits_from_account_detailsObject



61
62
63
64
65
66
67
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 61

def 
  data = browser.download(
    
  ).content.encode("UTF-8", "iso-8859-3").split("\r\n")
  transactions = data[3, data.count - 11].reverse
  (transactions)
end

#deposits_from_txtObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 38

def deposits_from_txt
  raise BankApi::Deposit::FetchError, "Couldn't fetch deposits" unless any_deposits?
  setup_authentication
  download = browser.download(deposits_txt_url)
  transactions = download.content.split("\n").drop(1).map { |r| r.split("|") }
  if transactions.empty?
    raise BankApi::Deposit::FetchError, "Couldn't fetch deposits, received #{download.content}"
  end
  format_transactions(transactions)
end

#deposits_txt_urlObject



128
129
130
131
132
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 128

def deposits_txt_url
  selenium_browser.execute_script("console.log(DescargarDocumentoTxtRecibidas)")
  log = selenium_browser.manage.logs.get(:browser).last
  /url = '(.*)';/.match(log.message).captures.first
end

#extract_client_name(text) ⇒ Object



101
102
103
104
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 101

def extract_client_name(text)
  parts = text.to_s.split(/\ DE | De | de /, 2)
  parts.last.to_s.strip
end

#fill_date_inputsObject



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

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

#format_account_transactions(transactions) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 82

def (transactions)
  transactions.inject([]) do |memo, t|
    parts = t.split(";")
    amount = parts[4].delete(",").to_i
    next memo if amount.zero?
    client = extract_client_name(parts[1])

    memo << {
      client: client,
      rut: nil,
      date: Date.strptime(parts[0], "%d/%m"),
      time: nil,
      amount: amount
    }

    memo
  end
end

#format_rut(rut) ⇒ Object



138
139
140
141
142
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 138

def format_rut(rut)
  verification_digit = rut[-1]
  without_verification_digit = rut[0..-2].reverse.scan(/.{1,3}/).join(".").reverse
  "#{without_verification_digit}-#{verification_digit}"
end

#format_transactions(transactions) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 69

def format_transactions(transactions)
  transactions.map do |t|
    datetime = timezone.local_to_utc(DateTime.parse(t[0]))
    {
      client: t[1],
      rut: format_rut(t[2]),
      date: datetime.to_date,
      time: datetime,
      amount: t[5].to_i
    }
  end
end

#select_deposits_rangeObject



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

def select_deposits_range
  browser.search('.BusquedaPorDefectoRecibida a:contains("búsqueda avanzada")').click
  browser.search('#RadioEntreFechasRecibido').click
  fill_date_inputs
  wait('.ContenedorSubmitRecibidas .btn_buscar').click
  wait_for_deposits_fetch
end

#session_headersObject



113
114
115
116
117
118
119
120
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 113

def session_headers
  {
    "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 " +
      "(KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
    "Accept" => "*/*",
    "Cookie" => cookies
  }
end

#setup_authenticationObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 49

def setup_authentication
  response = RestClient::Request.execute(
    url: SESSION_VALIDATION, method: :post, headers: session_headers
  )
  new_cookies = response.headers[:set_cookie].first.delete(" ").split(";").map do |a|
    a.split("=")
  end
  new_cookies.each do |key, value|
    selenium_browser.manage.add_cookie(name: key, value: value)
  end
end

#timezoneObject



165
166
167
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 165

def timezone
  @timezone ||= Timezone['America/Santiago']
end

#total_depositsObject



144
145
146
147
148
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 144

def total_deposits
  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_deposits(deposits) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 157

def validate_deposits(deposits)
  total_deposits_ = total_deposits
  unless deposits.count == total_deposits_
    raise BankApi::Deposit::QuantityError, "Expected #{total_deposits_} deposits," +
      " got #{deposits.count}."
  end
end

#wait_for_deposits_fetchObject



31
32
33
34
35
36
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 31

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