Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/workos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def self.key
autoload :Event, 'workos/event'
autoload :Events, 'workos/events'
autoload :Factor, 'workos/factor'
autoload :FeatureFlag, 'workos/feature_flag'
autoload :Impersonator, 'workos/impersonator'
autoload :Invitation, 'workos/invitation'
autoload :MagicAuth, 'workos/magic_auth'
Expand Down
34 changes: 34 additions & 0 deletions lib/workos/feature_flag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

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

attr_accessor :id, :name, :slug, :description, :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]
@created_at = hash[:created_at]
@updated_at = hash[:updated_at]
end

def to_json(*)
{
id: id,
name: name,
slug: slug,
description: description,
created_at: created_at,
updated_at: updated_at,
}
end
end
end
40 changes: 40 additions & 0 deletions lib/workos/organizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,46 @@ def list_organization_roles(organization_id:)
)
end

# Retrieve a list of feature flags for the given organization.
#
# @param [String] organization_id The ID of the organization to fetch feature flags for.
# @param [Hash] options
# @option options [String] before A pagination argument used to request
# feature flags before the provided FeatureFlag ID.
# @option options [String] after A pagination argument used to request
# feature flags after the provided FeatureFlag ID.
# @option options [Integer] limit A pagination argument used to limit the number
# of listed FeatureFlags that are returned.
# @option options [String] order The order in which to paginate records
#
# @example
# WorkOS::Organizations.list_organization_feature_flags(organization_id: 'org_01EHZNVPK3SFK441A1RGBFSHRT')
# => #<WorkOS::Types::ListStruct data=[#<WorkOS::FeatureFlag id="flag_123" slug="new-feature"
# enabled=true ...>] ...>
#
# @return [WorkOS::Types::ListStruct] - Collection of FeatureFlag objects
def list_organization_feature_flags(organization_id:, options: {})
options[:order] ||= 'desc'
response = execute_request(
request: get_request(
path: "/organizations/#{organization_id}/feature-flags",
auth: true,
params: options,
),
)

parsed_response = JSON.parse(response.body)

feature_flags = parsed_response['data'].map do |feature_flag|
WorkOS::FeatureFlag.new(feature_flag.to_json)
end

WorkOS::Types::ListStruct.new(
data: feature_flags,
list_metadata: parsed_response['list_metadata']&.transform_keys(&:to_sym),
)
end

private

def check_and_raise_organization_error(response:)
Expand Down
116 changes: 116 additions & 0 deletions spec/lib/workos/organizations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,120 @@
end
end
end

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

VCR.use_cassette 'organization/list_organization_feature_flags' do
feature_flags = described_class.list_organization_feature_flags(
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
)

expect(feature_flags.data.size).to eq(2)
expect(feature_flags.list_metadata).to eq(expected_metadata)
end
end
end

context 'with the before option' do
it 'forms the proper request to the API' do
request_args = [
'/organizations/org_01HX7Q7R12H1JMAKN75SH2G529/feature-flags?before=before-id&'\
'order=desc',
'Content-Type' => 'application/json'
]

expected_request = Net::HTTP::Get.new(*request_args)

expect(Net::HTTP::Get).to receive(:new).with(*request_args).
and_return(expected_request)

VCR.use_cassette 'organization/list_organization_feature_flags', match_requests_on: [:path] do
feature_flags = described_class.list_organization_feature_flags(
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
options: { before: 'before-id' },
)

expect(feature_flags.data.size).to eq(2)
end
end
end

context 'with the after option' do
it 'forms the proper request to the API' do
request_args = [
'/organizations/org_01HX7Q7R12H1JMAKN75SH2G529/feature-flags?after=after-id&'\
'order=desc',
'Content-Type' => 'application/json'
]

expected_request = Net::HTTP::Get.new(*request_args)

expect(Net::HTTP::Get).to receive(:new).with(*request_args).
and_return(expected_request)

VCR.use_cassette 'organization/list_organization_feature_flags', match_requests_on: [:path] do
feature_flags = described_class.list_organization_feature_flags(
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
options: { after: 'after-id' },
)

expect(feature_flags.data.size).to eq(2)
end
end
end

context 'with the limit option' do
it 'forms the proper request to the API' do
request_args = [
'/organizations/org_01HX7Q7R12H1JMAKN75SH2G529/feature-flags?limit=10&'\
'order=desc',
'Content-Type' => 'application/json'
]

expected_request = Net::HTTP::Get.new(*request_args)

expect(Net::HTTP::Get).to receive(:new).with(*request_args).
and_return(expected_request)

VCR.use_cassette 'organization/list_organization_feature_flags', match_requests_on: [:path] do
feature_flags = described_class.list_organization_feature_flags(
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
options: { limit: 10 },
)

expect(feature_flags.data.size).to eq(2)
end
end
end

context 'with multiple pagination options' do
it 'forms the proper request to the API' do
request_args = [
'/organizations/org_01HX7Q7R12H1JMAKN75SH2G529/feature-flags?after=after-id&'\
'limit=5&order=asc',
'Content-Type' => 'application/json'
]

expected_request = Net::HTTP::Get.new(*request_args)

expect(Net::HTTP::Get).to receive(:new).with(*request_args).
and_return(expected_request)

VCR.use_cassette 'organization/list_organization_feature_flags', match_requests_on: [:path] do
feature_flags = described_class.list_organization_feature_flags(
organization_id: 'org_01HX7Q7R12H1JMAKN75SH2G529',
options: { after: 'after-id', limit: 5, order: 'asc' },
)

expect(feature_flags.data.size).to eq(2)
end
end
end
end
end

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