OpenFeature Provider for Ruby
Getting started
1. Install the provider
gem install configcat-openfeature-provider
2. Initialize the provider
The initializer of ConfigCat::OpenFeature::Provider
takes the SDK key and an optional ConfigCat::ConfigCatOptions
argument containing the additional configuration options for the ConfigCat Ruby SDK:
require "configcat-openfeature-provider"
# Configure the OpenFeature API with the ConfigCat provider.
OpenFeature::SDK.configure do |config|
config.set_provider(ConfigCat::OpenFeature::Provider.new(
sdk_key: "<YOUR-CONFIGCAT-SDK-KEY>",
# Build options for the ConfigCat SDK.
options: ConfigCat::ConfigCatOptions.new(
polling_mode: ConfigCat::PollingMode.auto_poll
)))
end
# Create a client.
client = OpenFeature::SDK.build_client
For more information about all the configuration options, see the Ruby SDK documentation.
3. Evaluate your feature flag
is_awesome_feature_enabled = client.fetch_boolean_value(
flag_key: "isAwesomeFeatureEnabled",
default_value: false
)
if is_awesome_feature_enabled
do_the_new_thing
else
do_the_old_thing
end
Evaluation Context
An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.
The following table shows how the different context attributes are mapped to User Object attributes.
Evaluation context | User Object | Required |
---|---|---|
targeting_key | identifier | ☑ |
Email | email | |
Country | country | |
Any other | custom |
To evaluate feature flags for a context, use the OpenFeature Evaluation API:
context = OpenFeature::SDK::EvaluationContext.new(
OpenFeature::SDK::EvaluationContext::TARGETING_KEY => "#SOME-USER-ID#",
"Email" => "[email protected]",
"Country" => "CountryID",
"Rating" => 4.5,
"RegisteredAt" => DateTime.iso8601("2023-11-22T12:34:56+00:00"),
"Roles" => [ "Role1", "Role2" ]
)
is_awesome_feature_enabled = client.fetch_boolean_value(
flag_key: "isAwesomeFeatureEnabled",
default_value: false,
evaluation_context: context
)