Ruby API

type Rox

require 'rox/server/rox_server'
Rox::Server::RoxServer

The Rox class provides your application its primary interface with feature flags in the CloudBees Feature Flags system. This is the central repository for your application’s flags. It handles communications with the server to obtain the latest values, experiment settings, and configurations. It provides a mechanism through which you can use the feature flags to control your application’s behavior. It works with a RoxContainer object, which holds your application’s feature flags. The values contained in the provided container will be marked as feature flags and will appear in your application’s CloudBees Feature Flags dashboard once the application is run.

This class also allows you to manage Custom Properties. These can be static settings of type string, boolean, integer, or double, or you can use a generator type to provide a custom property that is dependent upon code state.

You can also use a RoxOptions object to configure some aspects of this type. You can set the verbosity level, custom platform, global freeze level, impression handler, and fetch handler.

setup

Rox::Server::RoxServer.setup("<ROLLOUT_KEY>"[, Rox::Server::RoxOptions]).join

Configures the Rox object to work with the provided application.

Parameter Modifier and Type Description

apiKey

string

The environment key provided by the CloudBees Feature Flags dashboard

options

RoxOptions (optional)

A RoxOptions instance with the desired configuration for this application

Returns

Thread that indicates when the SDK has received the configuration from the CloudBees Feature Flags servers.

register

Rox::Server::RoxServer.register(namespace, container)

Registers a feature flag container with the Rox client. The public member variables of this container will become a flag in your CloudBees Feature Flags dashboard and will be named with the object name.

Parameter Modifier and Type Description

namespace

string

The prefix namespace for all flags in this container

container

object

An object that contains your application’s feature flags

Example

Creating a new container and registering it:

require 'rox/server/flags/rox_flag'

class LoginContainer
  attr_accessor :include_github
  attr_accessor :include_facebook

  def initialize
    @include_github = Rox::Server::RoxFlag.new
    @include_facebook = Rox::Server::RoxFlag.new
  end
end

login_container = LoginContainer.new

Rox::Server::RoxServer.register(:login, login);

The flag and configuration in the dashboard will have the names login.include_facebook and login.include_github. This is very handy if you want to have groups of flags and configuration.

If you don’t need a namespace, you can set the namespace to an empty string.

set_custom_boolean_property

Rox::Server::RoxServer.set_custom_boolean_property(name, value = nil, &block)

Sets a custom boolean property on the Rox client. The value can either be a computed boolean, or a boolean.

Parameter Modifier and Type Description

name

value

The name of the property to create

boolean

boolean

(context) → boolean

Example:

Creating a new container and registering it:

Rox::Server::RoxServer.set_custom_boolean_property('hasItemsInShoppingCart') do |context|
  context['shopping_cart'].empty?
end

set_custom_string_property

Rox::Server::RoxServer.set_custom_string_property(name, value = nil, &block)

Sets a custom string property on the Rox client. The value can either be a computed string, or a string.

Parameter Modifier and Type Description

name

value

The name of the property to create

string

string

(context) → string

Example:

Creating a new container and registering it:

Rox::Server::RoxServer.set_custom_string_property('email') do |context|
  context['user'].email
end

set_custom_float_property

Rox::Server::RoxServer.set_custom_float_property(name, value = nil, &block)

Sets a custom float property on the Rox client. The value can either be a computed float, or a float.

Parameter Modifier and Type Description

name

value

The name of the property to create

float

float

(context) → float

Example

Creating a new container and registering it:

Rox::Server::RoxServer.set_custom_float_property('height') do |context|
  context['user'].height
end

set_custom_int_property

Rox::Server::RoxServer.set_custom_int_property(name, value = nil, &block)

Sets a custom int property on the Rox client. The value can either be a computed int, or a int.

Parameter Modifier and Type Description

name

value

The name of the property to create

int

int

(context) → int

Example

Creating a new container and registering it:

Rox::Server::RoxServer.set_custom_int_property('age') do |context|
  context['user'].age
end

set_custom_semver_property

Rox::Server::RoxServer.set_custom_semver_property(name, value = nil, &block)

Sets a custom semver property on the Rox client. The value can either be a computed semver, or a semver.

Parameter Modifier and Type Description

name

value

The name of the property to create

string

string

(context) → string

Example

Creating a new container and registering it:

Rox::Server::RoxServer.set_custom_semver_property('ruby version', RUBY_VERSION)

Global Context

Rox::Server::RoxServer.context

Set a global context, this context will be available to all flags evaluations.

global_context = {'user': user}
Rox::Server::RoxServer.context = global_context

fetch

thread = Rox::Server::RoxServer.fetch

Create a network request for the latest configuration.

type RoxOptions

Rox::Server::RoxOptions

RoxOptions covers configuration options for the Rox client. This includes settings like the verbosity of the logging. Instances of this type should be created using server.NewRoxOptions.

Example

Here is an example of setting up a new RoxOptions object. This object sets the version, logger, provides an impression handler, and includes a configurationFetchedHandler.

option = Rox::Server::RoxOptions.new(
      configuration_fetched_handler: configuration_fetched_handler,
      impression_handler: impression_handler,
      logger: Logger.new
    )

    Rox::Server::RoxServer.setup('<ROLLOUT_KEY>', option).join

version

Set the version of the service running the CloudBees Feature Flags SDK.

fetch_interval

Set the polling interval for fetching configuration from the CloudBees Feature Flags storage service.

logger

Set the Logger to be used for logging.

class Logger
    def debug(message, ex = nil)
      puts 'Before Rox.Setup', message
    end

    def error(message, ex = nil)
      puts 'Before Rox.Setup', message
    end

    def warn(message, ex = nil)
      puts 'Before Rox.Setup', message
    end
  end

  option = Rox::Server::RoxOptions.new(
    logger: Logger.new
  )

configuration_fetched_handler

Set the configuration event handler, to add actions after configurations were fetched

impression_handler

Set the impression event handler, to add actions after an impression

type RoxFlag

require 'rox/server/flags/rox_flag'
Rox::Server::RoxFlag.new

RoxFlag is a boolean feature flag. It can be either true or false

enabled?

enabled?(context)

Returns true if the flag is enabled based on given context.

enabled

enabled(context){ || block } -> Void

Runs the block if flag is true.

disabled

disabled(context){ || block } -> Void

Runs the block if flag is false.

name

@name

The name of the Flag.

Class RoxVariant

import  "github.com/rollout/rox-go/server"
Rox::Server::RoxVariant.new

RoxVariant is a feature flag object that can have values more complex than a simple boolean true or false. RoxVariant accepts a list of possible values for the feature flag and a default value. This list of values will be used when selecting new values for the feature and will be available for override via the dashboard.

Example

require 'rox/server/flags/rox_flag'

class MyContainer
  attr_accessor :title_color

 def initialize
    @title_color = Rox::Server:: Rox::Server::RoxVariant.new('red', ['red', 'blue', 'green'])
  end
end

container = MyContainer.new

Rox::Server::RoxServer.register('', container);
Rox::Server::RoxServer.setup('<ROLLOUT_KEY>', option).join;

value

@value

Returns the variant’s value considering the context given.

name

@name

The name of the Variant.