Class: Bankscrap::Arquia::Bank
- Inherits:
-
Bank
- Object
- Bank
- Bankscrap::Arquia::Bank
- Defined in:
- lib/bankscrap/arquia/bank.rb
Constant Summary collapse
- BASE_ENDPOINT =
Define the endpoints for the Bank API here
'https://api.arquia.es'.freeze
- AUTH_ENDPOINT =
'/api/Auth'.freeze
- TOKEN_ENDPOINT =
'/token'.freeze
- USER_ENDPOINT =
'/api/user'.freeze
- PRODUCTS_ENDPOINT =
'/api/products'.freeze
- ACCOUNTS_ENDPOINT =
'/api/accounts'.freeze
- REQUIRED_CREDENTIALS =
[:user, :password, :nif]
Instance Method Summary collapse
-
#fetch_accounts ⇒ Object
Fetch all the accounts for the given user.
-
#fetch_transactions_for(account, start_date: Date.today - 1.month, end_date: Date.today) ⇒ Object
Fetch transactions for the given account.
-
#initialize(credentials = {}) ⇒ Bank
constructor
A new instance of Bank.
Constructor Details
#initialize(credentials = {}) ⇒ Bank
Returns a new instance of Bank.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# 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 |
Instance Method Details
#fetch_accounts ⇒ Object
Fetch all the accounts for the given user
Should returns an array of Bankscrap::Account objects
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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) ⇒ Object
Fetch transactions for the given account.
Account should be a Bankscrap::Account object Should returns an array of Bankscrap::Account objects
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# 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 |