Class: Bankscrap::Openbank::Account

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

Direct Known Subclasses

Card

Constant Summary collapse

ACCOUNT_ENDPOINT =
'/OPB_BAMOBI_WS_ENS/ws/BAMOBI_WS_Def_Listener'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#money, #value_at_xpath

Instance Attribute Details

#contract_idObject

Returns the value of attribute contract_id.



8
9
10
# File 'lib/bankscrap/openbank/account.rb', line 8

def contract_id
  @contract_id
end

Instance Method Details

#build_transaction(data) ⇒ Object

Build a transaction object from API data



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bankscrap/openbank/account.rb', line 66

def build_transaction(data)
  Transaction.new(
    account: self,
    id: value_at_xpath(data, 'numeroMovimiento'),
    amount: money(data, 'importe'),
    description: value_at_xpath(data, 'descripcion'),
    effective_date: Date.strptime(value_at_xpath(data, 'fechaValor'), '%Y-%m-%d'),
    # TODO: Bankscrap has no interface to add operation date
    balance: money(data, 'importeSaldo')
  )
end

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

Fetch transactions for the given account. By default it fetches transactions for the last month,

Returns an array of BankScrap::Transaction objects



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bankscrap/openbank/account.rb', line 16

def fetch_transactions_for(connection, start_date: Date.today - 1.month, end_date: Date.today)
  transactions = []
  end_page = false
  repo = nil
  importe_cta = nil

  # Loop over pagination
  until end_page
    document = connection.post(, fields: (connection, start_date, end_date, repo, importe_cta))

    transactions += document.xpath('//listadoMovimientos/movimiento').map { |data| build_transaction(data) }

    repo = document.at_xpath('//methodResult/repo')
    importe_cta = document.at_xpath('//methodResult/importeCta')
    end_page = value_at_xpath(document, '//methodResult/finLista') != 'N'
  end

  transactions
end

#xml_account(connection, from_date, to_date, repo, importe_cta) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bankscrap/openbank/account.rb', line 36

def (connection, from_date, to_date, repo, importe_cta)
  is_pagination = repo ? 'S' : 'N'
  xml_from_date = xml_date(from_date)
  xml_to_date = xml_date(to_date)
  "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"   xmlns:v1=\"http://www.isban.es/webservices/BAMOBI/Cuentas/F_bamobi_cuentas_lip/internet/BAMOBICTA/v1\">\n  \#{connection.xml_security_header}\n  <soapenv:Body>\n    <v1:listaMovCuentasFechas_LIP facade=\"BAMOBICTA\">\n      <entrada>\n  \#{connection.xml_datos_cabecera}\n        <datosConexion>\#{connection.user_data.children}</datosConexion>\n        <contratoID>\#{contract_id}</contratoID>\n        <fechaDesde>\#{xml_from_date}</fechaDesde>\n        <fechaHasta>\#{xml_to_date}</fechaHasta>\n  \#{importe_cta}\n        <esUnaPaginacion>\#{is_pagination}</esUnaPaginacion>\n  \#{repo}\n      </entrada>\n    </v1:listaMovCuentasFechas_LIP>\n  </soapenv:Body>\n</soapenv:Envelope>\n  account\nend\n"

#xml_date(date) ⇒ Object



61
62
63
# File 'lib/bankscrap/openbank/account.rb', line 61

def xml_date(date)
  "<dia>#{date.day}</dia><mes>#{date.month}</mes><anyo>#{date.year}</anyo>"
end