Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle empty params directly in get method #157

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/lunchmoney/api_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(api_key: nil)
def get(endpoint, query_params: nil)
connection = request(flat_params: true)

if query_params
if query_params.present?
connection.get(BASE_URL + endpoint, query_params)
else
connection.get(BASE_URL + endpoint)
Expand Down
15 changes: 2 additions & 13 deletions lib/lunchmoney/budget/budget_calls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ class BudgetCalls < ApiCall
).returns(T.any(T::Array[LunchMoney::Budget], LunchMoney::Errors))
end
def budgets(start_date:, end_date:, currency: nil)
params = clean_params({
start_date:,
end_date:,
currency:,
})

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

api_errors = errors(response)
Expand All @@ -44,13 +39,7 @@ def budgets(start_date:, end_date:, currency: nil)
))
end
def upsert_budget(start_date:, category_id:, amount:, currency: nil)
params = clean_params({
start_date:,
category_id:,
amount:,
currency:,
})

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

api_errors = errors(response)
Expand Down
1 change: 0 additions & 1 deletion lib/lunchmoney/categories/category_calls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def update_category(category_id, name: nil, description: nil, is_income: nil, ex
archived:,
group_id:,
})

response = put("categories/#{category_id}", params)

api_errors = errors(response)
Expand Down
7 changes: 1 addition & 6 deletions lib/lunchmoney/plaid_accounts/plaid_account_calls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ def plaid_accounts
).returns(T.any(T::Boolean, LunchMoney::Errors))
end
def plaid_accounts_fetch(start_date: nil, end_date: nil, plaid_account_id: nil)
params = clean_params({
start_date:,
end_date:,
plaid_account_id:,
})

params = clean_params({ start_date:, end_date:, plaid_account_id: })
response = post("plaid_accounts/fetch", params)

api_errors = errors(response)
Expand Down
12 changes: 2 additions & 10 deletions lib/lunchmoney/recurring_expenses/recurring_expense_calls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,8 @@ class RecurringExpenseCalls < ApiCall
).returns(T.any(T::Array[LunchMoney::RecurringExpense], LunchMoney::Errors))
end
def recurring_expenses(start_date: nil, end_date: nil)
params = clean_params({
start_date:,
end_date:,
})

response = if params.empty?
get("recurring_expenses")
else
get("recurring_expenses", query_params: params)
end
params = clean_params({ start_date:, end_date: })
response = get("recurring_expenses", query_params: params)

api_errors = errors(response)
return api_errors if api_errors.present?
Expand Down
27 changes: 4 additions & 23 deletions lib/lunchmoney/transactions/transaction_calls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ def transactions(
offset:,
limit:,
})

response = if params.empty?
get("transactions")
else
get("transactions", query_params: params)
end
response = get("transactions", query_params: params)

api_errors = errors(response)
return api_errors if api_errors.present?
Expand All @@ -85,15 +80,8 @@ def transactions(
).returns(T.any(LunchMoney::Transaction, LunchMoney::Errors))
end
def transaction(transaction_id, debit_as_negative: nil)
params = clean_params({
debit_as_negative:,
})

response = if params.empty?
get("transactions/#{transaction_id}")
else
get("transactions/#{transaction_id}", query_params: params)
end
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?
Expand Down Expand Up @@ -121,7 +109,6 @@ def insert_transactions(transactions, apply_rules: nil, skip_duplicates: nil,
debit_as_negative:,
skip_balance_update:,
})

response = post("transactions", params)

api_errors = errors(response)
Expand All @@ -147,7 +134,6 @@ def update_transaction(transaction_id, transaction:, split: nil,
debit_as_negative:,
skip_balance_update:,
})

response = put("transactions/#{transaction_id}", params)

api_errors = errors(response)
Expand All @@ -163,11 +149,7 @@ def update_transaction(transaction_id, transaction:, split: nil,
).returns(T.any(T::Array[Integer], LunchMoney::Errors))
end
def unsplit_transaction(parent_ids, remove_parents:)
params = clean_params({
parent_ids:,
remove_parents:,
})

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

api_errors = errors(response)
Expand Down Expand Up @@ -205,7 +187,6 @@ def create_transaction_group(date:, payee:, transactions:, category_id: nil, not
notes:,
tags:,
})

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

api_errors = errors(response)
Expand Down
10 changes: 10 additions & 0 deletions test/lunchmoney/api_call_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ class ApiCallTest < ActiveSupport::TestCase

assert_equal(2, cleaned_params.keys.count)
end

test "get does not send query params when empty" do
response = mock_faraday_response({})
endpoint = LunchMoney::ApiCall::BASE_URL + "me"

Faraday::Connection.any_instance.expects(:get).with(endpoint).returns(response).once
Faraday::Connection.any_instance.expects(:get).with(endpoint, {}).returns(response).never

LunchMoney::ApiCall.new.send(:get, "me", query_params: {})
end
end