Skip to content

Commit 19e477b

Browse files
authored
Add GET /organization/:orgId/roles support (#338)
1 parent 8c401c2 commit 19e477b

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

lib/workos.rb

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def self.key
7171
autoload :Profile, 'workos/profile'
7272
autoload :ProfileAndToken, 'workos/profile_and_token'
7373
autoload :RefreshAuthenticationResponse, 'workos/refresh_authentication_response'
74+
autoload :Role, 'workos/role'
7475
autoload :Session, 'workos/session'
7576
autoload :SSO, 'workos/sso'
7677
autoload :Types, 'workos/types'

lib/workos/organizations.rb

+26
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,32 @@ def delete_organization(id:)
180180
response.is_a? Net::HTTPSuccess
181181
end
182182

183+
# Retrieve a list of roles for the given organization.
184+
#
185+
# @param [String] organizationId The ID of the organization to fetch roles for.
186+
def list_organization_roles(organization_id:)
187+
response = execute_request(
188+
request: get_request(
189+
path: "/organizations/#{organization_id}/roles",
190+
auth: true,
191+
),
192+
)
193+
194+
parsed_response = JSON.parse(response.body)
195+
196+
roles = parsed_response['data'].map do |role|
197+
WorkOS::Role.new(role.to_json)
198+
end
199+
200+
WorkOS::Types::ListStruct.new(
201+
data: roles,
202+
list_metadata: {
203+
after: nil,
204+
before: nil,
205+
},
206+
)
207+
end
208+
183209
private
184210

185211
def check_and_raise_organization_error(response:)

lib/workos/role.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
module WorkOS
4+
# The Role class provides a lightweight wrapper around
5+
# a WorkOS Role resource. This class is not meant to be instantiated
6+
# in user space, and is instantiated internally but exposed.
7+
class Role
8+
include HashProvider
9+
10+
attr_accessor :id, :name, :slug, :description, :type, :created_at, :updated_at
11+
12+
def initialize(json)
13+
hash = JSON.parse(json, symbolize_names: true)
14+
15+
@id = hash[:id]
16+
@name = hash[:name]
17+
@slug = hash[:slug]
18+
@description = hash[:description]
19+
@type = hash[:type]
20+
@created_at = hash[:created_at]
21+
@updated_at = hash[:updated_at]
22+
end
23+
24+
def to_json(*)
25+
{
26+
id: id,
27+
name: name,
28+
slug: slug,
29+
description: description,
30+
type: type,
31+
created_at: created_at,
32+
updated_at: updated_at,
33+
}
34+
end
35+
end
36+
end

spec/lib/workos/organizations_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,22 @@
323323
end
324324
end
325325
end
326+
327+
describe '.list_organization_roles' do
328+
context 'with no options' do
329+
it 'returns roles for organization' do
330+
expected_metadata = {
331+
after: nil,
332+
before: nil,
333+
}
334+
335+
VCR.use_cassette 'organization/list_organization_roles' do
336+
roles = described_class.list_organization_roles(organization_id: 'org_01JEXP6Z3X7HE4CB6WQSH9ZAFE')
337+
338+
expect(roles.data.size).to eq(7)
339+
expect(roles.list_metadata).to eq(expected_metadata)
340+
end
341+
end
342+
end
343+
end
326344
end

spec/support/fixtures/vcr_cassettes/organization/list_organization_roles.yml

+82
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)