Skip to content

Commit fc271a9

Browse files
authored
Add GET /organizations/:orgId/feature-flags support (#391)
1 parent 4d8a2ba commit fc271a9

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-0
lines changed

lib/workos.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def self.key
5959
autoload :Event, 'workos/event'
6060
autoload :Events, 'workos/events'
6161
autoload :Factor, 'workos/factor'
62+
autoload :FeatureFlag, 'workos/feature_flag'
6263
autoload :Impersonator, 'workos/impersonator'
6364
autoload :Invitation, 'workos/invitation'
6465
autoload :MagicAuth, 'workos/magic_auth'

lib/workos/feature_flag.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module WorkOS
4+
# The FeatureFlag class provides a lightweight wrapper around
5+
# a WorkOS Feature Flag resource. This class is not meant to be instantiated
6+
# in user space, and is instantiated internally but exposed.
7+
class FeatureFlag
8+
include HashProvider
9+
10+
attr_accessor :id, :name, :slug, :description, :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+
@created_at = hash[:created_at]
20+
@updated_at = hash[:updated_at]
21+
end
22+
23+
def to_json(*)
24+
{
25+
id: id,
26+
name: name,
27+
slug: slug,
28+
description: description,
29+
created_at: created_at,
30+
updated_at: updated_at,
31+
}
32+
end
33+
end
34+
end

lib/workos/organizations.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,46 @@ def list_organization_roles(organization_id:)
224224
)
225225
end
226226

227+
# Retrieve a list of feature flags for the given organization.
228+
#
229+
# @param [String] organization_id The ID of the organization to fetch feature flags for.
230+
# @param [Hash] options
231+
# @option options [String] before A pagination argument used to request
232+
# feature flags before the provided FeatureFlag ID.
233+
# @option options [String] after A pagination argument used to request
234+
# feature flags after the provided FeatureFlag ID.
235+
# @option options [Integer] limit A pagination argument used to limit the number
236+
# of listed FeatureFlags that are returned.
237+
# @option options [String] order The order in which to paginate records
238+
#
239+
# @example
240+
# WorkOS::Organizations.list_organization_feature_flags(organization_id: 'org_01EHZNVPK3SFK441A1RGBFSHRT')
241+
# => #<WorkOS::Types::ListStruct data=[#<WorkOS::FeatureFlag id="flag_123" slug="new-feature"
242+
# enabled=true ...>] ...>
243+
#
244+
# @return [WorkOS::Types::ListStruct] - Collection of FeatureFlag objects
245+
def list_organization_feature_flags(organization_id:, options: {})
246+
options[:order] ||= 'desc'
247+
response = execute_request(
248+
request: get_request(
249+
path: "/organizations/#{organization_id}/feature-flags",
250+
auth: true,
251+
params: options,
252+
),
253+
)
254+
255+
parsed_response = JSON.parse(response.body)
256+
257+
feature_flags = parsed_response['data'].map do |feature_flag|
258+
WorkOS::FeatureFlag.new(feature_flag.to_json)
259+
end
260+
261+
WorkOS::Types::ListStruct.new(
262+
data: feature_flags,
263+
list_metadata: parsed_response['list_metadata']&.transform_keys(&:to_sym),
264+
)
265+
end
266+
227267
private
228268

229269
def check_and_raise_organization_error(response:)

spec/lib/workos/organizations_spec.rb

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,120 @@
450450
end
451451
end
452452
end
453+
454+
describe '.list_organization_feature_flags' do
455+
context 'with no options' do
456+
it 'returns feature flags for organization' do
457+
expected_metadata = {
458+
after: nil,
459+
before: nil,
460+
}
461+
462+
VCR.use_cassette 'organization/list_organization_feature_flags' do
463+
feature_flags = described_class.list_organization_feature_flags(
464+
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
465+
)
466+
467+
expect(feature_flags.data.size).to eq(2)
468+
expect(feature_flags.list_metadata).to eq(expected_metadata)
469+
end
470+
end
471+
end
472+
473+
context 'with the before option' do
474+
it 'forms the proper request to the API' do
475+
request_args = [
476+
'/organizations/org_01HX7Q7R12H1JMAKN75SH2G529/feature-flags?before=before-id&'\
477+
'order=desc',
478+
'Content-Type' => 'application/json'
479+
]
480+
481+
expected_request = Net::HTTP::Get.new(*request_args)
482+
483+
expect(Net::HTTP::Get).to receive(:new).with(*request_args).
484+
and_return(expected_request)
485+
486+
VCR.use_cassette 'organization/list_organization_feature_flags', match_requests_on: [:path] do
487+
feature_flags = described_class.list_organization_feature_flags(
488+
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
489+
options: { before: 'before-id' },
490+
)
491+
492+
expect(feature_flags.data.size).to eq(2)
493+
end
494+
end
495+
end
496+
497+
context 'with the after option' do
498+
it 'forms the proper request to the API' do
499+
request_args = [
500+
'/organizations/org_01HX7Q7R12H1JMAKN75SH2G529/feature-flags?after=after-id&'\
501+
'order=desc',
502+
'Content-Type' => 'application/json'
503+
]
504+
505+
expected_request = Net::HTTP::Get.new(*request_args)
506+
507+
expect(Net::HTTP::Get).to receive(:new).with(*request_args).
508+
and_return(expected_request)
509+
510+
VCR.use_cassette 'organization/list_organization_feature_flags', match_requests_on: [:path] do
511+
feature_flags = described_class.list_organization_feature_flags(
512+
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
513+
options: { after: 'after-id' },
514+
)
515+
516+
expect(feature_flags.data.size).to eq(2)
517+
end
518+
end
519+
end
520+
521+
context 'with the limit option' do
522+
it 'forms the proper request to the API' do
523+
request_args = [
524+
'/organizations/org_01HX7Q7R12H1JMAKN75SH2G529/feature-flags?limit=10&'\
525+
'order=desc',
526+
'Content-Type' => 'application/json'
527+
]
528+
529+
expected_request = Net::HTTP::Get.new(*request_args)
530+
531+
expect(Net::HTTP::Get).to receive(:new).with(*request_args).
532+
and_return(expected_request)
533+
534+
VCR.use_cassette 'organization/list_organization_feature_flags', match_requests_on: [:path] do
535+
feature_flags = described_class.list_organization_feature_flags(
536+
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
537+
options: { limit: 10 },
538+
)
539+
540+
expect(feature_flags.data.size).to eq(2)
541+
end
542+
end
543+
end
544+
545+
context 'with multiple pagination options' do
546+
it 'forms the proper request to the API' do
547+
request_args = [
548+
'/organizations/org_01HX7Q7R12H1JMAKN75SH2G529/feature-flags?after=after-id&'\
549+
'limit=5&order=asc',
550+
'Content-Type' => 'application/json'
551+
]
552+
553+
expected_request = Net::HTTP::Get.new(*request_args)
554+
555+
expect(Net::HTTP::Get).to receive(:new).with(*request_args).
556+
and_return(expected_request)
557+
558+
VCR.use_cassette 'organization/list_organization_feature_flags', match_requests_on: [:path] do
559+
feature_flags = described_class.list_organization_feature_flags(
560+
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
561+
options: { after: 'after-id', limit: 5, order: 'asc' },
562+
)
563+
564+
expect(feature_flags.data.size).to eq(2)
565+
end
566+
end
567+
end
568+
end
453569
end

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

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)