The project is in a healthy, maintained state
This is the ruby client for Unleash, a powerful feature toggle system that gives you a great overview over all feature toggles across all your applications and services.


~> 2.1
~> 0.8
~> 12.3
~> 3.9
~> 0.80
~> 3.8


~> 0.1.6
 Project Readme


Build Status Coverage Status Gem Version

Unleash client so you can roll out your features with confidence.

Leverage the Unleash Server for powerful feature toggling in your ruby/rails applications.

Supported Ruby Interpreters

  • MRI 3.0
  • MRI 2.7
  • MRI 2.6
  • MRI 2.5
  • jruby


Add this line to your application's Gemfile:

gem 'unleash', '~> 3.2.3'

And then execute:

$ bundle

Or install it yourself as:

$ gem install unleash


It is required to configure:

  • url of the unleash server
  • app_name with the name of the runninng application.
  • custom_http_headers with {'Authorization': '<API token>'} when using Unleash v4.0.0 and later.

Please substitute the example '' for the url of your own instance.

It is highly recommended to configure:

  • instance_id parameter with a unique identifier for the running instance.
Unleash.configure do |config|
  config.app_name            = 'my_ruby_app'
  config.url                 = ''
  config.custom_http_headers = {'Authorization': '<API token>'}

or instantiate the client with the valid configuration:

UNLEASH = '', app_name: 'my_ruby_app', custom_http_headers: {'Authorization': '<API token>'})

List of Arguments

Argument Description Required? Type Default Value
url Unleash server URL. Y String N/A
app_name Name of your program. Y String N/A
instance_id Identifier for the running instance of program. Important so you can trace back to where metrics are being collected from. Highly recommended be be set. N String random UUID
environment Environment the program is running on. Could be for example prod or dev. Not yet in use. N String default
project_name Name of the project to retrieve features from. If not set, all feature flags will be retrieved. N String nil
refresh_interval How often the unleash client should check with the server for configuration changes. N Integer 15
metrics_interval How often the unleash client should send metrics to server. N Integer 30
disable_client Disables all communication with the Unleash server, effectively taking it offline. If set, is_enabled? will always answer with the default_value and configuration validation is skipped. Defeats the entire purpose of using unleash, but can be useful in when running tests. N Boolean false
disable_metrics Disables sending metrics to Unleash server. N Boolean false
custom_http_headers Custom headers to send to Unleash. As of Unleash v4.0.0, the Authorization header is required. For example: {'Authorization': '<API token>'} N Hash {}
timeout How long to wait for the connection to be established or wait in reading state (open_timeout/read_timeout) N Integer 30
retry_limit How many consecutive failures in connecting to the Unleash server are allowed before giving up. Use Float::INFINITY if you would like it to never give up. N Numeric 5
backup_file Filename to store the last known state from the Unleash server. Best to not change this from the default. N String Dir.tmpdir + "/unleash-#{app_name}-repo.json
logger Specify a custom Logger class to handle logs for the Unleash client. N Class
log_level Change the log level for the Logger class. Constant from Logger::Severity. N Constant Logger::WARN

For in a more in depth look, please see lib/unleash/configuration.rb.

Usage in a plain Ruby Application

require 'unleash'
require 'unleash/context'

@unleash = 'my_ruby_app', url: '', custom_http_headers: {'Authorization': '<API token>'})

feature_name = "AwesomeFeature"
unleash_context =
unleash_context.user_id = 123

if @unleash.is_enabled?(feature_name, unleash_context)
  puts " #{feature_name} is enabled according to unleash"
  puts " #{feature_name} is disabled according to unleash"

Usage in a Rails Application

Add Initializer

Put in config/initializers/unleash.rb:

Unleash.configure do |config|
  config.app_name = Rails.application.class.parent.to_s
  config.url      = ''
  # config.instance_id = "#{Socket.gethostname}"
  config.logger   = Rails.logger
  config.environment = Rails.env


For config.instance_id use a string with a unique identification for the running instance. For example: it could be the hostname, if you only run one App per host. Or the docker container id, if you are running in docker. If it is not set the client will generate an unique UUID for each execution.

Add Initializer if using Puma

In puma.rb ensure that the unleash client is configured and instantiated as below, inside the on_worker_boot code block:

on_worker_boot do
  # ...

  Unleash.configure do |config|
    config.app_name    = Rails.application.class.parent.to_s
    config.environment = Rails.env
    config.url                 = ''
    config.custom_http_headers = {'Authorization': '<API token>'}
  Rails.configuration.unleash =

Instead of the configuration in config/initializers/unleash.rb.

Add Initializer if using Phusion Passenger

The unleash client needs to be configured and instantiated inside the PhusionPassenger.on_event(:starting_worker_process) code block due to smart spawning:

The initializer in config/initializers/unleash.rb should look like:

PhusionPassenger.on_event(:starting_worker_process) do |forked|
  if forked
    Unleash.configure do |config|
      config.app_name    = Rails.application.class.parent.to_s
      # config.instance_id = "#{Socket.gethostname}"
      config.logger      = Rails.logger
      config.environment = Rails.env
      config.url                 = ''
      config.custom_http_headers = {'Authorization': '<API token>'}


Set Unleash::Context

Be sure to add the following method and callback in the application controller to have @unleash_context set for all requests:

Add in app/controllers/application_controller.rb:

  before_action :set_unleash_context

  def set_unleash_context
    @unleash_context =
      remote_address: request.remote_ip,
      user_id: session[:user_id]

Or if you see better fit, only in the controllers that you will be using unleash.

Sample usage

Then wherever in your application that you need a feature toggle, you can use:

if UNLEASH.is_enabled? "AwesomeFeature", @unleash_context
  puts "AwesomeFeature is enabled"

or if client is set in Rails.configuration.unleash:

if Rails.configuration.unleash.is_enabled? "AwesomeFeature", @unleash_context
  puts "AwesomeFeature is enabled"

If the feature is not found in the server, it will by default return false. However you can override that by setting the default return value to true:

if UNLEASH.is_enabled? "AwesomeFeature", @unleash_context, true
  puts "AwesomeFeature is enabled by default"

Another possibility is to send a block, Lambda or Proc to evaluate the default value:

net_check_proc = proc do |feature_name, context|

if UNLEASH.is_enabled?("AwesomeFeature", @unleash_context, &net_check_proc)
  puts "AwesomeFeature is enabled by default if you are in the 10.0.0.* network."


awesomeness = 10[:coolness] = 10

if UNLEASH.is_enabled?("AwesomeFeature", @unleash_context) { |feat, ctx| awesomeness >= 6 &&[:coolness] >= 8 }
  puts "AwesomeFeature is enabled by default if both the user has a high enought coolness and the application has a high enough awesomeness"


  • The block/lambda/proc can use feature name and context as an arguments.
  • The client will evaluate the fallback function once per call of is_enabled(). Please keep this in mind when creating your fallback function!
  • The returned value of the block should be a boolean. However the client will coerce the result to boolean via !!.
  • If both a default_value and fallback_function are supplied, the client will define the default value by ORing the default value and the output of the fallback function.

Alternatively by using if_enabled you can send a code block to be executed as a parameter:

UNLEASH.if_enabled "AwesomeFeature", @unleash_context, true do
  puts "AwesomeFeature is enabled by default"

Note: if_enabled only supports default_value, but not fallback_function.


If no variant is found in the server, use the fallback variant.

fallback_variant = 'default', enabled: true, payload: {"color" => "blue"})
variant = UNLEASH.get_variant "ColorVariants", @unleash_context, fallback_variant

puts "variant color is: #{variant.payload.fetch('color')}"

Client methods

Method Name Description Return Type
is_enabled? Check if feature toggle is to be enabled or not. Boolean
enabled? Alias to the is_enabled? method. But more ruby idiomatic. Boolean
if_enabled Run a code block, if a feature is enabled. yield
get_variant Get variant for a given feature Unleash::Variant
shutdown Save metrics to disk, flush metrics to server, and then kill ToggleFetcher and MetricsReporter threads. A safe shutdown. Not really useful in long running applications, like web applications. nil
shutdown! Kill ToggleFetcher and MetricsReporter threads immediately. nil

For the full method signatures, please see client.rb

Local test client

# cli unleash client:
bundle exec bin/unleash-client --help

# or a simple sample implementation (with values hardcoded):
bundle exec examples/simple.rb

Available Strategies

This client comes with the all the required strategies out of the box:

  • ApplicationHostnameStrategy
  • DefaultStrategy
  • GradualRolloutRandomStrategy
  • GradualRolloutSessionIdStrategy
  • GradualRolloutUserIdStrategy
  • RemoteAddressStrategy
  • UnknownStrategy
  • UserWithIdStrategy


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at

Please include tests with any pull requests, to avoid regressions.