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

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

Constant Summary collapse

DATE_COLUMN =
0
RUT_COLUMN =
2
AMOUNT_COLUMN =
5
NUMBER_OF_COLUMNS =
7

Instance Method Summary collapse

Instance Method Details

#any_deposits?Boolean

Returns:

  • (Boolean)


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

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

#deposit_rangeObject



96
97
98
99
100
101
102
103
104
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 96

def deposit_range
  @deposit_range ||= begin
    timezone = Timezone['America/Santiago']
    {
      start: (timezone.utc_to_local(Time.now).to_date - @days_to_check).strftime('%d/%m/%Y'),
      end: timezone.utc_to_local(Time.now).to_date.strftime('%d/%m/%Y')
    }
  end
end

#deposits_from_pageObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 52

def deposits_from_page
  deposits = []
  deposit = {}
  browser.search('#gridPrincipalRecibidas tbody td').each_with_index do |div, index|
    if (index % NUMBER_OF_COLUMNS) == RUT_COLUMN
      deposit[:rut] = div.text
    elsif (index % NUMBER_OF_COLUMNS) == DATE_COLUMN
      deposit[:date] = Date.parse div.text
    elsif (index % NUMBER_OF_COLUMNS) == AMOUNT_COLUMN
      deposit[:amount] = div.text.gsub(/[\. $]/, '').to_i
    elsif ((index + 1) % NUMBER_OF_COLUMNS).zero?
      deposits << deposit
      deposit = {}
    end
  end
  deposits
end

#extract_deposits_from_htmlObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 32

def extract_deposits_from_html
  deposits = []

  return deposits unless any_deposits?

  deposits += deposits_from_page
  last_seen_deposit = last_deposit_in_current_page
  ((total_deposits - 1) / @page_size).times do
    goto_next_page
    wait_for_next_page(last_seen_deposit)
    last_seen_deposit = last_deposit_in_current_page

    deposits += deposits_from_page
  end

  validate_deposits(deposits, last_seen_deposit)

  deposits.sort_by { |d| d[:date] }
end

#goto_next_pageObject



92
93
94
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 92

def goto_next_page
  browser.search('#gridPrincipalRecibidas a.k-link[title="Go to the next page"]').click
end

#last_deposit_in_current_pageObject



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

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

#select_deposits_rangeObject



11
12
13
14
15
16
17
18
19
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 11

def select_deposits_range
  browser.search('.BusquedaPorDefectoRecibida a:contains("búsqueda avanzada")').click
  browser.search('#RadioEntreFechasRecibido').click
  browser.search('#datePickerInicioRecibidas').set deposit_range[:start]
  browser.search('#datePickerFinRecibido').set deposit_range[:end]
  wait('.ContenedorSubmitRecibidas .btn_buscar').click
  wait_for_deposits_fetch
  set_page_size
end

#set_page_sizeObject



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

def set_page_size
  browser.search('[aria-owns="pagerSettingRecibidas_listbox"]').click
  sleep 0.1
  browser.search('.k-animation-container.km-popup li').find do |li|
    li.text == @page_size.to_s
  end.click
  wait('.k-loading-image') { browser.search('.k-loading-image').any? }
  wait('.k-loading-image') { browser.search('.k-loading-image').none? }
end

#total_depositsObject



86
87
88
89
90
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 86

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, last_seen_deposit) ⇒ Object



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

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

  unless last_seen_deposit == total_deposits_
    raise BankApi::Deposit::PaginationError, "Expected to fetch #{total_deposits_} deposits," +
      " the last seen deposit was nº #{last_seen_deposit}."
  end
end

#wait_for_deposits_fetchObject



21
22
23
24
25
26
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 21

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

#wait_for_next_page(last_seen_deposit) ⇒ Object



28
29
30
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 28

def wait_for_next_page(last_seen_deposit)
  wait(".k-pager-info") { last_seen_deposit < last_deposit_in_current_page }
end