Class: LunchMoney::Calls::Categories

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

Overview

https://lunchmoney.dev/#categories

Constant Summary collapse

VALID_FORMATS =

Valid query parameter formats 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:



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/lunchmoney/calls/categories.rb', line 143

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

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

  handle_api_response(response) do |body|
    LunchMoney::Objects::Category.new(**body)
  end
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
# File 'lib/lunchmoney/calls/categories.rb', line 24

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

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

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

#category(category_id) ⇒ LunchMoney::Objects::Category, LunchMoney::Errors

Parameters:

  • category_id (Integer)

Returns:



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

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

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

    LunchMoney::Objects::Category.new(**body)
  end
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:



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

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)

  handle_api_response(response) do |body|
    body
  end
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:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lunchmoney/calls/categories.rb', line 87

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)

  handle_api_response(response) do |body|
    body
  end
end

#delete_category(category_id) ⇒ Boolean, LunchMoney::Errors

Parameters:

  • category_id (Integer)

Returns:



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

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

  handle_api_response(response) do |body|
    body
  end
end

#force_delete_category(category_id) ⇒ Boolean, LunchMoney::Errors

Parameters:

  • category_id (Integer)

Returns:



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

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

  handle_api_response(response) do |body|
    body
  end
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:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lunchmoney/calls/categories.rb', line 118

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)

  handle_api_response(response) do |body|
    body
  end
end