Home

Awesome

<!-- markdownlint-disable MD033 --> <!-- x-hide-in-docs-start --> <p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/open-feature/community/0e23508c163a6a1ac8c0ced3e4bd78faafe627c7/assets/logo/horizontal/white/openfeature-horizontal-white.svg" /> <img align="center" alt="OpenFeature Logo" src="https://raw.githubusercontent.com/open-feature/community/0e23508c163a6a1ac8c0ced3e4bd78faafe627c7/assets/logo/horizontal/black/openfeature-horizontal-black.svg" /> </picture> </p> <h2 align="center">OpenFeature Ruby SDK</h2> <!-- x-hide-in-docs-end --> <!-- The 'github-badges' class is used in the docs --> <p align="center" class="github-badges"> <a href="https://github.com/open-feature/spec/releases/tag/v0.8.0"> <img alt="Specification" src="https://img.shields.io/static/v1?label=specification&message=v0.8.0&color=yellow&style=for-the-badge" /> </a> <!-- x-release-please-start-version --> <a href="https://github.com/open-feature/ruby-sdk/releases/tag/v0.4.0"> <img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v0.4.0&color=blue&style=for-the-badge" /> </a> <!-- x-release-please-end --> <br/> <a href="https://bestpractices.coreinfrastructure.org/projects/9337"> <img alt="CII Best Practices" src="https://bestpractices.coreinfrastructure.org/projects/9337/badge" /> </a> </p> <!-- x-hide-in-docs-start -->

OpenFeature is an open specification that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool or in-house solution.

<!-- x-hide-in-docs-end -->

🚀 Quick start

Requirements

Supported Ruby VersionOS
Ruby 3.1.4Windows, MacOS, Linux
Ruby 3.2.3Windows, MacOS, Linux
Ruby 3.3.0Windows, MacOS, Linux

Install

Install the gem and add to the application's Gemfile by executing:

bundle add openfeature-sdk

If bundler is not being used to manage dependencies, install the gem by executing:

gem install openfeature-sdk

Usage

require 'open_feature/sdk'
require 'json' # For JSON.dump

# API Initialization and configuration

OpenFeature::SDK.configure do |config|
  # your provider of choice, which will be used as the default provider
  config.set_provider(OpenFeature::SDK::Provider::InMemoryProvider.new(
    {
      "flag1" => true,
      "flag2" => 1
    }
  ))
end

# Create a client
client = OpenFeature::SDK.build_client

# fetching boolean value feature flag
bool_value = client.fetch_boolean_value(flag_key: 'boolean_flag', default_value: false)

# a details method is also available for more information about the flag evaluation
# see `ResolutionDetails` for more info
bool_details = client.fetch_boolean_details(flag_key: 'boolean_flag', default_value: false) ==

# fetching string value feature flag
string_value = client.fetch_string_value(flag_key: 'string_flag', default_value: false)

# fetching number value feature flag
float_value = client.fetch_number_value(flag_key: 'number_value', default_value: 1.0)
integer_value = client.fetch_number_value(flag_key: 'number_value', default_value: 1)

# get an object value
object = client.fetch_object_value(flag_key: 'object_value', default_value: JSON.dump({ name: 'object'}))

🌟 Features

StatusFeaturesDescription
ProvidersIntegrate with a commercial, open source, or in-house feature management tool.
TargetingContextually-aware flag evaluation using evaluation context.
⚠️HooksAdd functionality to various stages of the flag evaluation life-cycle.
LoggingIntegrate with popular logging packages.
DomainsLogically bind clients with providers.
EventingReact to state changes in the provider or flag management system.
⚠️ShutdownGracefully clean up a provider during application shutdown.
Transaction Context PropagationSet a specific evaluation context for a transaction (e.g. an HTTP request or a thread)
⚠️ExtendingExtend OpenFeature with custom providers and hooks.

<sub>Implemented: ✅ | In-progress: ⚠️ | Not implemented yet: ❌</sub>

Providers

Providers are an abstraction between a flag management system and the OpenFeature SDK. Look here for a complete list of available providers. If the provider you're looking for hasn't been created yet, see the develop a provider section to learn how to build it yourself.

Once you've added a provider as a dependency, it can be registered with OpenFeature like this:

OpenFeature::SDK.configure do |config|
  # your provider of choice, which will be used as the default provider
  config.set_provider(OpenFeature::SDK::Provider::InMemoryProvider.new(
    {
      "v2_enabled" => true,
    }
  ))
end

In some situations, it may be beneficial to register multiple providers in the same application. This is possible using domains, which is covered in more detail below.

Targeting

Sometimes, the value of a flag must consider some dynamic criteria about the application or user, such as the user's location, IP, email address, or the server's location. In OpenFeature, we refer to this as targeting. If the flag management system you're using supports targeting, you can provide the input data using the evaluation context.

OpenFeature::SDK.configure do |config|
  # you can set a global evaluation context here
  config.evaluation_context = OpenFeature::SDK::EvaluationContext.new("host" => "myhost.com")
end

# Evaluation context can be set on a client as well
client_with_context = OpenFeature::SDK.build_client(
  evaluation_context: OpenFeature::SDK::EvaluationContext.new("controller_name" => "admin")
)

# Invocation evaluation context can also be passed in during flag evaluation.
# During flag evaluation, invocation context takes precedence over client context
# which takes precedence over API (aka global) context.
bool_value = client.fetch_boolean_value(
  flag_key: 'boolean_flag',
  default_value: false,
  evaluation_context: OpenFeature::SDK::EvaluationContext.new("is_friday" => true)
)

Hooks

Coming Soon! Issue available to be worked on.

<!-- [Hooks](https://openfeature.dev/docs/reference/concepts/hooks) allow for custom logic to be added at well-defined points of the flag evaluation life-cycle. Look [here](https://openfeature.dev/ecosystem/?instant_search%5BrefinementList%5D%5Btype%5D%5B0%5D=Hook&instant_search%5BrefinementList%5D%5Btechnology%5D%5B0%5D=Ruby) for a complete list of available hooks. If the hook you're looking for hasn't been created yet, see the [develop a hook](#develop-a-hook) section to learn how to build it yourself. Once you've added a hook as a dependency, it can be registered at the global, client, or flag invocation level. --> <!-- TODO: code example of setting hooks at all levels -->

Logging

Coming Soon! Issue available to work on.

<!-- TODO: talk about logging config and include a code example -->

Domains

Clients can be assigned to a domain. A domain is a logical identifier which can be used to associate clients with a particular provider. If a domain has no associated provider, the default provider is used.

OpenFeature::SDK.configure do |config|
  config.set_provider(OpenFeature::SDK::Provider::NoOpProvider.new, domain: "legacy_flags")
end

# Create a client for a different domain, this will use the provider assigned to that domain
legacy_flag_client = OpenFeature::SDK.build_client(domain: "legacy_flags")

Eventing

Coming Soon! Issue available to be worked on.

<!-- Events allow you to react to state changes in the provider or underlying flag management system, such as flag definition changes, provider readiness, or error conditions. Initialization events (`PROVIDER_READY` on success, `PROVIDER_ERROR` on failure) are dispatched for every provider. Some providers support additional events, such as `PROVIDER_CONFIGURATION_CHANGED`. Please refer to the documentation of the provider you're using to see what events are supported. --> <!-- TODO: code example of a PROVIDER_CONFIGURATION_CHANGED event for the client and a PROVIDER_STALE event for the API -->

Shutdown

Coming Soon! Issue available to be worked on.

<!-- TODO The OpenFeature API provides a close function to perform a cleanup of all registered providers. This should only be called when your application is in the process of shutting down. ```ruby class MyProvider def shutdown # Perform any shutdown/reclamation steps with flag management system here # Return value is ignored end end ``` -->

Transaction Context Propagation

Coming Soon! Issue available to be worked on.

<!-- Transaction context is a container for transaction-specific evaluation context (e.g. user id, user agent, IP). Transaction context can be set where specific data is available (e.g. an auth service or request handler) and by using the transaction context propagator it will automatically be applied to all flag evaluations within a transaction (e.g. a request or thread). --> <!-- TODO: code example for global shutdown -->

Extending

Develop a provider

To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency. This can be a new repository or included in the existing contrib repository available under the OpenFeature organization. You’ll then need to write the provider by implementing the Provider duck.

class MyProvider
  def init
    # Perform any initialization steps with flag management system here
    # Return value is ignored
    # **Note** The OpenFeature spec defines a lifecycle method called `initialize` to be called when a new provider is set.
    # To avoid conflicting with the Ruby `initialize` method, this method should be named `init` when creating a provider.
  end

  def shutdown
    # Perform any shutdown/reclamation steps with flag management system here
    # Return value is ignored
  end

  def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil)
    # Retrieve a boolean value from provider source
  end

  def fetch_string_value(flag_key:, default_value:, evaluation_context: nil)
    # Retrieve a string value from provider source
  end

  def fetch_number_value(flag_key:, default_value:, evaluation_context: nil)
    # Retrieve a numeric value from provider source
  end

  def fetch_integer_value(flag_key:, default_value:, evaluation_context: nil)
    # Retrieve a integer value from provider source
  end

  def fetch_float_value(flag_key:, default_value:, evaluation_context: nil)
    # Retrieve a float value from provider source
  end

  def fetch_object_value(flag_key:, default_value:, evaluation_context: nil)
    # Retrieve a hash value from provider source
  end
end

Built a new provider? Let us know so we can add it to the docs!

Develop a hook

Coming Soon! Issue available to be worked on.

<!-- To develop a hook, you need to create a new project and include the OpenFeature SDK as a dependency. This can be a new repository or included in [the existing contrib repository](https://github.com/open-feature/ruby-sdk-contrib) available under the OpenFeature organization. Implement your own hook by conforming to the `Hook interface`. To satisfy the interface, all methods (`Before`/`After`/`Finally`/`Error`) need to be defined. To avoid defining empty functions, make use of the `UnimplementedHook` struct (which already implements all the empty functions). --> <!-- TODO: code example of hook implementation --> <!-- > Built a new hook? [Let us know](https://github.com/open-feature/openfeature.dev/issues/new?assignees=&labels=hook&projects=&template=document-hook.yaml&title=%5BHook%5D%3A+) so we can add it to the docs! --> <!-- x-hide-in-docs-start -->

⭐️ Support the project

🤝 Contributing

Interested in contributing? Great, we'd love your help! To get started, take a look at the CONTRIBUTING guide.

Thanks to everyone who has already contributed

<a href="https://github.com/open-feature/ruby-sdk/graphs/contributors"> <img src="https://contrib.rocks/image?repo=open-feature/ruby-sdk" alt="Pictures of the folks who have contributed to the project" /> </a>

Made with contrib.rocks.

<!-- x-hide-in-docs-end -->