diff --git a/lib/lunchmoney/api_call.rb b/lib/lunchmoney/api_call.rb index b39f3c7..3aff000 100644 --- a/lib/lunchmoney/api_call.rb +++ b/lib/lunchmoney/api_call.rb @@ -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) diff --git a/lib/lunchmoney/budget/budget_calls.rb b/lib/lunchmoney/budget/budget_calls.rb index d2b571d..eec4155 100644 --- a/lib/lunchmoney/budget/budget_calls.rb +++ b/lib/lunchmoney/budget/budget_calls.rb @@ -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) @@ -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) diff --git a/lib/lunchmoney/categories/category_calls.rb b/lib/lunchmoney/categories/category_calls.rb index d90a203..5109cd4 100644 --- a/lib/lunchmoney/categories/category_calls.rb +++ b/lib/lunchmoney/categories/category_calls.rb @@ -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) diff --git a/lib/lunchmoney/plaid_accounts/plaid_account_calls.rb b/lib/lunchmoney/plaid_accounts/plaid_account_calls.rb index a82affb..986a43b 100644 --- a/lib/lunchmoney/plaid_accounts/plaid_account_calls.rb +++ b/lib/lunchmoney/plaid_accounts/plaid_account_calls.rb @@ -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) diff --git a/lib/lunchmoney/recurring_expenses/recurring_expense_calls.rb b/lib/lunchmoney/recurring_expenses/recurring_expense_calls.rb index 2629071..43da4a1 100644 --- a/lib/lunchmoney/recurring_expenses/recurring_expense_calls.rb +++ b/lib/lunchmoney/recurring_expenses/recurring_expense_calls.rb @@ -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? diff --git a/lib/lunchmoney/transactions/transaction_calls.rb b/lib/lunchmoney/transactions/transaction_calls.rb index 04d14b2..0709cf6 100644 --- a/lib/lunchmoney/transactions/transaction_calls.rb +++ b/lib/lunchmoney/transactions/transaction_calls.rb @@ -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? @@ -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? @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/test/lunchmoney/api_call_test.rb b/test/lunchmoney/api_call_test.rb index 6986d0b..37ba68c 100644 --- a/test/lunchmoney/api_call_test.rb +++ b/test/lunchmoney/api_call_test.rb @@ -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