Class: Financo::N26::Bank

Inherits:
Object
  • Object
show all
Defined in:
lib/financo/n26/bank.rb

Overview

Bank

Constant Summary collapse

MAX_PENDING_SECONDS =
31 * 24 * 60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(client: Client.new) ⇒ Bank

Returns a new instance of Bank.



9
10
11
12
13
# File 'lib/financo/n26/bank.rb', line 9

def initialize(client: Client.new)
  @client = client

  @account = nil
end

Instance Method Details

#accountObject



23
24
25
26
27
28
29
30
# File 'lib/financo/n26/bank.rb', line 23

def 
  @account ||=
    begin
      data = @client.me

      Financo::Bank::.new(*data.values_at('id', 'firstName'))
    end
end

#authenticate(username, password) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/financo/n26/bank.rb', line 15

def authenticate(username, password)
  @client.(username, password)

  @account = nil
rescue Financo::N26::ClientError
  raise Financo::Bank::AuthenticationError, 'invalid username or password'
end

#transactions(checking:, **options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/financo/n26/bank.rb', line 32

def transactions(checking:, **options)
  history_store = Financo::N26::HistoryStore.new(**options)
  history = history_store.load(.id)

  start_date = history.loaded_at - MAX_PENDING_SECONDS
  start_date = start_date.positive? ? start_date : 0

  end_date = Time.now.to_i

  result =
    @client
      .transactions(from: start_date, to: end_date)
      .map { |t| parse_n26_transaction(checking, t) }
      .each { |t| t.status = history.add(t.id, t.date, t.amount) }

  history.loaded_at = end_date
  history_store.save(.id, history)

  result
end