Ruby Feature Flags

Ruby & Ruby on Rails Feature Flag Resources/Solutions

  • LaunchDarkly Ruby Feature Flag SDK – LaunchDarkly
    • An installable feature flag software development kit for Ruby and Ruby on Rails apps.  This SDK harnesses the LaunchDarkly app and allows you to target users and manage rollouts.  Feature flags are evaluated in microseconds.
    • Ruby Client Github
    • LD Client
  • The Rollout Gem – Leonard Garvey Reinteractive
  • Feature Flipper – Florian Munz
    • FeatureFlipper is a simple library that allows you to restrict certain blocks of code to certain environments. This is mainly useful in projects where you deploy your application from HEAD and don’t use branches.
  • Flip – Paul Annesley
    • Flip provides a declarative, layered way of enabling and disabling application functionality at run-time.
  • Setler – Chris Kelly
    • Setler is a Gem that lets one easily implement the “Feature Flags” pattern, or add settings to individual models. This is a cleanroom implementation of what the ‘rails-settings’ gem does. It’s been forked all over the place, and my favorite version of it doesn’t have any tests and doesn’t work with settings associated with models.
  • Switches – John Pignata
    • A gem that manages features switches in an application. Feature switches are dynamically configured. They can be turned on, on to a percentage of actors, on to a specific group of actors, or off without a code deploy. There are some excellent and mature projects that provide this functionality already: rollout and flipper.
  • Fluid Features – Phil Whelan
    • gem fluidfeatures-rails is a Ruby on Rails client for the API of FluidFeatures.com, which provides an elegant way to wrap new code so you have real-time control over rolling out new features to your user-base.
  • Chili – Jens Balvig
    • “Have you ever wanted to test out a new feature on only a subset of users? Did that implementation end up being lots of if/else statements embedded in the main code? If so, Chili can help.   Chili is built on top of Rails Engines and Deface and allows you to conditionally add new/modify existing views, while leaving the main code untouched.”
  • Simple Feature Flags – Jay Stakelon
    • “Selectively rolling out new bits of an app using feature flags isn’t exactly a new idea. But being reminded of it during the keynote preceding the overall awesomeness of Heroku’s developer conference last week, right in the midst of my work adding more and more faster and faster to Fullscreen’s products, made me think that it was high time we got something like this hooked up.”
  • Rails Feature Flags – ClearCove Software, Inc.
  • A simple feature toggle for a Rails app – Szymon Fiedler
    • “You’ve probably heard before about feature toggle. Theory looks fine on the paper, but you’re possibly wondering how to implement such feature in your Rails app.”
  • Bandiera – Springer Nature
    • “Bandiera is a simple, stand-alone feature flagging service that is not tied to any existing web framework or language as all communication is via a simple REST API. It also has a simple web interface for setting up and configuring flags.”
    • Github Repo 

———————

Ruby SDK Reference for LaunchDarkly

Source

This reference guide documents all of the methods available in our Ruby SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source– head to our Ruby SDK GitHub repository to look under the hood.

Ruby SDK Reference


This reference guide documents all of the methods available in our Ruby SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source– head to our Ruby SDK GitHub repository to look under the hood.

Getting started


If you haven’t taken a look at our Quickstart guide yet, we recommend starting there to see how install our SDK into your Ruby application.

Once the SDK is installed, you’ll want to create a single, shared instance of the LaunchDarkly client:

ld_client = LaunchDarkly::LDClient.new("YOUR_SDK_KEY")

LDClient must be a singleton

It’s important to make this a singleton– internally, the client instance maintains internal state that allows us to serve feature flags without making any remote requests. Be sure that you’re not instantiating a new client with every request.

Customizing your client


You can also customize the behavior of the client by creating a custom configuration object:

config = LaunchDarkly::Config.new({:connect_timeout => 1, :read_timeout => 1})
ld_client = LaunchDarkly::LDClient.new("YOUR_SDK_KEY", config)

The client constructor takes a configuration object as an optional parameter. In this example, we’ve set the connection timeout to LaunchDarkly to 1 second, and the read timeout to 2 seconds. The complete list of customizable parameters is as follows:

  • connect_timeout: Must be a number, in seconds, and controls the connection timeout to LaunchDarkly
  • read_timeout: Must be a number, in seconds, and controls the read timeout to LaunchDarkly
  • capacity: Must be a number, and controls the maximum size of the event buffer. LaunchDarkly sends events asynchronously, and buffers them for efficiency.
  • flush_interval: Must be a number, in seconds, and controls how long LaunchDarkly buffers events before sending them back to our server. If your server generates many events per second, we suggest decreasing the flush_interval and / or increasing capacity to meet your needs.
  • logger: A logger to use for messages from the LaunchDarkly client. Defaults to the Rails logger in a Rails environment, or stdout otherwise.
  • cache_store: A cache store for the Faraday HTTP caching library. Defaults to the Rails cache in a Rails environment, or a thread-safe in-memory store otherwise.
  • offline: Whether the client should be initialized in offline mode. In offline mode, default values are returned for all flags and no remote network requests are made.
  • stream: Whether streaming or polling should be used to receive flag updates.

Users


Feature flag targeting and rollouts are all determined by the user you pass to your variation calls. In our Ruby SDK, users are simply hashes. Here’s an example:

user = {
  :key => "aa0ceb",
  :firstName => "Ernestina",
  :lastName => "Evans",
  :email => "ernestina@example.com",
  :custom => {
    :groups => ["Google", "Microsoft"]
  }
}

Let’s walk through this snippet. The most important attribute is the user key– in this case we’ve used the hash"aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an e-mail address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.

All of the other attributes (like firstName, email, and the custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.

Besides the key, LaunchDarkly supports the following attributes at the “top level”. Remember, all of these are optional:

  • ip: Must be an IP address. If you provide an IP, LaunchDarkly will use a geolocation service to automatically infer a country for the user (unless you’ve already specified one).
  • firstName: Must be a string. If you provide a first name, you can search for users on the Users page by name.
  • lastName: Must be a string. If you provide a last name, you can search for users on the Users page by name.
  • country: Must be a string representing the country associated with the user.
  • email: Must be a string representing the user’s e-mail address. If an avatar URL is not provided, we’ll useGravatar to try to display an avatar for the user on the Users page.
  • avatar: Must be an absolute URL to an avatar image for the user.
  • name: Must be a string. You can search for users on the User page by name
  • anonymous: Must be a boolean. See the section below on anonymous users for more details.

In addition to built-in attributes, you can pass us any of your own user data by passing custom attributes, like thegroups attribute in the example above.

A note on types

Most of our built-in attributes (like names and e-mail addresses) expect string values. Custom attribute values can be strings, booleans (like true or false), numbers, or lists of strings, booleans or numbers.

If you enter a custom value on our dashboard that looks like a number or a boolean, it’ll be interpreted that way.

Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us– organizations, groups, account plans– anything you pass to us becomes available instantly on our dashboard.

Anonymous users


You can also distinguish logged-in users from anonymous users in the SDK, as follows:

user = {:key => "aa0ceb", :anonymous => true}

You will still need to generate a unique key for anonymous users– session IDs or UUIDs work best for this.

Anonymous users work just like regular users, except that they won’t appear on your Users page in LaunchDarkly. You also can’t search for anonymous users on your Features page, and you can’t search or autocomplete by anonymous user keys. This is actually a good thing– it keeps anonymous users from polluting your Users page!

Variation


The variation method determines which variation of a feature flag a user receives.

ld_client.variation("your.feature.key", user, false)

variation calls take the feature flag key, a user, and a default value.

The default value will only be returned if an error is encountered– for example, if the feature flag key doesn’t exist or the user doesn’t have a key specified.

The variation call will automatically create a user in LaunchDarkly if a user with that user key doesn’t exist already. There’s no need to create users ahead of time (but if you do need to, take a look at Identify).

All flags


The all_flags method produces a map of feature flag keys to their values for a specific user.

This method can be useful for passing feature flags to your front-end. In particular, it can be used to provide bootstrap flag settings for our JavaScript SDK.

ld_client.all_flags

Track


The track method allows you to record actions your users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests. Here’s a simple example:

ld_client.track("Signed up", user)

You can also attach an extra hash containing arbitrary data to your event:

ld_client.track("Completed purchase", {:price => 320})

Identify


The identify method creates or updates users on LaunchDarkly, making them available for targeting and autocomplete on the dashboard. In most cases, you won’t need to call identify— the variation call will automatically create users on the dashboard for you. identify can be useful if you want to pre-populate your dashboard before launching any features.

ld_client.identify(user)

Secure mode hash


The secure_mode_hash method computes an HMAC signature of a user signed with the client’s SDK key. If you’re using our JavaScript SDK for client-side flags, this method generates the signature you need for secure mode.

ld_client.secure_mode_hash(user)

Flush


Internally, the LaunchDarkly SDK keeps an event buffer for track and identify calls. These are flushed periodically in a background thread. In some situations (for example, if you’re testing out the SDK in a REPL), you may want to manually call flush to process events immediately.

ld_client.flush

Note that the flush interval is configurable– if you need to change the interval, you can do so when configuring your client instance.

Offline mode


In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on premise, it might make sense to fall back to defaults when running on premise. Offline mode lets you do this easily.

config = LaunchDarkly::Config.new({:offline => true})
ld_client = LaunchDarkly::LDClient.new("YOUR_SDK_KEY", config)
ld_client.variation("any.feature.flag", user, false) # will always return the default value (false)

Close


Close safely shuts down the client instance and releases all resources associated with the client. In most long-running applications, you should not have to call close.

ld_client.close