Class: Ynaby::Transaction

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#ynab_client

Constructor Details

#initialize(id: nil, date:, amount:, memo: nil, cleared: nil, approved: nil, flag_color: nil, payee_id: nil, category_id: nil, transfer_account_id: nil, transfer_transaction_id: nil, import_id: nil, account_name: nil, payee_name:, category_name: nil, account:, subtransactions: nil) ⇒ Transaction

Returns a new instance of Transaction.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ynaby/transaction.rb', line 10

def initialize(id: nil,
               date:,
               amount:,
               memo: nil,
               cleared: nil,
               approved: nil,
               flag_color: nil,
               payee_id: nil,
               category_id: nil,
               transfer_account_id: nil,
               transfer_transaction_id: nil,
               import_id: nil,
               account_name: nil,
               payee_name:,
               category_name: nil,
               account:,
               subtransactions: nil)

  @id = id
  @date = date
  @amount = amount
  @memo = memo
  @cleared = cleared
  @approved = approved
  @flag_color = flag_color
  @payee_id = payee_id
  @category_id = category_id
  @transfer_account_id = 
  @transfer_transaction_id = transfer_transaction_id
  @import_id = import_id
  @account_name = 
  @payee_name = payee_name
  @category_name = category_name
  @account = 
  @subtransactions = subtransactions
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



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

def 
  @account
end

#account_nameObject (readonly)

Returns the value of attribute account_name.



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

def 
  @account_name
end

#amountObject

Returns the value of attribute amount.



8
9
10
# File 'lib/ynaby/transaction.rb', line 8

def amount
  @amount
end

#approvedObject (readonly)

Returns the value of attribute approved.



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

def approved
  @approved
end

#category_idObject (readonly)

Returns the value of attribute category_id.



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

def category_id
  @category_id
end

#category_nameObject (readonly)

Returns the value of attribute category_name.



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

def category_name
  @category_name
end

#clearedObject (readonly)

Returns the value of attribute cleared.



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

def cleared
  @cleared
end

#dateObject (readonly)

Returns the value of attribute date.



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

def date
  @date
end

#flag_colorObject (readonly)

Returns the value of attribute flag_color.



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

def flag_color
  @flag_color
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/ynaby/transaction.rb', line 8

def id
  @id
end

#import_idObject (readonly)

Returns the value of attribute import_id.



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

def import_id
  @import_id
end

#memoObject

Returns the value of attribute memo.



8
9
10
# File 'lib/ynaby/transaction.rb', line 8

def memo
  @memo
end

#payee_idObject (readonly)

Returns the value of attribute payee_id.



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

def payee_id
  @payee_id
end

#payee_nameObject (readonly)

Returns the value of attribute payee_name.



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

def payee_name
  @payee_name
end

#subtransactionsObject (readonly)

Returns the value of attribute subtransactions.



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

def subtransactions
  @subtransactions
end

#transfer_account_idObject (readonly)

Returns the value of attribute transfer_account_id.



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

def 
  @transfer_account_id
end

#transfer_transaction_idObject (readonly)

Returns the value of attribute transfer_transaction_id.



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

def transfer_transaction_id
  @transfer_transaction_id
end

Class Method Details

.parse(object:, account:) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ynaby/transaction.rb', line 87

def self.parse(object:, account:)
  new(
    id: object.id,
    date: object.date,
    amount: object.amount.to_i,
    memo: object.memo,
    cleared: object.cleared,
    approved: object.approved,
    flag_color: object.flag_color,
    payee_id: object.payee_id,
    category_id: object.category_id,
    transfer_account_id: object.,
    transfer_transaction_id: object.transfer_transaction_id,
    import_id: object.import_id,
    account_name: object.,
    payee_name: object.payee_name,
    category_name: object.category_name,
    account: ,
    subtransactions: object.subtransactions.map(&:to_body)
  )
end

Instance Method Details

#updateObject



77
78
79
80
81
82
83
84
85
# File 'lib/ynaby/transaction.rb', line 77

def update
  body = {
    transaction: upload_hash
  }

  response = ynab_client.transactions.update_transaction(budget_id, id, body)

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

#uploadObject



47
48
49
50
51
52
53
54
55
# File 'lib/ynaby/transaction.rb', line 47

def upload
  body = {
    transaction: upload_hash
  }

  response = ynab_client.transactions.create_transaction(budget_id, body)

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

#upload_hashObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ynaby/transaction.rb', line 57

def upload_hash
  hash = {
    account_id: @account.id,
    date: @date.to_date.iso8601,
    amount: @amount,
    import_id: @import_id,
    memo: @memo&.slice(0...50)
  }

  if @transfer_account_id && @transfer_transaction_id
    hash[:transfer_account_id] = @transfer_account_id
    hash[:transfer_transaction_id] = @transfer_transaction_id
  else
    hash[:payee_name] = @payee_name
    hash[:payee_id] = nil
  end

  hash
end