Skip to content

Commit

Permalink
feat!: updates deprecated method signatures and fixes linter complexi…
Browse files Browse the repository at this point in the history
…ty issues
  • Loading branch information
ctran88 committed Jan 2, 2025
1 parent 2e6fff0 commit 46f7b55
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 82 deletions.
96 changes: 40 additions & 56 deletions lib/passageidentity/user_api.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:)
Expand Down Expand Up @@ -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:)
Expand Down Expand Up @@ -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
53 changes: 27 additions & 26 deletions tests/user_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]',
user_metadata: {
example1: 'cool'
}
)
args = {
'email' => '[email protected]',
'user_metadata' => {
'example1' => 'cool'
}
}
@test_user = PassageClient.user.create(args: args)
end

def test_create_delete_user
user =
PassageClient.user.create(
email: '[email protected]',
user_metadata: {
example1: 'cool'
}
)
args = {
'email' => 'passage+test-create[email protected]',
'user_metadata' => {
'example1' => 'cool'
}
}
user = PassageClient.user.create(args: args)
assert_equal '[email protected]', user.email
assert_equal 'cool', user.user_metadata[:example1]

deleted = PassageClient.user.delete(user_id: user.id)
assert_equal true, deleted
end
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -97,14 +99,13 @@ def test_activate_user

def test_update_user
new_email = '[email protected]'
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]
Expand Down

0 comments on commit 46f7b55

Please sign in to comment.