Class: Bank
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
#accounts ⇒ Object
Returns the value of attribute accounts.
8
9
10
|
# File 'lib/ynap/models/bank.rb', line 8
def accounts
@accounts
end
|
#id ⇒ Object
Returns the value of attribute id.
8
9
10
|
# File 'lib/ynap/models/bank.rb', line 8
def id
@id
end
|
#name ⇒ Object
Returns the value of attribute name.
8
9
10
|
# File 'lib/ynap/models/bank.rb', line 8
def name
@name
end
|
#plaid_access_token ⇒ Object
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
|
#result ⇒ Object
Returns the value of attribute result.
8
9
10
|
# File 'lib/ynap/models/bank.rb', line 8
def result
@result
end
|
#transactions_horizon ⇒ Object
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_descriptions ⇒ Object
30
31
32
|
# File 'lib/ynap/models/bank.rb', line 30
def self.accounts_descriptions
all.map(&:accounts_descriptions).flatten.join("\n")
end
|
.all ⇒ Object
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_memos ⇒ Object
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 account(plaid_id:)
accounts.find { |account| account.plaid_id == plaid_id }
end
|
#accounts_descriptions ⇒ Object
56
57
58
|
# File 'lib/ynap/models/bank.rb', line 56
def accounts_descriptions
accounts.map(&:description)
end
|
#all_plaid_accounts ⇒ Object
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_ids ⇒ Object
52
53
54
|
# File 'lib/ynap/models/bank.rb', line 52
def all_plaid_ids
all_plaid_accounts.map { |account| { name: account.name, official_name: account.official_name, plaid_id: account.account_id } }
end
|
#fetch_plaid_transactions(from_date: nil, to_date: Date.today) ⇒ Object
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
|
#import ⇒ Object
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_transactions ⇒ Object
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|
account = account(plaid_id: transaction.account_id)
account.start_date.nil? || Date.parse(transaction.date) >= account.start_date
end
end
|
#payees ⇒ Object
116
117
118
|
# File 'lib/ynap/models/bank.rb', line 116
def payees
ynab_transactions.map(&:payee_name).uniq.sort
end
|
#payees_memos ⇒ Object
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_transactions ⇒ Object
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
132
133
134
|
# File 'lib/ynap/models/bank.rb', line 132
def reconciled?
@bank.accounts.inject(true) { |reconciled, account| reconciled && reconciliate_account(account) }
end
|
#reconciliate_account(account) ⇒ Object
128
129
130
|
# File 'lib/ynap/models/bank.rb', line 128
def reconciliate_account(account)
account.ynab_balance + transactions_total(account) == account.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
108
109
110
|
# File 'lib/ynap/models/bank.rb', line 108
def transactions_for(ynab_id)
ynab_transactions.select { |transaction| transaction.account_id == ynab_id }
end
|
#transactions_total(account) ⇒ Object
112
113
114
|
# File 'lib/ynap/models/bank.rb', line 112
def transactions_total(account)
transactions_for(account.ynab_id).sum(&:amount)
end
|
#wrapped_ynab_transactions ⇒ Object
100
101
102
|
# File 'lib/ynap/models/bank.rb', line 100
def wrapped_ynab_transactions
YNAB::SaveTransactionsWrapper.new(transactions: ynab_transactions)
end
|
#ynab_transactions ⇒ Object
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|
account = account(plaid_id: plaid_transaction.account_id)
converter = ParamsConverter.new account, plaid_transaction
YNAB::SaveTransaction.new(converter.to_params)
end
end
|