Class: LunchMoney::Calls::Budgets

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

Overview

Constant Summary

Constants inherited from Base

LunchMoney::Calls::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

#budgets(start_date:, end_date:, currency: nil) ⇒ Object



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/lunchmoney/calls/budgets.rb', line 17

def budgets(start_date:, end_date:, currency: nil)
  params = clean_params({ start_date:, end_date:, currency: })
  response = get("budgets", query_params: params)

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

  response.body.map do |budget|
    if budget[:data]
      data_keys = budget[:data].keys
      data_keys.each do |data_key|
        budget[:data][data_key] = LunchMoney::Objects::Data.new(**budget[:data][data_key])
      end
    end

    if budget[:config]
      config_keys = budget[:config].keys
      config_keys.each do |config_key|
        budget[:config][config_key] = LunchMoney::Objects::Data.new(**budget[:config][config_key])
      end
    end

    if budget[:recurring]
      budget[:recurring][:list]&.map! { |recurring| LunchMoney::Objects::RecurringExpenseBase.new(**recurring) }
    end

    LunchMoney::Objects::Budget.new(**budget)
  end
end

#remove_budget(start_date:, category_id:) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lunchmoney/calls/budgets.rb', line 69

def remove_budget(start_date:, category_id:)
  params = {
    start_date:,
    category_id:,
  }

  response = delete("budgets", query_params: params)

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

  response.body
end

#upsert_budget(start_date:, category_id:, amount:, currency: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/lunchmoney/calls/budgets.rb', line 58

def upsert_budget(start_date:, category_id:, amount:, currency: nil)
  params = clean_params({ start_date:, category_id:, amount:, currency: })
  response = put("budgets", params)

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

  response.body
end