Class: Bank

Inherits:
BridgeRecord show all
Defined in:
lib/ynap/models/bank.rb

Instance Attribute Summary collapse

Attributes inherited from BridgeRecord

#plaid_client, #ynab_client

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, plaid_access_token:, accounts: []) ⇒ Bank

Returns a new instance of Bank.



11
12
13
14
15
16
17
18
# File 'lib/ynap/models/bank.rb', line 11

def initialize(id:, name:, plaid_access_token:, accounts: [])
  super(plaid_access_token)
  @id                   = id
  @name                 = name
  @plaid_access_token   = plaid_access_token
  @accounts             = accounts.map { |params| Account.new params.merge(plaid_access_token: plaid_access_token) }
  @transactions_horizon = Account::TRANSACTIONS_HORIZON
end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



8
9
10
# File 'lib/ynap/models/bank.rb', line 8

def accounts
  @accounts
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/ynap/models/bank.rb', line 8

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/ynap/models/bank.rb', line 8

def name
  @name
end

#plaid_access_tokenObject (readonly)

Returns the value of attribute plaid_access_token.



8
9
10
# File 'lib/ynap/models/bank.rb', line 8

def plaid_access_token
  @plaid_access_token
end

#resultObject (readonly)

Returns the value of attribute result.



8
9
10
# File 'lib/ynap/models/bank.rb', line 8

def result
  @result
end

#transactions_horizonObject

Returns the value of attribute transactions_horizon.



9
10
11
# File 'lib/ynap/models/bank.rb', line 9

def transactions_horizon
  @transactions_horizon
end

Class Method Details

.accounts_descriptionsObject



30
31
32
# File 'lib/ynap/models/bank.rb', line 30

def self.accounts_descriptions
  all.map(&:accounts_descriptions).flatten.join("\n")
end

.allObject



24
25
26
27
28
# File 'lib/ynap/models/bank.rb', line 24

def self.all
  Ynap.config[:banks].map do |params|
    new params
  end
end

.find(id) ⇒ Object



20
21
22
# File 'lib/ynap/models/bank.rb', line 20

def self.find(id)
  new Ynap.bank_config(id)
end

.payees(with_memos: false) ⇒ Object



34
35
36
# File 'lib/ynap/models/bank.rb', line 34

def self.payees(with_memos: false)
  with_memos ? payees_memos : all.map(&:payees).flatten.uniq
end

.payees_memosObject



38
39
40
# File 'lib/ynap/models/bank.rb', line 38

def self.payees_memos
  all.map(&:payees_memos).flatten.uniq
end

Instance Method Details

#account(plaid_id:) ⇒ Object



60
61
62
# File 'lib/ynap/models/bank.rb', line 60

def (plaid_id:)
  accounts.find { || .plaid_id == plaid_id }
end

#accounts_descriptionsObject



56
57
58
# File 'lib/ynap/models/bank.rb', line 56

def accounts_descriptions
  accounts.map(&:description)
end

#all_plaid_accountsObject

Accounts



48
49
50
# File 'lib/ynap/models/bank.rb', line 48

def all_plaid_accounts
  @all_plaid_accounts ||= plaid_client.accounts.get(plaid_access_token).accounts
end

#all_plaid_idsObject



52
53
54
# File 'lib/ynap/models/bank.rb', line 52

def all_plaid_ids
  all_plaid_accounts.map { || { name: .name, official_name: .official_name, plaid_id: . } }
end

#fetch_plaid_transactions(from_date: nil, to_date: Date.today) ⇒ Object

Transactions



66
67
68
69
# File 'lib/ynap/models/bank.rb', line 66

def fetch_plaid_transactions(from_date: nil, to_date: Date.today)
  start_date = from_date || (to_date - @transactions_horizon)
  plaid_client.transactions.get(plaid_access_token, start_date, to_date).transactions
end

#importObject

Import



140
141
142
143
144
# File 'lib/ynap/models/bank.rb', line 140

def import
  @result = ynab_client.transactions.create_transactions(Ynap.config.dig(:ynab, :budget_id), wrapped_ynab_transactions).tap do |result|
    puts "#{name} - New: #{result.data.transaction_ids.size}, Known: #{result.data.duplicate_import_ids.size}\n"
  end
end

#importable_plaid_transactionsObject

Since we fetch all plaid transactions at once and accounts have various started_date, we need to filter line by line



77
78
79
80
81
82
# File 'lib/ynap/models/bank.rb', line 77

def importable_plaid_transactions
  @importable_plaid_transactions ||= plaid_transactions.filter do |transaction|
     = (plaid_id: transaction.)
    .start_date.nil? || Date.parse(transaction.date) >= .start_date
  end
end

#payeesObject



116
117
118
# File 'lib/ynap/models/bank.rb', line 116

def payees
  ynab_transactions.map(&:payee_name).uniq.sort
end

#payees_memosObject



120
121
122
# File 'lib/ynap/models/bank.rb', line 120

def payees_memos
  ynab_transactions.map { |t| [t.payee_name, t.memo].join(" <~> ") }.uniq.sort
end

#plaid_transactionsObject



71
72
73
# File 'lib/ynap/models/bank.rb', line 71

def plaid_transactions
  @plaid_transactions ||= fetch_plaid_transactions(from_date: nil, to_date: Date.today)
end

#reconciled?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/ynap/models/bank.rb', line 132

def reconciled?
  @bank.accounts.inject(true) { |reconciled, | reconciled && () }
end

#reconciliate_account(account) ⇒ Object

Balances



128
129
130
# File 'lib/ynap/models/bank.rb', line 128

def ()
  .ynab_balance + transactions_total() == .plaid_balance.to_ynab
end

#refresh_plaid_transactions!Object



84
85
86
# File 'lib/ynap/models/bank.rb', line 84

def refresh_plaid_transactions!
  plaid_client.transactions.refresh(plaid_access_token)
end

#transactions_for(ynab_id) ⇒ Object

Transactions Queries



108
109
110
# File 'lib/ynap/models/bank.rb', line 108

def transactions_for(ynab_id)
  ynab_transactions.select { |transaction| transaction. == ynab_id }
end

#transactions_total(account) ⇒ Object



112
113
114
# File 'lib/ynap/models/bank.rb', line 112

def transactions_total()
  transactions_for(.ynab_id).sum(&:amount)
end

#wrapped_ynab_transactionsObject



100
101
102
# File 'lib/ynap/models/bank.rb', line 100

def wrapped_ynab_transactions
  YNAB::SaveTransactionsWrapper.new(transactions: ynab_transactions)
end

#ynab_transactionsObject

YNABist



92
93
94
95
96
97
98
# File 'lib/ynap/models/bank.rb', line 92

def ynab_transactions
  @ynab_transactions ||= importable_plaid_transactions.map do |plaid_transaction|
     = (plaid_id: plaid_transaction.)
    converter = ParamsConverter.new , plaid_transaction
    YNAB::SaveTransaction.new(converter.to_params)
  end
end