Class: PortalScraper::Accounts::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Raises:



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_idObject

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

#secretObject

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 (user)
  
  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.message }
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)
  
  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.click_button
      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_balanceObject



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
  
  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|
                  = link.click
      selected_link      = nil
      opened_on, balance = nil, 0.to_d

      .links_with(css: 'table#contrat td a').each do ||
        row = .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 = 
        opened_on     = row_date
        balance       = parse_number(row.at('td[5]'))
      end

      next unless selected_link

      transfers = scrap_transfers(selected_link)
       = {
          client_ref: find_table_value(, 'Identifiant tiers'),
          balance:    balance,
          opened_on:  opened_on,
          transfers:  transfers,
      }
      if block_given?
        yield()
      else
        accounts_data[:accounts] << 
      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