Class: Account

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

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

Returns a new instance of 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_idObject (readonly)

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_dateObject (readonly)

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_idObject (readonly)

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

#balancesObject



23
24
25
# File 'lib/ynap/models/account.rb', line 23

def balances
  { plaid: plaid_balance, ynab: ynab_balance.to_plaid }
end

#descriptionObject



18
19
20
21
# File 'lib/ynap/models/account.rb', line 18

def description
  reconciled = reconciled? ? "" : ""
  "#{.name}: #{plaid_balance} P#{reconciled}Y #{ynab_balance.to_plaid} #{.balances.iso_currency_code}"
end

#plaid_accountObject



37
38
39
# File 'lib/ynap/models/account.rb', line 37

def 
  @plaid_account ||= plaid_accounts.find { || . == plaid_id }
end

#plaid_accountsObject

Plaid



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_balanceObject



41
42
43
# File 'lib/ynap/models/account.rb', line 41

def plaid_balance
  @plaid_balance ||= .balances.available
end

#plaid_transactionsObject



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

Returns:

  • (Boolean)


27
28
29
# File 'lib/ynap/models/account.rb', line 27

def reconciled?
  plaid_balance.to_ynab == ynab_balance
end

#ynab_accountObject

YNAB



51
52
53
# File 'lib/ynap/models/account.rb', line 51

def 
  @ynab_account ||= ynab_client.accounts.(Ynap.config.dig(:ynab, :budget_id), ynab_id).data.
end

#ynab_balanceObject



55
56
57
# File 'lib/ynap/models/account.rb', line 55

def ynab_balance
  @ynab_balance ||= .balance
end

#ynab_transactionsObject



59
60
61
# File 'lib/ynap/models/account.rb', line 59

def ynab_transactions
  @ynab_transactions ||= ynab_client.transactions.(Ynap.config.dig(:ynab, :budget_id), ynab_id, since_date: @from_date).data.transactions.reverse
end