Skip to content

Commit

Permalink
Add GET /organization/:orgId/roles support (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgd authored Dec 31, 2024
1 parent 8c401c2 commit 19e477b
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/workos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def self.key
autoload :Profile, 'workos/profile'
autoload :ProfileAndToken, 'workos/profile_and_token'
autoload :RefreshAuthenticationResponse, 'workos/refresh_authentication_response'
autoload :Role, 'workos/role'
autoload :Session, 'workos/session'
autoload :SSO, 'workos/sso'
autoload :Types, 'workos/types'
Expand Down
26 changes: 26 additions & 0 deletions lib/workos/organizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,32 @@ def delete_organization(id:)
response.is_a? Net::HTTPSuccess
end

# Retrieve a list of roles for the given organization.
#
# @param [String] organizationId The ID of the organization to fetch roles for.
def list_organization_roles(organization_id:)
response = execute_request(
request: get_request(
path: "/organizations/#{organization_id}/roles",
auth: true,
),
)

parsed_response = JSON.parse(response.body)

roles = parsed_response['data'].map do |role|
WorkOS::Role.new(role.to_json)
end

WorkOS::Types::ListStruct.new(
data: roles,
list_metadata: {
after: nil,
before: nil,
},
)
end

private

def check_and_raise_organization_error(response:)
Expand Down
36 changes: 36 additions & 0 deletions lib/workos/role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module WorkOS
# The Role class provides a lightweight wrapper around
# a WorkOS Role resource. This class is not meant to be instantiated
# in user space, and is instantiated internally but exposed.
class Role
include HashProvider

attr_accessor :id, :name, :slug, :description, :type, :created_at, :updated_at

def initialize(json)
hash = JSON.parse(json, symbolize_names: true)

@id = hash[:id]
@name = hash[:name]
@slug = hash[:slug]
@description = hash[:description]
@type = hash[:type]
@created_at = hash[:created_at]
@updated_at = hash[:updated_at]
end

def to_json(*)
{
id: id,
name: name,
slug: slug,
description: description,
type: type,
created_at: created_at,
updated_at: updated_at,
}
end
end
end
18 changes: 18 additions & 0 deletions spec/lib/workos/organizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,22 @@
end
end
end

describe '.list_organization_roles' do
context 'with no options' do
it 'returns roles for organization' do
expected_metadata = {
after: nil,
before: nil,
}

VCR.use_cassette 'organization/list_organization_roles' do
roles = described_class.list_organization_roles(organization_id: 'org_01JEXP6Z3X7HE4CB6WQSH9ZAFE')

expect(roles.data.size).to eq(7)
expect(roles.list_metadata).to eq(expected_metadata)
end
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 19e477b

Please sign in to comment.