Module: BankApi::Clients::BancoSecurity::Deposits
- Included in:
- CompanyClient
- Defined in:
- lib/bank_api/clients/banco_security/concerns/deposits.rb
Instance Method Summary collapse
- #any_deposits? ⇒ Boolean
- #deposit_range ⇒ Object
- #deposits_account_details_url ⇒ Object
- #deposits_from_account_details ⇒ Object
- #deposits_from_txt ⇒ Object
- #deposits_txt_url ⇒ Object
- #extract_client_name(text) ⇒ Object
- #fill_date_inputs ⇒ Object
- #format_account_transactions(transactions) ⇒ Object
- #format_transactions(transactions) ⇒ Object
- #select_deposits_range ⇒ Object
- #timezone ⇒ Object
- #wait_for_deposits_fetch ⇒ Object
Instance Method Details
#any_deposits? ⇒ Boolean
109 110 111 112 113 114 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 109 def any_deposits? browser.search( "#gridPrincipalRecibidas " \ ".k-label:contains('No se han encontrado transacciones para la búsqueda seleccionada.')" ).none? end |
#deposit_range ⇒ Object
92 93 94 95 96 97 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 92 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_url ⇒ Object
105 106 107 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 105 def deposits_account_details_url browser.search("a:contains('Descargar TXT')").first.attribute("href") end |
#deposits_from_account_details ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 47 def deposits_from_account_details data = browser.download( deposits_account_details_url ).content.encode("UTF-8", "iso-8859-3").split("\r\n") transactions = data[3, data.count - 11].reverse format_account_transactions(transactions) end |
#deposits_from_txt ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 36 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_url ⇒ Object
99 100 101 102 103 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 99 def deposits_txt_url selenium_browser.execute_script("console.log(DescargarDocumentoTxtRecibidas)") log = selenium_browser.manage.logs.get(:browser).last /url = '(.*)';/.match(log.).captures.first end |
#extract_client_name(text) ⇒ Object
87 88 89 90 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 87 def extract_client_name(text) parts = text.to_s.split(/\ DE | De | de /, 2) parts.last.to_s.strip end |
#fill_date_inputs ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 14 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 68 def format_account_transactions(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_transactions(transactions) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 55 def format_transactions(transactions) transactions.map do |t| datetime = timezone.local_to_utc(DateTime.parse(t[0])) { client: t[1], rut: Utils::BancoSecurity.format_rut(t[2]), date: datetime.to_date, time: datetime, amount: t[5].to_i } end end |
#select_deposits_range ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 6 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 |
#timezone ⇒ Object
116 117 118 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 116 def timezone @timezone ||= Timezone['America/Santiago'] end |
#wait_for_deposits_fetch ⇒ Object
29 30 31 32 33 34 |
# File 'lib/bank_api/clients/banco_security/concerns/deposits.rb', line 29 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 |