From d031054c095d270016a6fa6b891849ad428d9a56 Mon Sep 17 00:00:00 2001 From: Chris Tran Date: Mon, 23 Dec 2024 16:36:08 -0600 Subject: [PATCH 1/2] feat!: updates deprecated method signatures and fixes linter complexity issues --- lib/passageidentity/user_api.rb | 96 ++++++++++++++------------------- tests/user_api_test.rb | 53 +++++++++--------- 2 files changed, 67 insertions(+), 82 deletions(-) diff --git a/lib/passageidentity/user_api.rb b/lib/passageidentity/user_api.rb index 06050a4..72d4ef7 100644 --- a/lib/passageidentity/user_api.rb +++ b/lib/passageidentity/user_api.rb @@ -1,11 +1,10 @@ # frozen_string_literal: true -require_relative 'client' +require_relative '../openapi_client' module Passage # The UserAPI class provides methods for interacting with Passage Users class UserAPI - # rubocop:disable Metrics/AbcSize def initialize(app_id:, req_opts:) @app_id = app_id @req_opts = req_opts @@ -33,27 +32,16 @@ def get_by_identifier(user_identifier:) raise ArgumentError, 'identifier is required.' unless user_identifier && !user_identifier.empty? begin - @req_opts[:limit] = 1 - @req_opts[:identifier] = user_identifier.downcase - response = @user_client.list_paginated_users(@app_id, @req_opts) - users = response.users - - if users.empty? - raise PassageError.new( - status_code: 404, - body: { - error: 'User not found.', - code: 'user_not_found' - } - ) - end - get(user_id: users.first.id) + req_opts = set_get_by_identifier_query_params(identifier: user_identifier) + response = @user_client.list_paginated_users(@app_id, req_opts) rescue Faraday::Error => e raise PassageError.new( status_code: e.response[:status], body: e.response[:body] ) end + + handle_get_by_identifier(users: response.users) end def activate(user_id:) @@ -84,28 +72,29 @@ def deactivate(user_id:) end end - def update(user_id:, email: '', phone: '', user_metadata: {}) - warn '[DEPRECATED] the `update` method parameters will change to `user_id: string, ' \ - 'options: UpdateUserArgs`. Parameters will change on or after 2025-1.' - - updates = {} - updates['email'] = email unless email.empty? - updates['phone'] = phone unless phone.empty? - updates['user_metadata'] = user_metadata unless user_metadata.empty? + def update(user_id:, options:) + raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty? + raise ArgumentError, 'options are required.' unless options && !options.empty? - update_v2(user_id: user_id, options: updates) + response = @user_client.update_user(@app_id, user_id, options, @req_opts) + response.user + rescue Faraday::Error => e + raise PassageError.new( + status_code: e.response[:status], + body: e.response[:body] + ) end - def create(email: '', phone: '', user_metadata: {}) - warn '[DEPRECATED] the `create` method parameters will change to `args: CreateUserArgs`.' \ - 'Parameters will change on or after 2025-1.' - - create = {} - create['email'] = email unless email.empty? - create['phone'] = phone unless phone.empty? - create['user_metadata'] = user_metadata unless user_metadata.empty? + def create(args:) + raise ArgumentError, 'At least one of args.email or args.phone is required.' unless args['phone'] || args['email'] - create_v2(args: create) + response = @user_client.create_user(@app_id, args, @req_opts) + response.user + rescue Faraday::Error => e + raise PassageError.new( + status_code: e.response[:status], + body: e.response[:body] + ) end def delete(user_id:) @@ -166,30 +155,25 @@ def revoke_refresh_tokens(user_id:) private - def create_v2(args: {}) - raise ArgumentError, 'At least one of args.email or args.phone is required.' unless args['phone'] || args['email'] - - response = @user_client.create_user(@app_id, args, @req_opts) - response.user - rescue Faraday::Error => e - raise PassageError.new( - status_code: e.response[:status], - body: e.response[:body] - ) + def set_get_by_identifier_query_params(identifier:) + req_opts = @req_opts.dup + req_opts[:limit] = 1 + req_opts[:identifier] = identifier.downcase + req_opts end - def update_v2(user_id:, options: {}) - raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty? - raise ArgumentError, 'options are required.' if options.empty? + def handle_get_by_identifier(users:) + if users.empty? + raise PassageError.new( + status_code: 404, + body: { + error: 'User not found.', + code: 'user_not_found' + } + ) + end - response = @user_client.update_user(@app_id, user_id, options, @req_opts) - response.user - rescue Faraday::Error => e - raise PassageError.new( - status_code: e.response[:status], - body: e.response[:body] - ) + get(user_id: users.first.id) end - # rubocop:enable Metrics/AbcSize end end diff --git a/tests/user_api_test.rb b/tests/user_api_test.rb index c3010d5..b98cd6d 100644 --- a/tests/user_api_test.rb +++ b/tests/user_api_test.rb @@ -12,25 +12,26 @@ class TestUserAPI < Test::Unit::TestCase PassageClient = Passage::Client.new(app_id: ENV['APP_ID'], api_key: ENV['API_KEY']) def setup - @test_user = - PassageClient.user.create( - email: 'passage+test-ruby@passage.id', - user_metadata: { - example1: 'cool' - } - ) + args = { + 'email' => 'passage+test-ruby@passage.id', + 'user_metadata' => { + 'example1' => 'cool' + } + } + @test_user = PassageClient.user.create(args: args) end def test_create_delete_user - user = - PassageClient.user.create( - email: 'passage+test-create-delete@passage.id', - user_metadata: { - example1: 'cool' - } - ) + args = { + 'email' => 'passage+test-create-delete@passage.id', + 'user_metadata' => { + 'example1' => 'cool' + } + } + user = PassageClient.user.create(args: args) assert_equal 'passage+test-create-delete@passage.id', user.email assert_equal 'cool', user.user_metadata[:example1] + deleted = PassageClient.user.delete(user_id: user.id) assert_equal true, deleted end @@ -62,9 +63,10 @@ def test_get_user_by_identifier_upper_case def test_get_user_by_identifier_phone phone = '+15005550007' - create_user = PassageClient.user.create( - phone: phone - ) + args = { + 'phone' => phone + } + create_user = PassageClient.user.create(args: args) user = PassageClient.user.get(user_id: create_user.id) assert_equal create_user.id, user.id @@ -74,7 +76,7 @@ def test_get_user_by_identifier_phone assert_equal user, user_by_identifier end - def test_invalid_get_user_by_identifier + def test_invalid_get_by_identifier user = PassageClient.user.get(user_id: @test_user.id) assert_equal @test_user.id, user.id @@ -97,14 +99,13 @@ def test_activate_user def test_update_user new_email = 'passage+update_test-ruby@passage.id' - user = - PassageClient.user.update( - user_id: @test_user.id, - email: new_email, - user_metadata: { - example1: 'lame' - } - ) + opts = { + 'email' => new_email, + 'user_metadata' => { + 'example1' => 'lame' + } + } + user = PassageClient.user.update(user_id: @test_user.id, options: opts) assert_equal @test_user.id, user.id assert_equal new_email, user.email assert_equal 'lame', user.user_metadata[:example1] From 7988bec03d7c1fd5311b03247ef2336ab0166167 Mon Sep 17 00:00:00 2001 From: Chris Tran Date: Thu, 2 Jan 2025 14:53:52 -0600 Subject: [PATCH 2/2] feat!: renames User class and other function argument names (#154) --- lib/passageidentity/client.rb | 4 ++-- lib/passageidentity/{user_api.rb => user.rb} | 10 +++++----- tests/{user_api_test.rb => user_test.rb} | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) rename lib/passageidentity/{user_api.rb => user.rb} (95%) rename tests/{user_api_test.rb => user_test.rb} (86%) diff --git a/lib/passageidentity/client.rb b/lib/passageidentity/client.rb index 58ee817..786fec4 100644 --- a/lib/passageidentity/client.rb +++ b/lib/passageidentity/client.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require_relative 'auth' -require_relative 'user_api' +require_relative 'user' require_relative 'version' module Passage @@ -30,7 +30,7 @@ def initialize(app_id:, api_key:) req_opts[:debug_auth_names] = ['header'] @auth = Passage::Auth.new(app_id: app_id, req_opts: req_opts) - @user = Passage::UserAPI.new(app_id: app_id, req_opts: req_opts) + @user = Passage::User.new(app_id: app_id, req_opts: req_opts) end end end diff --git a/lib/passageidentity/user_api.rb b/lib/passageidentity/user.rb similarity index 95% rename from lib/passageidentity/user_api.rb rename to lib/passageidentity/user.rb index 72d4ef7..8ac8c1a 100644 --- a/lib/passageidentity/user_api.rb +++ b/lib/passageidentity/user.rb @@ -3,8 +3,8 @@ require_relative '../openapi_client' module Passage - # The UserAPI class provides methods for interacting with Passage Users - class UserAPI + # The User class provides methods for interacting with Passage Users + class User def initialize(app_id:, req_opts:) @app_id = app_id @req_opts = req_opts @@ -28,11 +28,11 @@ def get(user_id:) end end - def get_by_identifier(user_identifier:) - raise ArgumentError, 'identifier is required.' unless user_identifier && !user_identifier.empty? + def get_by_identifier(identifier:) + raise ArgumentError, 'identifier is required.' unless identifier && !identifier.empty? begin - req_opts = set_get_by_identifier_query_params(identifier: user_identifier) + req_opts = set_get_by_identifier_query_params(identifier: identifier) response = @user_client.list_paginated_users(@app_id, req_opts) rescue Faraday::Error => e raise PassageError.new( diff --git a/tests/user_api_test.rb b/tests/user_test.rb similarity index 86% rename from tests/user_api_test.rb rename to tests/user_test.rb index b98cd6d..a5ca6ee 100644 --- a/tests/user_api_test.rb +++ b/tests/user_test.rb @@ -7,8 +7,8 @@ Dotenv.load('.env') -# This is a test suite for the Passage User API using the Test::Unit framework. -class TestUserAPI < Test::Unit::TestCase +# This is a test suite for the Passage User using the Test::Unit framework. +class TestUser < Test::Unit::TestCase PassageClient = Passage::Client.new(app_id: ENV['APP_ID'], api_key: ENV['API_KEY']) def setup @@ -45,7 +45,7 @@ def test_get_user_by_identifier user = PassageClient.user.get(user_id: @test_user.id) assert_equal @test_user.id, user.id - user_by_identifier = PassageClient.user.get_by_identifier(user_identifier: @test_user.email) + user_by_identifier = PassageClient.user.get_by_identifier(identifier: @test_user.email) assert_equal @test_user.id, user_by_identifier.id assert_equal user, user_by_identifier @@ -55,7 +55,7 @@ def test_get_user_by_identifier_upper_case user = PassageClient.user.get(user_id: @test_user.id) assert_equal @test_user.id, user.id - user_by_identifier = PassageClient.user.get_by_identifier(user_identifier: @test_user.email.upcase) + user_by_identifier = PassageClient.user.get_by_identifier(identifier: @test_user.email.upcase) assert_equal @test_user.id, user_by_identifier.id assert_equal user, user_by_identifier @@ -70,7 +70,7 @@ def test_get_user_by_identifier_phone user = PassageClient.user.get(user_id: create_user.id) assert_equal create_user.id, user.id - user_by_identifier = PassageClient.user.get_by_identifier(user_identifier: phone) + user_by_identifier = PassageClient.user.get_by_identifier(identifier: phone) assert_equal create_user.id, user_by_identifier.id assert_equal user, user_by_identifier @@ -81,7 +81,7 @@ def test_invalid_get_by_identifier assert_equal @test_user.id, user.id assert_raise Passage::PassageError do - PassageClient.user.get_by_identifier(user_identifier: 'error@passage.id') + PassageClient.user.get_by_identifier(identifier: 'error@passage.id') end end