Class: Bankscrap::Openbank::Bank

Inherits:
Bank
  • Object
show all
Includes:
Utils
Defined in:
lib/bankscrap/openbank/bank.rb

Constant Summary collapse

BASE_ENDPOINT =

Define the endpoints for the Bank API here

'https://api.openbank.es'.freeze
LOGIN_ENDPOINT =
'/authenticationcomposite/login'.freeze
PRODUCTS_ENDPOINT =
'/posicion-global-total'.freeze
USER_AGENT =
'okhttp/3.9.1'.freeze

Instance Method Summary collapse

Methods included from Utils

#format_date, #money, #next_page_fields, #parse_date

Constructor Details

#initialize(credentials = {}) ⇒ Bank

Returns a new instance of Bank.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bankscrap/openbank/bank.rb', line 20

def initialize(credentials = {})
  @token_credential = nil
  super do
    add_headers(
      'Content-Type'     => 'application/json; charset=utf-8',
      'User-Agent'       => USER_AGENT,
      'Connection'       => 'Keep-Alive',
      'Accept-Encoding'  => 'gzip'
    )
  end
end

Instance Method Details

#fetch_accountsObject

Fetch all the accounts for the given user

Should returns an array of Bankscrap::Account objects



53
54
55
56
57
58
# File 'lib/bankscrap/openbank/bank.rb', line 53

def fetch_accounts
  log 'fetch_accounts'
  data = get(PRODUCTS_ENDPOINT, fields: { carteras: false,listaSolicitada: 'TODOS',indicadorSaldoPreTarj: false })
  cuentas = data['datosSalidaCuentas']['cuentas'].zip(data['datosSalidaCodIban']['datosIban'])
  cuentas.map{ |data| (data) }
end

#fetch_cardsObject

Fetch all the cards for the given user

Should returns an array of Bankscrap::Card objects



63
64
65
66
67
# File 'lib/bankscrap/openbank/bank.rb', line 63

def fetch_cards
   log 'fetch_cards'
   data = get(PRODUCTS_ENDPOINT, fields: { carteras: false,listaSolicitada: 'TODOS',indicadorSaldoPreTarj: false })
   data['datosSalidaTarjetas']['tarjetas'].map{ |data| build_card(data) }
end

#fetch_transactions_for(product, start_date: Date.today - 1.month, end_date: Date.today) ⇒ Object

Fetch transactions for the given account.

Account should be a Bankscrap::Account object Should returns an array of Bankscrap::Account objects



72
73
74
# File 'lib/bankscrap/openbank/bank.rb', line 72

def fetch_transactions_for(product, start_date: Date.today - 1.month, end_date: Date.today)
  product.fetch_transactions_for(self, start_date: start_date, end_date: end_date)
end

#get(method, fields: {}) ⇒ Object



82
83
84
85
86
# File 'lib/bankscrap/openbank/bank.rb', line 82

def get(method, fields: {})
  set_auth_headers()
  response = super(BASE_ENDPOINT + method, params: fields)
  parse_context(response)
end

#loginObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bankscrap/openbank/bank.rb', line 32

def 
  log 'login'
   = {
      document: @user,
      documentType: "N",
      password: @password,
      force: true,
      osVersion: "8.1.0",
      uuid: "#{SecureRandom.hex(10)[0..2]}-#{SecureRandom.hex(10)[0..6]}",
      mobileDeviceInfo: { pushEnabled:false,
                          rooted: false,
                          version: "1.1.16",
                          device: "SAMSUNG",
                          platform: "ANDROID" }
      }
  post(, fields: )
end

#post(method, fields: {}) ⇒ Object



76
77
78
79
80
# File 'lib/bankscrap/openbank/bank.rb', line 76

def post(method, fields: {})
  set_auth_headers()
  response = super(BASE_ENDPOINT + method, fields: JSON.generate(fields))
  parse_context(response)
end