Class: Ynaby::Account

Inherits:
Base
  • Object
show all
Defined in:
lib/ynaby/account.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#ynab_client

Constructor Details

#initialize(id:, name:, type:, on_budget:, closed:, note:, balance:, cleared_balance:, uncleared_balance:, budget:) ⇒ Account

Returns a new instance of Account.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ynaby/account.rb', line 5

def initialize(id:,
               name:,
               type:,
               on_budget:,
               closed:,
               note:,
               balance:,
               cleared_balance:,
               uncleared_balance:,
               budget:)

  @id = id
  @name = name
  @type = type
  @on_budget = on_budget
  @closed = closed
  @note = note
  @balance = balance
  @cleared_balance = cleared_balance
  @uncleared_balance = uncleared_balance
  @budget = budget
end

Instance Attribute Details

#balanceObject (readonly)

Returns the value of attribute balance.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def balance
  @balance
end

#budgetObject (readonly)

Returns the value of attribute budget.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def budget
  @budget
end

#cleared_balanceObject (readonly)

Returns the value of attribute cleared_balance.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def cleared_balance
  @cleared_balance
end

#closedObject (readonly)

Returns the value of attribute closed.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def closed
  @closed
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def name
  @name
end

#noteObject (readonly)

Returns the value of attribute note.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def note
  @note
end

#on_budgetObject (readonly)

Returns the value of attribute on_budget.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def on_budget
  @on_budget
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def type
  @type
end

#uncleared_balanceObject (readonly)

Returns the value of attribute uncleared_balance.



3
4
5
# File 'lib/ynaby/account.rb', line 3

def uncleared_balance
  @uncleared_balance
end

Class Method Details

.parse(object:, budget:) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ynaby/account.rb', line 76

def self.parse(object:, budget:)
  new(
    id: object.id,
    name: object.name,
    type: object.type,
    on_budget: object.on_budget,
    closed: object.closed,
    note: object.note,
    balance: object.balance,
    cleared_balance: object.cleared_balance,
    uncleared_balance: object.uncleared_balance,
    budget: budget
  )
end

Instance Method Details

#api_tokenObject



91
92
93
# File 'lib/ynaby/account.rb', line 91

def api_token
  budget.api_token
end

#bulk_upload_transactions(transactions) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ynaby/account.rb', line 42

def bulk_upload_transactions(transactions)
  if transactions.to_a.empty?
    return {
      new: 0,
      updated: 0
    }
  end

  body = {
    transactions: transactions.map(&:upload_hash)
  }

  response = ynab_client.transactions.bulk_create_transactions(budget.id, body)
  duplicate_transactions = response.data.bulk.duplicate_import_ids

  if duplicate_transactions.any?
    update_duplicate_transactions(
      new_transactions: transactions,
      duplicate_transactions_ids: response.data.bulk.duplicate_import_ids
    )
  end

  {
    new: response.data.bulk.transaction_ids.count,
    updated: duplicate_transactions.count
  }
end

#transaction(transaction_id) ⇒ Object



70
71
72
73
74
# File 'lib/ynaby/account.rb', line 70

def transaction(transaction_id)
  response = ynab_client.transactions.get_transaction_by_id(budget.id, transaction_id)

  Transaction.parse(object: response.data.transaction, account: self)
end

#transactions(since: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ynaby/account.rb', line 28

def transactions(since: nil)
  response = ynab_client
    .transactions
    .(
      budget.id,
      @id,
      since_date: since&.to_date&.iso8601
    )

  response.data.transactions.map do |transaction|
    Transaction.parse(object: transaction, account: self)
  end
end