Skip to content

Commit

Permalink
Merge pull request #132 from halorrr/add-plaid-accounts-fetch
Browse files Browse the repository at this point in the history
Add plaid accounts fetch
  • Loading branch information
mmenanno authored Jan 26, 2024
2 parents b56b5a3 + f16e957 commit e4dbb2d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/lunchmoney/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def asset_calls
end
end

delegate :plaid_accounts, to: :plaid_account_calls
delegate :plaid_accounts, :plaid_accounts_fetch, to: :plaid_account_calls

sig { returns(LunchMoney::ApiCall) }
def plaid_account_calls
Expand Down
17 changes: 15 additions & 2 deletions lib/lunchmoney/api_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ def request(json_request: false, flat_params: false)
def errors(response)
body = response.body

return parse_errors(body) if body.is_a?(Hash) && body[:error]
return parse_errors(body) unless error_hash(body).nil?

[]
end

sig { params(body: T::Hash[Symbol, T.any(String, T::Array[String])]).returns(LunchMoney::Errors) }
def parse_errors(body)
errors = body[:error] || body[:errors]
errors = error_hash(body)
return [] if errors.blank?

api_errors = []
Expand All @@ -84,5 +84,18 @@ def parse_errors(body)

api_errors
end

sig { params(body: T.untyped).returns(T.untyped) }
def error_hash(body)
return unless body.is_a?(Hash)

if body[:error]
body[:error]
elsif body[:errors]
body[:errors]
elsif body[:name] == "Error"
body[:message]
end
end
end
end
23 changes: 23 additions & 0 deletions lib/lunchmoney/plaid_accounts/plaid_account_calls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,28 @@ def plaid_accounts
LunchMoney::PlaidAccount.new(**plaid_account)
end
end

sig do
params(
start_date: T.nilable(String),
end_date: T.nilable(String),
plaid_account_id: T.nilable(Integer),
).returns(T.any(T::Boolean, LunchMoney::Errors))
end
def plaid_accounts_fetch(start_date: nil, end_date: nil, plaid_account_id: nil)
params = {
start_date:,
end_date:,
plaid_account_id:,
}
params.reject! { |_key, value| value.nil? }

response = post("plaid_accounts/fetch", params)
# binding.pry
api_errors = errors(response)
return api_errors if api_errors.present?

response.body
end
end
end
20 changes: 20 additions & 0 deletions test/lunchmoney/plaid_accounts/plaid_account_calls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,24 @@ class PlaidAccountCallsTest < ActiveSupport::TestCase

assert_kind_of(LunchMoney::Error, api_call.first)
end

test "plaid_accounts_fetch returns a boolean response on success" do
response = mock_faraday_response(true)

LunchMoney::PlaidAccountCalls.any_instance.stubs(:post).returns(response)

api_call = LunchMoney::PlaidAccountCalls.new.plaid_accounts_fetch

assert_kind_of(TrueClass, api_call)
end

test "plaid_accounts_fetch returns an array of Error objects on error response" do
response = mock_faraday_response(fake_general_error)

LunchMoney::PlaidAccountCalls.any_instance.stubs(:post).returns(response)

api_call = LunchMoney::PlaidAccountCalls.new.plaid_accounts_fetch

assert_kind_of(LunchMoney::Error, T.unsafe(api_call).first)
end
end

0 comments on commit e4dbb2d

Please sign in to comment.