Skip to main content

Ruby Server SDK Usage

RubyGems GitHub

User Object

The full user data must be passed into every method. The only required field is user_id. The rest are optional and are used by the system for user segmentation into variables and features.

See the User model in the Ruby user model doc for all accepted fields including custom fields.

user = DevCycle::User.new({ user_id: 'user_id_example' })

Get and use Variable by key

To get values from your Variables, devcycle_client.variable_value() is used to fetch variable values using the user data, variable key, coupled with a default value for the variable. The default variable will be used in cases where the user is not segmented into a feature using that variable, or the project configuration is unavailable to be fetched from DevCycle's CDN.

begin
# Get value of given variable by key, using default value if segmentation is not passed or variable does not exit
result = devcycle_client.variable_value(user, "variable-key", true)
p "Received value for 'variable-key': #{result}"
rescue
puts "Exception when calling DevCycle::Client->variable_value"
end

The default value can be of type string, boolean, number, or object.

If you would like to get the full Variable you can use devcycle_client.variable() instead. This contains fields such as: key, value, type, defaultValue, isDefaulted.

Getting all Features

You can fetch all segmented features for a user:

begin
# Get all features for user data
result = devcycle_client.all_features(user)
p result
rescue
puts "Exception when calling DevCycle::Client->all_features"
end

Getting all Variables

To grab all the segmented variables for a user:

begin
# Get all variables for user data
result = devcycle_client.all_variables(user)
p result
rescue
puts "Exception when calling DevCycle::Client->all_variables"
end
caution

This method is intended to be used for debugging and analytics purposes, not as a method for retrieving the value of Variables to change code behaviour. For that purpose, we strongly recommend using the individual variable access method described in Get and use Variable by key Using this method instead will result in no evaluation events being tracked for individual variables, and will not allow the use of other DevCycle features such as Code Usage detection

Track Events

Track a custom event for a user, pass in the user and event object.

Calling Track will queue the event, which will be sent in batches to the DevCycle servers.

event_data = DevCycle::Event.new({
type: "my-event",
target: "some_event_target",
value: 12,
meta_data: {
myKey: "my-value"
}
})

begin
# Post events for given user data
result = devcycle_client.track(user, event_data)
p result
rescue => e
puts "Exception when calling DevCycle::Client->track: #{e}"
end

Override Logger

To provide a custom logger, override the logger property of the SDK configuration.

options = DevCycle::Options.new(logger: @yourCustomLogger)

Set Client Custom Data

To assist with segmentation and bucketing you can set a custom data hash that will be used for all variable and feature evaluations. User specific custom data will override this client custom data.

begin
# Set client custom data
custom_data = {"some-key" : "some-value"}
devcycle_client.set_client_custom_data(custom_data)
rescue => e
puts "Exception when calling DevCycle::Client->set_client_custom_data: #{e}"
end
caution

Client Custom Data is only available for the Local Bucketing SDK

EdgeDB

EdgeDB allows you to save user data to our EdgeDB storage so that you don't have to pass in all the user data every time you identify a user. Read more about EdgeDB.

To get started, contact us at [email protected] to enable EdgeDB for your project.

Once you have EdgeDB enabled in your project, pass in the enableEdgeDB option to turn on EdgeDB mode for the SDK:

# Load the gem
require 'devcycle-ruby-server-sdk'

# Setup authorization
options = DevCycle::Options.new(enable_edge_db: true, enable_cloud_bucketing: true)

devcycle_client = DevCycle::Client.new("dvc_server_token_hash", options, true)
user = DevCycle::User.new({
user_id: 'test_user',
email: '[email protected]',
country: 'CA'
})

This will send a request to our EdgeDB API to save the custom data under the user test_user.

In the example, Email and Country are associated to the user test_user. In your next identify call for the same user_id, you may omit any of the data you've sent already as it will be pulled from the EdgeDB storage when segmenting to experiments and features.