bank_api 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 658eeb4228e71f3270ec9fcb7f9455e2a8f703ba
4
- data.tar.gz: dea5f4a5b8c30973b5f1bcae01829869938c9272
3
+ metadata.gz: abde8158cac67c0a38e773a4667767026e6146ae
4
+ data.tar.gz: aeb12e55f14718d2df458d28173f61c9f5d032f1
5
5
  SHA512:
6
- metadata.gz: 69241713c94c45fae648a6d92aa5002d781121d743e3f9579d28c52f5f078d3b8967a51bbba8630bcd132d75027775e4ea82f54139ae46f26db15ccb65fc2974
7
- data.tar.gz: 9ab588cfcd5e6db746f9365707da79a99f2728dacdec3018072f191e33a0155be4357482a0aab4adcb497f5f3b8ae31b14543c59a94fdb8b8b5e0ca2f5df2ec1
6
+ metadata.gz: b87b6c4371123c40cb0727a2c9a1335bf7f4c3e599e0cb74fe57c81a767df9b9e26c250e819e90e20c8f64a06645799f28bacbd920e19b816f2ec3043da82b3e
7
+ data.tar.gz: 25af2be30ba0ae54b42b45c59b98e7760ac14a7b1b1046306c9d7e39b7099c3e08a698e196583a6480cb39de197141cb0b9e7729bec9683de17efd869c02e39b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bank_api (0.3.1)
4
+ bank_api (0.3.2)
5
5
  pincers
6
6
  timezone (~> 1.0)
7
7
 
@@ -1,3 +1,4 @@
1
+ require 'cgi'
1
2
  require 'timezone'
2
3
 
3
4
  require 'bank_api/clients/base_client'
@@ -29,17 +30,13 @@ module BankApi::Clients::BancoSecurity
29
30
 
30
31
  def get_deposits
31
32
  login
32
- deposits_first_try = get_deposits_try
33
- deposits_second_try = get_deposits_try
34
- browser.close
35
- deposits_first_try == deposits_second_try ? deposits_first_try : []
36
- end
37
-
38
- def get_deposits_try
39
33
  goto_company_dashboard
40
34
  goto_deposits
41
35
  select_deposits_range
42
- extract_deposits_from_html
36
+ deposits = deposits_from_txt
37
+ validate_deposits(deposits) unless deposits.empty?
38
+ browser.close
39
+ deposits
43
40
  end
44
41
 
45
42
  def execute_transfer(transfer_data)
@@ -6,16 +6,13 @@ module BankApi::Clients::BancoSecurity
6
6
  RUT_COLUMN = 2
7
7
  AMOUNT_COLUMN = 5
8
8
 
9
- NUMBER_OF_COLUMNS = 7
10
-
11
9
  def select_deposits_range
12
10
  browser.search('.BusquedaPorDefectoRecibida a:contains("búsqueda avanzada")').click
13
11
  browser.search('#RadioEntreFechasRecibido').click
14
- browser.search('#datePickerInicioRecibidas').set deposit_range[:start]
15
- browser.search('#datePickerFinRecibido').set deposit_range[:end]
12
+ browser.search('#datePickerInicioRecibidas').set deposit_range[:start].strftime('%d/%m/%Y')
13
+ browser.search('#datePickerFinRecibido').set deposit_range[:end].strftime('%d/%m/%Y')
16
14
  wait('.ContenedorSubmitRecibidas .btn_buscar').click
17
15
  wait_for_deposits_fetch
18
- set_page_size
19
16
  end
20
17
 
21
18
  def wait_for_deposits_fetch
@@ -25,62 +22,42 @@ module BankApi::Clients::BancoSecurity
25
22
  wait('.k-loading-image') { browser.search('.k-loading-image').none? }
26
23
  end
27
24
 
28
- def wait_for_next_page(last_seen_deposit)
29
- wait(".k-pager-info") { last_seen_deposit < last_deposit_in_current_page }
25
+ def deposits_from_txt
26
+ return [] unless any_deposits?
27
+ download = browser.download(deposits_txt_url)
28
+ transactions = download.content.split("\n").drop(1).map { |r| r.split("|") }
29
+ format_transactions(transactions)
30
30
  end
31
31
 
32
- def extract_deposits_from_html
33
- deposits = []
34
-
35
- return deposits unless any_deposits?
36
-
37
- deposits += deposits_from_page
38
- last_seen_deposit = last_deposit_in_current_page
39
- ((total_deposits - 1) / @page_size).times do
40
- goto_next_page
41
- wait_for_next_page(last_seen_deposit)
42
- last_seen_deposit = last_deposit_in_current_page
43
-
44
- deposits += deposits_from_page
32
+ def format_transactions(transactions)
33
+ transactions.map do |t|
34
+ {
35
+ rut: format_rut(t[RUT_COLUMN]),
36
+ date: Date.parse(t[DATE_COLUMN]),
37
+ amount: t[AMOUNT_COLUMN].to_i
38
+ }
45
39
  end
46
-
47
- validate_deposits(deposits, last_seen_deposit)
48
-
49
- deposits.sort_by { |d| d[:date] }
50
40
  end
51
41
 
52
- def deposits_from_page
53
- deposits = []
54
- deposit = {}
55
- browser.search('#gridPrincipalRecibidas tbody td').each_with_index do |div, index|
56
- if (index % NUMBER_OF_COLUMNS) == RUT_COLUMN
57
- deposit[:rut] = div.text
58
- elsif (index % NUMBER_OF_COLUMNS) == DATE_COLUMN
59
- deposit[:date] = Date.parse div.text
60
- elsif (index % NUMBER_OF_COLUMNS) == AMOUNT_COLUMN
61
- deposit[:amount] = div.text.gsub(/[\. $]/, '').to_i
62
- elsif ((index + 1) % NUMBER_OF_COLUMNS).zero?
63
- deposits << deposit
64
- deposit = {}
65
- end
42
+ def deposit_range
43
+ @deposit_range ||= begin
44
+ timezone = Timezone['America/Santiago']
45
+ today = timezone.utc_to_local(Time.now).to_date
46
+ { start: (today - @days_to_check), end: today }
66
47
  end
67
- deposits
68
48
  end
69
49
 
70
- def set_page_size
71
- browser.search('[aria-owns="pagerSettingRecibidas_listbox"]').click
72
- sleep 0.1
73
- browser.search('.k-animation-container.km-popup li').find do |li|
74
- li.text == @page_size.to_s
75
- end.click
76
- wait('.k-loading-image') { browser.search('.k-loading-image').any? }
77
- wait('.k-loading-image') { browser.search('.k-loading-image').none? }
50
+ def deposits_txt_url
51
+ account_number = CGI.escape(selenium_browser.execute_script('return nCuenta'))
52
+ start_ = CGI.escape("#{deposit_range[:start].strftime('%m/%d/%Y')} 00:00:00")
53
+ end_ = CGI.escape("#{deposit_range[:end].strftime('%m/%d/%Y')} 00:00:00")
54
+ "https://www.bancosecurity.cl/ConvivenciaEmpresas/CartolasTEF/Download/" +
55
+ "CrearTxTRecibidas?numeroCuenta=#{account_number}&monto=0&" +
56
+ "fechaInicio=#{start_}&fechaFin=#{end_}&estado=1"
78
57
  end
79
58
 
80
- def last_deposit_in_current_page
81
- pages_info = wait(".k-pager-info")
82
- matches = pages_info.text.match(/(\d+)[a-z\s-]+(\d+)[a-z\s-]+(\d+)/)
83
- matches[2].to_i
59
+ def format_rut(rut)
60
+ rut.insert(-2, '-')
84
61
  end
85
62
 
86
63
  def total_deposits
@@ -89,18 +66,6 @@ module BankApi::Clients::BancoSecurity
89
66
  matches[3].to_i
90
67
  end
91
68
 
92
- def goto_next_page
93
- browser.search('#gridPrincipalRecibidas a.k-link[title="Go to the next page"]').click
94
- end
95
-
96
- def deposit_range
97
- @deposit_range ||= begin
98
- timezone = Timezone['America/Santiago']
99
- today = timezone.utc_to_local(Time.now).to_date
100
- { start: (today - @days_to_check).strftime('%d/%m/%Y'), end: today.strftime('%d/%m/%Y') }
101
- end
102
- end
103
-
104
69
  def any_deposits?
105
70
  browser.search(
106
71
  "#gridPrincipalRecibidas " \
@@ -108,17 +73,12 @@ module BankApi::Clients::BancoSecurity
108
73
  ).none?
109
74
  end
110
75
 
111
- def validate_deposits(deposits, last_seen_deposit)
76
+ def validate_deposits(deposits)
112
77
  total_deposits_ = total_deposits
113
78
  unless deposits.count == total_deposits_
114
79
  raise BankApi::Deposit::QuantityError, "Expected #{total_deposits_} deposits," +
115
80
  " got #{deposits.count}."
116
81
  end
117
-
118
- unless last_seen_deposit == total_deposits_
119
- raise BankApi::Deposit::PaginationError, "Expected to fetch #{total_deposits_} deposits," +
120
- " the last seen deposit was nº #{last_seen_deposit}."
121
- end
122
82
  end
123
83
  end
124
84
  end
@@ -1,3 +1,3 @@
1
1
  module BankApi
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bank_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - oaestay
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-15 00:00:00.000000000 Z
11
+ date: 2018-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pincers