Class: PortalScraper::Accounts::Client
- Inherits:
-
Object
- Object
- PortalScraper::Accounts::Client
- Includes:
- ParsingHelper
- Defined in:
- lib/portal_scraper/accounts/client.rb
Constant Summary collapse
- CODE_TO_COUNTRY_MAPPING =
JSON.parse(File.read(File.join(File.dirname(__FILE__), 'code_to_country.json')))
- IS_POSTAL_CODE_DOM =
JSON.parse(File.read(File.join(File.dirname(__FILE__), 'is_postal_code_dom.json')))
- MINIMUM_AMOUNT =
10
- PROPOSITION_INDEX =
5
Instance Attribute Summary collapse
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#secret ⇒ Object
Returns the value of attribute secret.
Instance Method Summary collapse
- #create_account(user) ⇒ Object
- #create_proposition(client_ref) ⇒ Object
-
#initialize(app_id = nil, secret = nil) ⇒ Client
constructor
A new instance of Client.
- #scrap_accounts_balance ⇒ Object
Methods included from ParsingHelper
#parse_city_name, #parse_date, #parse_dates, #parse_number
Constructor Details
#initialize(app_id = nil, secret = nil) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 |
# File 'lib/portal_scraper/accounts/client.rb', line 16 def initialize(app_id = nil, secret = nil) @app_id = app_id || config.app_id @secret = secret || config.secret raise BadRootUrl unless config.root_url =~ URI::DEFAULT_PARSER.make_regexp end |
Instance Attribute Details
#app_id ⇒ Object
Returns the value of attribute app_id.
9 10 11 |
# File 'lib/portal_scraper/accounts/client.rb', line 9 def app_id @app_id end |
#secret ⇒ Object
Returns the value of attribute secret.
9 10 11 |
# File 'lib/portal_scraper/accounts/client.rb', line 9 def secret @secret end |
Instance Method Details
#create_account(user) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/portal_scraper/accounts/client.rb', line 69 def create_account(user) login app.get(url_for('/appTiersCreation.do')) do |create_page| create_page.form_with(name: /(create|creation)/i) do |form| form.fields.each { |f| try_form_fields(form, [f.name], user[f.name.to_sym]) } if user[:codeInsee].present? form['commune'] = user[:codeInsee] form['libCommune'] = user[:ville] else fill_commune(app, form, user[:codePostal], parse_city_name(user[:ville])) end fill_address(form, user[:numeroVoie]) fill_birth_place(form, user[:paysNaissance]) form['modeContact'] = '' form['creerTiers'] = 'Créer' form.submit result_page = app.post(url_for('/appTiersCreation.do'), { confirmer: 'Confirmer' }) if result_page.uri.path =~ /error.do/ return { error: 'Missing data' } else ref = result_page.search('td.ZMSG').text.split(':')[1].strip return { client_ref: ref } end end end nil rescue => e { error: e. } end |
#create_proposition(client_ref) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/portal_scraper/accounts/client.rb', line 99 def create_proposition(client_ref) login app.get(url_for('/appRecherche.do')) do |search_user_page| search_user_page.form_with(name: /(frmCritRech)/i) do |form| form['idTiers'] = client_ref result_page = form. result_page.links.find { |l| l.text.strip == client_ref }.click app.get(url_for('/propositionList.do')) return { proposition_ref: proposition } end end nil end |
#scrap_accounts_balance ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/portal_scraper/accounts/client.rb', line 22 def scrap_accounts_balance login accounts_data = { accounts: [] } user_page = app.post(url_for('/appRecherche.do'), { nom: '', prenom: '', chercher: 'Chercher' }) loop do user_page.links_with(href: /appTiersDetail.do/).each do |link| account = link.click selected_link = nil opened_on, balance = nil, 0.to_d account.links_with(css: 'table#contrat td a').each do |account_link| row = account_link.node.ancestors('tr') next unless row.at('td[1]').text.strip == 'Compte épargne rémunéré' row_date = parse_date(row.at('td[2]')) next unless opened_on.nil? || opened_on < row_date selected_link = account_link opened_on = row_date balance = parse_number(row.at('td[5]')) end next unless selected_link transfers = scrap_transfers(selected_link) account_data = { client_ref: find_table_value(account, 'Identifiant tiers'), balance: balance, opened_on: opened_on, transfers: transfers, } if block_given? yield(account_data) else accounts_data[:accounts] << account_data end end break unless user_page.search('.pg_next').present? # Need to fetch the first page as you cannot navigate from client back to research page app.post(url_for('/appRecherche.do'), { nom: '', prenom: '', chercher: 'Chercher' }) user_page = app.get(user_page.search('a.pg_next').first['href']) end accounts_data end |