Ruby SDK reference

7 minute readReference

The following information is for the latest SDK version (6.x). The CloudBees platform requires that your installed SDK version be at least 6.x. Please install the latest SDK by following the instructions in the platform UI or in the SDK installation documentation.

Any updates to version 6.x are noted in the platform changelog.

type Rox

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

The Rox class provides an interface for CloudBees Feature Management to manage feature flags that control the behavior of your application. This is the central repository for your application’s flags. This class handles communications with the server to obtain the latest flag values, implement flag settings, and set up flag configurations. It works with a RoxContainer object, which holds your application’s feature flags. The values in the provided container are marked as feature flags and display in the CloudBees Feature Management UI 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 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. 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-specific SDK key provided by the CloudBees platform UI.

options

RoxOptions (optional)

A RoxOptions instance with the desired configuration for this application.

Returns a thread that indicates when the SDK has received the configuration from the CloudBees platform.

shutdown

Rox::Server::RoxServer.shutdown

Performs graceful shutdown of the Rox object to close and clean all background tasks and threads. Shutdown can only be used when Rox has been successfully set up; otherwise it results in a log warning. You can call Setup again after Shutdown. In that case, register and setCustomProperty use the new Rox object, and register the objects to the UI on the next setup call.

register

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

Registers a feature flag container with the Rox client. The public member variables of this container are a flag in the platform UI and are named with the object name. When using register, the container does not use namespace, so flag names do not have a prefix. It is equivalent to register_with_namespace with a namespace as an empty string.

Parameter Modifier and type Description

namespace

string

The prefix namespace for all flags in this container (defaults to '').

container

object

An object that contains your application’s feature flags.

The following example creates and registers a new container.

require 'rox/server/rox_server' 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 class SystemContainer attr_accessor :showNewUserScreen attr_accessor :useBulkFetch def initialize @showNewUserScreen = Rox::Server::RoxFlag.new @useBulkFetch = Rox::Server::RoxFlag.new end end login_container = LoginContainer.new system_container = SystemContainer.new Rox::Server::RoxServer.register_with_namespace('login', login_container) Rox::Server::RoxServer.register(system_container)

The flags in the UI have the names login.include_facebook, login.include_github, showNewUserScreen, and useBulkFetch. The namespace is useful for organizing flags.

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 be either a Boolean or a computed Boolean.

Parameter Modifier and type Description

name

value

The name of the property to create.

Boolean

boolean

(context) → boolean (the value of the property, or a code block to compute the value for a given context).

To create a new container and register 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 be either a string or a computed string.

Parameter Modifier and type Description

name

value

The name of the property to create.

string

string

(context) → string (the value of the property, or a code block to compute the value for a given context).

To create a new container and register 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 be either a float or a computed float.

Parameter Modifier and type Description

name

value

The name of the property to create

float

float

(context) → float (the value of the property, or a code block to compute the value for a given context).

To create a new container and register 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 be either an integer or a computed integer.

Parameter Modifier and type Description

name

value

The name of the property to create.

int

int

(context) → int (the value of the property, or a code block to compute the value for a given context).

To create a new container and register 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 be either a semver or a computed semver type.

Parameter Modifier and type Description

name

value

The name of the property to create

string

string

(context) → string (the value of the property, or a code block to compute the value for a given context).

To create a new container and register it:

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

Global context

Rox::Server::RoxServer.context

Sets a global context that is available to all flag evaluations.

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

fetch

thread = Rox::Server::RoxServer.fetch

Creates a network request for the latest configuration.

use_userspace_unhandled_error_handler

Rox::Server::RoxServer.use_userspace_unhandled_error_handler do |exception_source, exception_trigger, exception| # ... end

A handler for all user function errors to assist with debugging and understanding unexpected flag values. If not set, errors are logged to the logger.

CloudBees recommends wrapping all user methods with try-except to handle errors in the relevant context, rather than handling errors after.

When invoking the user_unhandled_error_invoker, the arguments passed are the exception_source, exception_trigger and exception. The exception_source is the original handler that caused the exception.

The exception_trigger is an enum for the user to delegate the type:

class ExceptionTrigger DYNAMIC_PROPERTIES_RULE = 1 CONFIGURATION_FETCHED_HANDLER = 2 IMPRESSION_HANDLER = 3 CUSTOM_PROPERTY_GENERATOR = 4 end

The exception is the exception that was originally raised.

type RoxOptions

Rox::Server::RoxOptions

RoxOptions covers configuration options, including logging verbosity settings for the Rox client. Create instances of this type using server.NewRoxOptions.

The following example sets up a new RoxOptions object. This object sets the version, creates a logger, provides an impression handler, and includes a configurationFetchedHandler.

require 'rox/server/rox_server' option = Rox::Server::RoxOptions.new( configuration_fetched_handler: configuration_fetched_handler, impression_handler: impression_handler, dynamic_property_rule_handler: dynamic_property_handler, logger: Logger.new, version: '2.0.0', fetch_interval: 120 ) Rox::Server::RoxServer.setup('<ROLLOUT_KEY>', option).join

version

Sets the version of the service running the CloudBees platform SDK.

fetch_interval

Sets the polling interval (in seconds) for fetching the configuration from the CloudBees platform storage service. The default is 60 seconds, and it cannot be set to below 30 seconds.

logger

Sets 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

Sets the configuration event handler, to add actions after configurations are fetched.

proc do |configuration_fetcher_args| # .. end

Properties of the argument, below, are passed into the configuration_fetched_handler.

Property Description

fetcher_status

Enum indicating the fetch result; can be APPLIED_FROM_EMBEDDED, APPLIED_FROM_LOCAL_STORAGE, APPLIED_FROM_NETWORK, or ERROR_FETCHED_FAILED.

creation_date

Date and time when the fetched configurations are created and signed.

error_details

Enum indicating which error occurred while fetching and setting up new configurations. Can be CORRUPTED_JSON, EMPTY_JSON, SIGNATURE_VERIFICATION_ERROR, NETWORK_ERROR, MISMATCH_APP_KEY, UNKNOWN, or NO_ERROR.

has_changes

A Boolean indication (as opposed to a configuration change).

impression_handler

Sets the impression event handler to add actions after an impression.

proc do |impression_handler_args| # .. end

Properties of the argument to be passed into the impression_handler are detailed below.

Property Description

reporting_value

Object that contains name (string), value (string), and targeting (boolean).

context

The context in which the impression is called. This is a merged context containing the global context and the call’s context.

dynamic_property_rule_handler

The dynamic custom property generator is called when an explicit custom property definition does not exist on the client side.

proc do |prop_name, context| # .. end

If you do not set the dynamic_property_rule_handler, it activates the default method, which attempts to extract the property value from context.

Table 1. Arguments passed into the dynamic_property_rule_handler
Property Description

prop_name

The property name request by the flag evaluation.

context

The context used to evaluate the flag.

Refer to Properties, custom properties, and target groups for more information.

roxy_url

A roxy URL.

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 the given context.

enabled

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

Runs the block if the flag is true.

disabled

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

Runs the block if the flag is false.

name

@name

The name of the flag.

Class RoxString

require 'rox/server/rox_server'
Rox::Server::RoxString.new

RoxString is a feature flag object that can have string values. RoxString accepts a list of possible values for the feature flag and a default value. Use this list when selecting new values for the feature, and you can override the value via the UI.

require 'rox/server/rox_string' class MyContainer attr_accessor :title_color def initialize @title_color = Rox::Server::RoxString.new('red', ['red', 'blue', 'green']) end end container = MyContainer.new Rox::Server::RoxServer.register(container) Rox::Server::RoxServer.setup('<ROLLOUT_KEY>').join

value

@value

Returns the RoxString value, considering the given context.

name

@name

The name of the RoxString.

Class RoxInt

require 'rox/server/rox_int'

RoxInt is a feature flag object that can have int values. RoxInt accepts a list of possible int values for the feature flag and a default value. This list of values is used when selecting new values for the feature and is available for override in the UI.

title_size_flag = Rox::Server::RoxInt.new(14, [14, 18, 24]) title_size = title_size_flag.value

value

flag.value(context = nil)

Returns the RoxInt value, considering the given context.

Class RoxDouble

require 'rox/server/rox_double'

RoxDouble is a feature flag object that can have double values. RoxDouble accepts a list of possible double values for the feature flag and a default value. This list of values is used when selecting new values for the feature and is available for override in the UI.

Example

price_options_flag = Rox::Server::RoxDouble.new(19.99, [19.99, 29.99, 40.5]) price = price_options_flag.value

value

flag.value(context = nil)

Returns the RoxDouble value, considering the given context.