Ruby

Installation

Install the tggl package:

gem install tggl

Quick start

Instantiate a client with your API key and call eval_context to evaluate a context. A single API call evaluating all flags is performed, making all subsequent flag checking methods extremely fast.

This means that you do not need to cache results of the get method since it does not trigger an API call, it simply looks up the data in the already fetched response.

require "tggl"
 
$client = Tggl::Client.new("YOUR_API_KEY")
 
# An API call to Tggl is performed here
$flags = $client.eval_context({
  userId: "abc",
  email: "foo@gmail.com",
  country: "FR",
  # ...
})
 
if $flags.get("my-feature", "Variation A") == "Variation A"
  # ...
end

Hard-coded fallback values

You must provide a fallback value that will be returned if the flag is inactive, does not exist, or in case of network error:

if $flags.get("my-feature", "Variation A") == "Variation A"
  # This code will be executed if 'my-feature' is either:
  # - explicitly equal to 'Variation A'
  # - deleted or network error
end

Evaluating contexts in batches

If you have multiple contexts to evaluate at once, you can batch your calls in a single HTTP request which is much more performant:

$result = $client.eval_contexts([
  { userId: 'foo' },
  { userId: 'bar' },
])
 
# Responses are returned in the same order
$fooFlags = $result[0]
$barFlags = $result[1]

Evaluate flags locally

It is possible to evaluate flags locally on the server but not recommended unless you have performance issues evaluating flags at a high frequency. Evaluating flags locally forces you to maintain the copy of flags configuration up to date and might be a source of issues.

Danger

Make sure to add the right keys to your context to be perfectly consistent with the Tggl API.

require "tggl"
 
$client = Tggl::LocalClient.new("YOUR_SERVER_API_KEY")
 
# This method performs an API call and updates the flags configuration
$client.fetch_config
 
# Evaluation is performed locally
$client.get({ userId: "foo" }, "my-feature", 42)

You should cache the configuration and instantiate the client with the cached version, so you don't need to call fetchConfig for every request:

$cachedConfig = $client.fetch_config
 
# Create another client with the cached config
$client = Tggl::LocalClient.new("YOUR_SERVER_API_KEY",
  config: $cachedConfig
)

It is your responsibility to keep the cache of the configuration up to date by calling fetchConfig when needed. You can use webhooks to be notified when the configuration changes.