Class: LunchMoney::Calls::Transactions

Inherits:
Base
  • Object
show all
Defined in:
lib/lunchmoney/calls/transactions.rb

Overview

Constant Summary

Constants inherited from Base

Base::BASE_URL

Instance Attribute Summary

Attributes inherited from Base

#api_key

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from LunchMoney::Calls::Base

Instance Method Details

#create_transaction_group(date:, payee:, transactions:, category_id: nil, notes: nil, tags: nil) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/lunchmoney/calls/transactions.rb', line 187

def create_transaction_group(date:, payee:, transactions:, category_id: nil, notes: nil, tags: nil)
  params = clean_params({
    date:,
    payee:,
    transactions:,
    category_id:,
    notes:,
    tags:,
  })
  response = post("transactions/group", params)

  api_errors = errors(response)
  return api_errors if api_errors.present?

  response.body
end

#delete_transaction_group(transaction_id) ⇒ Object



210
211
212
213
214
215
216
217
# File 'lib/lunchmoney/calls/transactions.rb', line 210

def delete_transaction_group(transaction_id)
  response = delete("transactions/group/#{transaction_id}")

  api_errors = errors(response)
  return api_errors if api_errors.present?

  response.body
end

#insert_transactions(transactions, apply_rules: nil, skip_duplicates: nil, check_for_recurring: nil, debit_as_negative: nil, skip_balance_update: nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lunchmoney/calls/transactions.rb', line 103

def insert_transactions(transactions, apply_rules: nil, skip_duplicates: nil,
  check_for_recurring: nil, debit_as_negative: nil, skip_balance_update: nil)
  params = clean_params({
    transactions: transactions.map(&:serialize),
    apply_rules:,
    skip_duplicates:,
    check_for_recurring:,
    debit_as_negative:,
    skip_balance_update:,
  })
  response = post("transactions", params)

  api_errors = errors(response)
  return api_errors if api_errors.present?

  response.body
end

#transaction(transaction_id, debit_as_negative: nil) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/lunchmoney/calls/transactions.rb', line 83

def transaction(transaction_id, debit_as_negative: nil)
  params = clean_params({ debit_as_negative: })
  response = get("transactions/#{transaction_id}", query_params: params)

  api_errors = errors(response)
  return api_errors if api_errors.present?

  LunchMoney::Objects::Transaction.new(**response.body)
end

#transaction_group(transaction_id) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/lunchmoney/calls/transactions.rb', line 168

def transaction_group(transaction_id)
  response = get("transactions/group", query_params: { transaction_id: })

  api_errors = errors(response)
  return api_errors if api_errors.present?

  LunchMoney::Objects::Transaction.new(**response.body)
end

#transactions(tag_id: nil, recurring_id: nil, plaid_account_id: nil, category_id: nil, asset_id: nil, is_group: nil, status: nil, start_date: nil, end_date: nil, debit_as_negative: nil, pending: nil, offset: nil, limit: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
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
69
70
71
72
73
74
75
# File 'lib/lunchmoney/calls/transactions.rb', line 30

def transactions(
  tag_id: nil,
  recurring_id: nil,
  plaid_account_id: nil,
  category_id: nil,
  asset_id: nil,
  is_group: nil,
  status: nil,
  start_date: nil,
  end_date: nil,
  debit_as_negative: nil,
  pending: nil,
  offset: nil,
  limit: nil
)

  params = clean_params({
    tag_id:,
    recurring_id:,
    plaid_account_id:,
    category_id:,
    asset_id:,
    is_group:,
    status:,
    start_date:,
    end_date:,
    debit_as_negative:,
    pending:,
    offset:,
    limit:,
  })
  response = get("transactions", query_params: params)

  api_errors = errors(response)
  return api_errors if api_errors.present?

  response.body[:transactions].map do |transaction|
    transaction[:tags].map! { |tag| LunchMoney::Objects::TagBase.new(**tag) }

    transaction[:children]&.map! do |child_transaction|
      LunchMoney::Objects::ChildTransaction.new(**child_transaction)
    end

    LunchMoney::Objects::Transaction.new(**transaction)
  end
end

#unsplit_transaction(parent_ids, remove_parents: false) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/lunchmoney/calls/transactions.rb', line 157

def unsplit_transaction(parent_ids, remove_parents: false)
  params = { parent_ids:, remove_parents: }
  response = post("transactions/unsplit", params)

  api_errors = errors(response)
  return api_errors if api_errors.present?

  response.body
end

#update_transaction(transaction_id, transaction: nil, split: nil, debit_as_negative: nil, skip_balance_update: nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/lunchmoney/calls/transactions.rb', line 130

def update_transaction(transaction_id, transaction: nil, split: nil,
  debit_as_negative: nil, skip_balance_update: nil)
  raise(
    LunchMoney::MissingArgument,
    "Either a transaction or split must be provided",
  ) if transaction.nil? && split.nil?

  params = clean_params({
    transaction: transaction&.serialize,
    split: split&.map!(&:serialize),
    debit_as_negative:,
    skip_balance_update:,
  })
  response = put("transactions/#{transaction_id}", params)

  api_errors = errors(response)
  return api_errors if api_errors.present?

  response.body
end