Class: Account
Constant Summary
collapse
- TRANSACTIONS_HORIZON =
30
Instance Attribute Summary collapse
Attributes inherited from BridgeRecord
#plaid_access_token, #plaid_client, #ynab_client
Instance Method Summary
collapse
Constructor Details
#initialize(plaid_id:, plaid_access_token:, ynab_id: nil, start_date: nil) ⇒ Account
9
10
11
12
13
14
15
16
|
# File 'lib/ynap/models/account.rb', line 9
def initialize(plaid_id:, plaid_access_token:, ynab_id: nil, start_date: nil)
super(plaid_access_token)
@plaid_id = plaid_id
@ynab_id = ynab_id
@start_date = Date.parse(start_date) unless start_date.nil?
@to_date = Date.today
@from_date = @start_date || (@to_date - TRANSACTIONS_HORIZON)
end
|
Instance Attribute Details
#plaid_id ⇒ Object
Returns the value of attribute plaid_id.
7
8
9
|
# File 'lib/ynap/models/account.rb', line 7
def plaid_id
@plaid_id
end
|
#start_date ⇒ Object
Returns the value of attribute start_date.
7
8
9
|
# File 'lib/ynap/models/account.rb', line 7
def start_date
@start_date
end
|
#ynab_id ⇒ Object
Returns the value of attribute ynab_id.
7
8
9
|
# File 'lib/ynap/models/account.rb', line 7
def ynab_id
@ynab_id
end
|
Instance Method Details
#balances ⇒ Object
23
24
25
|
# File 'lib/ynap/models/account.rb', line 23
def balances
{ plaid: plaid_balance, ynab: ynab_balance.to_plaid }
end
|
#description ⇒ Object
18
19
20
21
|
# File 'lib/ynap/models/account.rb', line 18
def description
reconciled = reconciled? ? "✅" : "❌"
"#{ynab_account.name}: #{plaid_balance} P#{reconciled}Y #{ynab_balance.to_plaid} #{plaid_account.balances.iso_currency_code}"
end
|
#plaid_account ⇒ Object
37
38
39
|
# File 'lib/ynap/models/account.rb', line 37
def plaid_account
@plaid_account ||= plaid_accounts.find { |account| account.account_id == plaid_id }
end
|
#plaid_accounts ⇒ Object
33
34
35
|
# File 'lib/ynap/models/account.rb', line 33
def plaid_accounts
@plaid_accounts ||= plaid_client.accounts.balance.get(plaid_access_token).accounts
end
|
#plaid_balance ⇒ Object
41
42
43
|
# File 'lib/ynap/models/account.rb', line 41
def plaid_balance
@plaid_balance ||= plaid_account.balances.available
end
|
#plaid_transactions ⇒ Object
45
46
47
|
# File 'lib/ynap/models/account.rb', line 45
def plaid_transactions
@plaid_transactions ||= plaid_client.transactions.get(plaid_access_token, @from_date, @to_date, account_ids: [plaid_id]).transactions
end
|
#reconciled? ⇒ Boolean
27
28
29
|
# File 'lib/ynap/models/account.rb', line 27
def reconciled?
plaid_balance.to_ynab == ynab_balance
end
|
#ynab_account ⇒ Object
51
52
53
|
# File 'lib/ynap/models/account.rb', line 51
def ynab_account
@ynab_account ||= ynab_client.accounts.get_account_by_id(Ynap.config.dig(:ynab, :budget_id), ynab_id).data.account
end
|
#ynab_balance ⇒ Object
55
56
57
|
# File 'lib/ynap/models/account.rb', line 55
def ynab_balance
@ynab_balance ||= ynab_account.balance
end
|
#ynab_transactions ⇒ Object
59
60
61
|
# File 'lib/ynap/models/account.rb', line 59
def ynab_transactions
@ynab_transactions ||= ynab_client.transactions.get_transactions_by_account(Ynap.config.dig(:ynab, :budget_id), ynab_id, since_date: @from_date).data.transactions.reverse
end
|