From b677c9612694ccf86e6ccf1c776288934483eb8a Mon Sep 17 00:00:00 2001 From: halorrr Date: Wed, 31 Jan 2024 00:27:23 -0500 Subject: [PATCH] redesign error object to act more like an array --- lib/lunchmoney/api_call.rb | 15 ++++----- lib/lunchmoney/error.rb | 18 ----------- lib/lunchmoney/errors.rb | 19 +++++++++++ test/lunchmoney/api_call_test.rb | 4 +-- test/lunchmoney/assets/asset_calls_test.rb | 2 +- test/lunchmoney/budget/budget_calls_test.rb | 14 +++----- .../categories/category_calls_test.rb | 32 +++++-------------- test/lunchmoney/crypto/crypto_calls_test.rb | 8 ++--- .../plaid_account_calls_test.rb | 8 ++--- .../recurring_expense_calls_test.rb | 4 +-- test/lunchmoney/tags/tag_calls_test.rb | 4 +-- .../transactions/transaction_calls_test.rb | 32 +++++-------------- test/lunchmoney/user/user_calls_test.rb | 4 +-- 13 files changed, 55 insertions(+), 109 deletions(-) delete mode 100644 lib/lunchmoney/error.rb create mode 100644 lib/lunchmoney/errors.rb diff --git a/lib/lunchmoney/api_call.rb b/lib/lunchmoney/api_call.rb index 70dac4f..14b42af 100644 --- a/lib/lunchmoney/api_call.rb +++ b/lib/lunchmoney/api_call.rb @@ -1,7 +1,7 @@ # typed: strict # frozen_string_literal: true -require_relative "error" +require_relative "errors" module LunchMoney # Base class for all API call types @@ -69,23 +69,20 @@ def errors(response) return parse_errors(body) unless error_hash(body).nil? - [] + LunchMoney::Errors.new end sig { params(body: T::Hash[Symbol, T.any(String, T::Array[String])]).returns(LunchMoney::Errors) } def parse_errors(body) errors = error_hash(body) - return [] if errors.blank? - - api_errors = [] + api_errors = LunchMoney::Errors.new + return api_errors if errors.blank? case errors when String - api_errors << LunchMoney::Error.new(message: errors) + api_errors << errors when Array - errors.each do |error| - api_errors << LunchMoney::Error.new(message: error) - end + errors.each { |error| api_errors << error } end api_errors diff --git a/lib/lunchmoney/error.rb b/lib/lunchmoney/error.rb deleted file mode 100644 index 6724624..0000000 --- a/lib/lunchmoney/error.rb +++ /dev/null @@ -1,18 +0,0 @@ -# typed: strict -# frozen_string_literal: true - -module LunchMoney - # This class is used to represent errors returned directly from the LunchMoney API - class Error - sig { returns(String) } - attr_reader :message - - sig { params(message: String).void } - def initialize(message:) - @message = message - end - end -end - -# A type alias for an array of LunchMoney::Error -LunchMoney::Errors = T.type_alias { T::Array[LunchMoney::Error] } diff --git a/lib/lunchmoney/errors.rb b/lib/lunchmoney/errors.rb new file mode 100644 index 0000000..b849624 --- /dev/null +++ b/lib/lunchmoney/errors.rb @@ -0,0 +1,19 @@ +# typed: strict +# frozen_string_literal: true + +module LunchMoney + # This class is used to represent errors returned directly from the LunchMoney API + class Errors + sig { returns(T::Array[String]) } + attr_accessor :messages + + sig { params(message: T.nilable(String)).void } + def initialize(message: nil) + @messages = T.let([], T::Array[String]) + + @messages << message unless message.nil? + end + + delegate :[], :<<, :each, :to_a, :first, :last, :empty?, :present?, to: :@messages + end +end diff --git a/test/lunchmoney/api_call_test.rb b/test/lunchmoney/api_call_test.rb index 37ba68c..21aad1c 100644 --- a/test/lunchmoney/api_call_test.rb +++ b/test/lunchmoney/api_call_test.rb @@ -23,7 +23,7 @@ class ApiCallTest < ActiveSupport::TestCase refute_empty(errors) - assert_equal(error_message, errors.first.message) + assert_equal(error_message, errors.first) end test "errors returns errors when multiple errors are returned" do @@ -35,7 +35,7 @@ class ApiCallTest < ActiveSupport::TestCase refute_empty(errors) errors.each do |error| - assert_includes(error_messages, error.message) + assert_includes(error_messages, error) end end diff --git a/test/lunchmoney/assets/asset_calls_test.rb b/test/lunchmoney/assets/asset_calls_test.rb index 5813204..6bb1b04 100644 --- a/test/lunchmoney/assets/asset_calls_test.rb +++ b/test/lunchmoney/assets/asset_calls_test.rb @@ -25,6 +25,6 @@ class AssetCallsTest < ActiveSupport::TestCase api_call = LunchMoney::AssetCalls.new.assets - assert_kind_of(LunchMoney::Error, api_call.first) + assert_kind_of(LunchMoney::Errors, api_call) end end diff --git a/test/lunchmoney/budget/budget_calls_test.rb b/test/lunchmoney/budget/budget_calls_test.rb index d94b0ab..8802e88 100644 --- a/test/lunchmoney/budget/budget_calls_test.rb +++ b/test/lunchmoney/budget/budget_calls_test.rb @@ -25,9 +25,7 @@ class BudgetCallsTest < ActiveSupport::TestCase api_call = LunchMoney::BudgetCalls.new.budgets(start_date: "2023-01-01", end_date: "2024-01-01") - api_call.each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "upsert_budget does not return an error on success response" do @@ -38,9 +36,7 @@ class BudgetCallsTest < ActiveSupport::TestCase amount: 400.99, ) - api_call.each do |group| - refute_kind_of(LunchMoney::Error, group) - end + refute_kind_of(LunchMoney::Errors, api_call) end end @@ -54,7 +50,7 @@ class BudgetCallsTest < ActiveSupport::TestCase amount: 400.99, ) - assert_kind_of(LunchMoney::Error, api_call.first) + assert_kind_of(LunchMoney::Errors, api_call) end test "remove_budget returns a boolean on success response" do @@ -71,8 +67,6 @@ class BudgetCallsTest < ActiveSupport::TestCase api_call = LunchMoney::BudgetCalls.new.remove_budget(start_date: "2023-01-01", category_id: 777052) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end end diff --git a/test/lunchmoney/categories/category_calls_test.rb b/test/lunchmoney/categories/category_calls_test.rb index 8574c0f..9e06cd8 100644 --- a/test/lunchmoney/categories/category_calls_test.rb +++ b/test/lunchmoney/categories/category_calls_test.rb @@ -25,9 +25,7 @@ class CategoryCallsTest < ActiveSupport::TestCase api_call = LunchMoney::CategoryCalls.new.categories(format: "flattened") - api_call.each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "categories does not raise an error when called with flattened format" do @@ -89,9 +87,7 @@ class CategoryCallsTest < ActiveSupport::TestCase VCR.use_cassette("categories/category_does_not_exist_failure") do api_call = LunchMoney::CategoryCalls.new.category(1) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end end end @@ -111,9 +107,7 @@ class CategoryCallsTest < ActiveSupport::TestCase api_call = LunchMoney::CategoryCalls.new.create_category(name: "Create Category Test") - api_call.each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "create_category_group returns anid of created category group on success response" do @@ -131,9 +125,7 @@ class CategoryCallsTest < ActiveSupport::TestCase api_call = LunchMoney::CategoryCalls.new.create_category_group(name: "Create Category Group Test") - api_call.each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "update_category returns a boolean on success response" do @@ -150,9 +142,7 @@ class CategoryCallsTest < ActiveSupport::TestCase api_call = LunchMoney::CategoryCalls.new.update_category(784587, name: "Update Category Test") - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "add_to_category_group returns a Category object on success response" do @@ -169,9 +159,7 @@ class CategoryCallsTest < ActiveSupport::TestCase api_call = LunchMoney::CategoryCalls.new.add_to_category_group(784588, new_categories: ["New Category Test"]) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "delete_category returns a boolean on success response" do @@ -188,9 +176,7 @@ class CategoryCallsTest < ActiveSupport::TestCase api_call = LunchMoney::CategoryCalls.new.delete_category(784587) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "force_delete_category returns a boolean on success response" do @@ -207,8 +193,6 @@ class CategoryCallsTest < ActiveSupport::TestCase api_call = LunchMoney::CategoryCalls.new.force_delete_category(784588) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end end diff --git a/test/lunchmoney/crypto/crypto_calls_test.rb b/test/lunchmoney/crypto/crypto_calls_test.rb index ebb3751..21aa598 100644 --- a/test/lunchmoney/crypto/crypto_calls_test.rb +++ b/test/lunchmoney/crypto/crypto_calls_test.rb @@ -25,9 +25,7 @@ class CryptoCallsTest < ActiveSupport::TestCase api_call = LunchMoney::CryptoCalls.new.crypto - api_call.each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "update_crypto returns a Crypto objects on success response" do @@ -44,8 +42,6 @@ class CryptoCallsTest < ActiveSupport::TestCase api_call = LunchMoney::CryptoCalls.new.update_crypto(7638, balance: "1.000000000000000000") - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end end diff --git a/test/lunchmoney/plaid_accounts/plaid_account_calls_test.rb b/test/lunchmoney/plaid_accounts/plaid_account_calls_test.rb index 2e14f2d..af2bea0 100644 --- a/test/lunchmoney/plaid_accounts/plaid_account_calls_test.rb +++ b/test/lunchmoney/plaid_accounts/plaid_account_calls_test.rb @@ -25,9 +25,7 @@ class PlaidAccountCallsTest < ActiveSupport::TestCase api_call = LunchMoney::PlaidAccountCalls.new.plaid_accounts - api_call.each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "plaid_accounts_fetch returns a boolean response on success" do @@ -46,8 +44,6 @@ class PlaidAccountCallsTest < ActiveSupport::TestCase api_call = LunchMoney::PlaidAccountCalls.new.plaid_accounts_fetch - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end end diff --git a/test/lunchmoney/recurring_expenses/recurring_expense_calls_test.rb b/test/lunchmoney/recurring_expenses/recurring_expense_calls_test.rb index 1237e66..3798c69 100644 --- a/test/lunchmoney/recurring_expenses/recurring_expense_calls_test.rb +++ b/test/lunchmoney/recurring_expenses/recurring_expense_calls_test.rb @@ -25,8 +25,6 @@ class RecurringExpenseCallsTest < ActiveSupport::TestCase api_call = LunchMoney::RecurringExpenseCalls.new.recurring_expenses - api_call.each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end end diff --git a/test/lunchmoney/tags/tag_calls_test.rb b/test/lunchmoney/tags/tag_calls_test.rb index aaa5245..e6f7992 100644 --- a/test/lunchmoney/tags/tag_calls_test.rb +++ b/test/lunchmoney/tags/tag_calls_test.rb @@ -25,8 +25,6 @@ class TagCallsTest < ActiveSupport::TestCase api_call = LunchMoney::TagCalls.new.tags - api_call.each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end end diff --git a/test/lunchmoney/transactions/transaction_calls_test.rb b/test/lunchmoney/transactions/transaction_calls_test.rb index 9c02744..5b77daa 100644 --- a/test/lunchmoney/transactions/transaction_calls_test.rb +++ b/test/lunchmoney/transactions/transaction_calls_test.rb @@ -25,9 +25,7 @@ class TransactionCallsTest < ActiveSupport::TestCase api_call = LunchMoney::TransactionCalls.new.transactions(start_date: "2019-01-01", end_date: "2025-01-01") - api_call.each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "transaction returns a Transaction objects on success response" do @@ -46,9 +44,7 @@ class TransactionCallsTest < ActiveSupport::TestCase api_call = LunchMoney::TransactionCalls.new.transaction(893631800) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "transaction_group returns a Transaction objects on success response" do @@ -67,9 +63,7 @@ class TransactionCallsTest < ActiveSupport::TestCase api_call = LunchMoney::TransactionCalls.new.transaction(893631800) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "insert_transactions returns a hash containing an array of ids on success response" do @@ -91,9 +85,7 @@ class TransactionCallsTest < ActiveSupport::TestCase api_call = LunchMoney::TransactionCalls.new.insert_transactions([random_update_transaction]) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "update_transaction returns a hash containing an updated boolean on success response" do @@ -142,9 +134,7 @@ class TransactionCallsTest < ActiveSupport::TestCase transaction: random_update_transaction(status: "cleared"), ) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "unsplit_transaction returns an array of unsplit transaction ids on success response" do @@ -163,9 +153,7 @@ class TransactionCallsTest < ActiveSupport::TestCase api_call = LunchMoney::TransactionCalls.new.unsplit_transaction([904778058]) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "create_transaction_group returns a transaction id of the created group on success response" do @@ -192,9 +180,7 @@ class TransactionCallsTest < ActiveSupport::TestCase api_call = LunchMoney::TransactionCalls.new.create_transaction_group(**arguments) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end test "delete_transaction_group returns an array of transaction ids from the deleted group on success response" do @@ -214,9 +200,7 @@ class TransactionCallsTest < ActiveSupport::TestCase api_call = LunchMoney::TransactionCalls.new.delete_transaction_group(905483362) - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end private diff --git a/test/lunchmoney/user/user_calls_test.rb b/test/lunchmoney/user/user_calls_test.rb index cf6b592..3bf9c22 100644 --- a/test/lunchmoney/user/user_calls_test.rb +++ b/test/lunchmoney/user/user_calls_test.rb @@ -23,8 +23,6 @@ class UserCallsTest < ActiveSupport::TestCase api_call = LunchMoney::UserCalls.new.me - T.unsafe(api_call).each do |error| - assert_kind_of(LunchMoney::Error, error) - end + assert_kind_of(LunchMoney::Errors, api_call) end end