class Bankscrap::Arquia::Bank
Constants
- ACCOUNTS_ENDPOINT
- AUTH_ENDPOINT
- BASE_ENDPOINT
Define the endpoints for the
Bank
API here- PRODUCTS_ENDPOINT
- REQUIRED_CREDENTIALS
- TOKEN_ENDPOINT
- USER_ENDPOINT
Public Class Methods
new(credentials = {})
click to toggle source
Calls superclass method
# File lib/bankscrap/arquia/bank.rb, line 18 def initialize(credentials = {}) super do @nif = @nif.dup.upcase add_headers( 'Authorization' => 'Bearer', 'api-version' => '2', 'Content-Type' => 'application/json; charset=utf-8', 'Host' => 'api.arquia.es', 'User-Agent' => '' ) end end
Public Instance Methods
fetch_accounts()
click to toggle source
Fetch all the accounts for the given user
Should returns an array of Bankscrap::Account objects
# File lib/bankscrap/arquia/bank.rb, line 35 def fetch_accounts # First we need to fetch the products / views for the user user_response = JSON.parse(get(BASE_ENDPOINT + USER_ENDPOINT)) view = user_response['views'].find { |view| view['type'] == 0} view_id = view['id'] # Now we get the accounts for that product / view products_response = JSON.parse(get(BASE_ENDPOINT + PRODUCTS_ENDPOINT + "/#{view_id}")) # Find which products are accounts products_response.select! do |product| product['$type'] == 'Arquia.Backend.Domain.Models.Entities.Account, Arquia.Backend.Domain.Models' end products_response.map { |account| build_account(account) } end
fetch_transactions_for(account, start_date: Date.today - 1.month, end_date: Date.today)
click to toggle source
Fetch transactions for the given account.
Account should be a Bankscrap::Account object Should returns an array of Bankscrap::Account objects
# File lib/bankscrap/arquia/bank.rb, line 57 def fetch_transactions_for(account, start_date: Date.today - 1.month, end_date: Date.today) from = format_date(start_date) to = format_date(end_date) page = 1 transactions = [] loop do url = BASE_ENDPOINT + ACCOUNTS_ENDPOINT + "/#{account.id}/extract/from/#{from}/to/#{to}/page/#{page}" response = get(url) json = JSON.parse(response) transactions += json.map do |data| build_transaction(data, account) end page += 1 break unless json.any? end transactions end
Private Instance Methods
build_account(data)
click to toggle source
Build an Account object from API data
# File lib/bankscrap/arquia/bank.rb, line 113 def build_account(data) Account.new( bank: self, id: data['numProd'], name: data['description'], available_balance: Money.new(data['availableBalance'] * 100, 'EUR'), balance: Money.new(data['balance'] * 100, 'EUR'), iban: data['iban']['ibanCode'], description: "ARQUIA: #{data['description']}" ) end
build_transaction(data, account)
click to toggle source
Build a transaction object from API data
# File lib/bankscrap/arquia/bank.rb, line 126 def build_transaction(data, account) Transaction.new( account: account, id: data['id'], amount: Money.new(data['amount'].to_f * 100, 'EUR'), description: data['description'], effective_date: data['valueDate'].to_date, # Format is 2016-05-02T00:00:00 balance: Money.new(data['balance'].to_f * 100, 'EUR') ) end
format_date(date)
click to toggle source
# File lib/bankscrap/arquia/bank.rb, line 150 def format_date(date) date.strftime('%Y-%m-%d') end
get_correct_positions(keyboard_order, required_pw_positions)
click to toggle source
Example: @password = ‘12345678’ get_correct_positions
(“4912650378”, “03”) => “20” Required position 0 is “1”, which matches position 2 Required position 3 is “4”, which matches position 0
# File lib/bankscrap/arquia/bank.rb, line 144 def get_correct_positions(keyboard_order, required_pw_positions) required_pw_positions.chars.collect do |position| keyboard_order.index(@password[position.to_i]) end.join end
login()
click to toggle source
# File lib/bankscrap/arquia/bank.rb, line 79 def login # First step for login: request keyboard positions params = { "$type" => "Arquia.Backend.Domain.Models.ValueObjects.IdentificationData, Arquia.Backend.Domain.Models", "DocumentId" => @nif, "UserName" => @user } auth_response = JSON.parse(post(BASE_ENDPOINT + AUTH_ENDPOINT, fields: params.to_json)) keyboard_order = auth_response['order'] required_pw_positions = auth_response['positions'] identifier = auth_response['identifier'] # Second step: get token params = { 'client_id' => 'mobilefrontend', 'grant_type' => 'password', 'documentid' => @nif, 'username' => @user, 'positions' => get_correct_positions(keyboard_order, required_pw_positions), 'ChallengeId' => identifier, 'client_version' => '1.1.5' } # Note this request is a form type, not a json, that's why we don't use params.to_json token_response = JSON.parse(post(BASE_ENDPOINT + TOKEN_ENDPOINT, fields: params)) token = token_response['access_token'] # Set the token as header for future requests add_headers( 'Authorization' => "Bearer #{token}" ) end