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: []) ⇒ LunchMoney::Objects::Category, LunchMoney::Errors

Parameters:

  • group_id (Integer)
  • category_ids (Array<Integer>) (defaults to: [])
  • new_categories (Array<String>) (defaults to: [])

Returns:



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) ⇒ Array<LunchMoney::Objects::Category>, LunchMoney::Errors

Parameters:

  • format (String, Symbol, nil) (defaults to: nil)

Returns:



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) ⇒ LunchMoney::Objects::Category, LunchMoney::Errors

Parameters:

  • category_id (Integer)

Returns:



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) ⇒ Hash{Symbol => Integer}, LunchMoney::Errors

Parameters:

  • name (String)
  • description (String, nil) (defaults to: nil)
  • is_income (Boolean) (defaults to: false)
  • exclude_from_budget (Boolean) (defaults to: false)
  • exclude_from_totals (Boolean) (defaults to: false)
  • archived (Boolean) (defaults to: false)
  • group_id (Integer, nil) (defaults to: nil)

Returns:



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: []) ⇒ Hash{Symbol => Integer}, LunchMoney::Errors

Parameters:

  • name (String)
  • description (String, nil) (defaults to: nil)
  • is_income (Boolean) (defaults to: false)
  • exclude_from_budget (Boolean) (defaults to: false)
  • exclude_from_totals (Boolean) (defaults to: false)
  • category_ids (Array<Integer>) (defaults to: [])
  • new_categories (Array<String>) (defaults to: [])

Returns:



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) ⇒ Boolean, LunchMoney::Errors

Parameters:

  • category_id (Integer)

Returns:



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) ⇒ Boolean, LunchMoney::Errors

Parameters:

  • category_id (Integer)

Returns:



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) ⇒ Boolean, LunchMoney::Errors

Parameters:

  • category_id (Integer)
  • name (String, nil) (defaults to: nil)
  • description (String, nil) (defaults to: nil)
  • is_income (Boolean, nil) (defaults to: nil)
  • exclude_from_budget (Boolean, nil) (defaults to: nil)
  • exclude_from_totals (Boolean, nil) (defaults to: nil)
  • archived (Boolean, nil) (defaults to: nil)
  • group_id (Integer, nil) (defaults to: nil)

Returns:



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