Class: LunchMoney::Calls::Categories

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

Overview

Constant Summary collapse

VALID_FORMATS =

Valid query parameter formets for categories

T.let(
  [
    "flattened",
    "nested",
  ],
  T::Array[String],
)

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

#add_to_category_group(group_id, category_ids: [], new_categories: []) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/lunchmoney/calls/categories.rb', line 150

def add_to_category_group(group_id, category_ids: [], new_categories: [])
  params = {
    category_ids:,
    new_categories:,
  }

  response = post("categories/group/#{group_id}/add", params)

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

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

#categories(format: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lunchmoney/calls/categories.rb', line 24

def categories(format: nil)
  response = get("categories", query_params: categories_params(format:))

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

  response.body[:categories].map do |category|
    category[:children]&.map! { |child_category| LunchMoney::Objects::Category.new(**child_category) }

    LunchMoney::Objects::Category.new(**category)
  end
end

#category(category_id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/lunchmoney/calls/categories.rb', line 38

def category(category_id)
  response = get("categories/#{category_id}")

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

  response.body[:children]&.map! { |child_category| LunchMoney::Objects::ChildCategory.new(**child_category) }

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

#create_category(name:, description: nil, is_income: false, exclude_from_budget: false, exclude_from_totals: false, archived: false, group_id: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lunchmoney/calls/categories.rb', line 60

def create_category(name:, description: nil, is_income: false, exclude_from_budget: false,
  exclude_from_totals: false, archived: false, group_id: nil)
  params = clean_params({
    name:,
    description:,
    is_income:,
    exclude_from_budget:,
    exclude_from_totals:,
    archived:,
    group_id:,
  })
  response = post("categories", params)

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

  response.body
end

#create_category_group(name:, description: nil, is_income: false, exclude_from_budget: false, exclude_from_totals: false, category_ids: [], new_categories: []) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lunchmoney/calls/categories.rb', line 90

def create_category_group(name:, description: nil, is_income: false, exclude_from_budget: false,
  exclude_from_totals: false, category_ids: [], new_categories: [])

  params = {
    name:,
    description:,
    is_income:,
    exclude_from_budget:,
    exclude_from_totals:,
    category_ids:,
    new_categories:,
  }

  response = post("categories/group", params)

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

  response.body
end

#delete_category(category_id) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/lunchmoney/calls/categories.rb', line 165

def delete_category(category_id)
  response = delete("categories/#{category_id}")

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

  response.body
end

#force_delete_category(category_id) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/lunchmoney/calls/categories.rb', line 175

def force_delete_category(category_id)
  response = delete("categories/#{category_id}/force")

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

  response.body
end

#update_category(category_id, name: nil, description: nil, is_income: nil, exclude_from_budget: nil, exclude_from_totals: nil, archived: nil, group_id: nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lunchmoney/calls/categories.rb', line 123

def update_category(category_id, name: nil, description: nil, is_income: nil, exclude_from_budget: nil,
  exclude_from_totals: nil, archived: nil, group_id: nil)

  params = clean_params({
    name:,
    description:,
    is_income:,
    exclude_from_budget:,
    exclude_from_totals:,
    archived:,
    group_id:,
  })
  response = put("categories/#{category_id}", params)

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

  response.body
end