Skip to main content

Ruby Server SDK Getting Started

RubyGems GitHub

Please follow the installation procedure and then run the following code:

Please note; the default mode is to use Local Bucketing - to use cloud bucketing - set the enable_cloud_bucketing option to true.

The last argument to DevCycle::Client.new tells the sdk whether you want to wait for initialization - meaning that the method will block until the first config is fetched and set successfully or an unrecoverable error occurs during initialization.

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

# Setup authorization
devcycle_client = DevCycle::Client.new(ENV['DEVCYCLE_SERVER_SDK_KEY'], DevCycle::Options.new, true)
user = DevCycle::User.new({ user_id: 'user_id_example' })

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

Initializing the SDK in a Rails App

The SDK can be initialized in an initializer file:

Step 1: Create a new file in config/initializers called devcycle.rb.

Step 2: Add the following code to the devcycle.rb file:

Rails.configuration.devcycle_client = DevCycle::Client.new(
ENV['DEVCYCLE_SERVER_SDK_KEY'],
DevCycle::Options.new,
true
)

Initializing the SDK in a Rails App Using Unicorn

When using Unicorn with the preload_app configuration set to true, the SDK needs to be initialized in the after_work block in the config/unicorn.rb file:

after_fork do |server, worker|
Rails.configuration.devcycle_client = DevCycle::Client.new(
ENV['DEVCYCLE_SERVER_SDK_KEY'],
DevCycle::Options.new,
true
)
end

Initializing the SDK in a Rails App Using Puma

When using Puma with the preload_app configuration set to true, the SDK needs to be initialized in the on_worker_boot block in the config/puma.rb file:

on_worker_boot do
Rails.configuration.devcycle_client = DevCycle::Client.new(
ENV['DEVCYCLE_SERVER_SDK_KEY'],
DevCycle::Options.new,
true
)
end

Initialization Options

The SDK exposes various initialization options which can be set when registering the DevCycle Client:

# 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: 'example@example.ca',
country: 'CA'
})
DevCycle OptionTypeDescription
loggerDevCycleLoggerLogger override to replace default logger
logLevelStringSet log level of the default logger. Options are: debug, info, warn, error. Defaults to info.
enableCloudBucketingBooleanSwitches the SDK to use Cloud Bucketing (via the DevCycle Bucketing API) instead of Local Bucketing.
enableEdgeDBBooleanEnables the usage of EdgeDB for DevCycle that syncs User Data to DevCycle.
NOTE: This is only available with Cloud Bucketing.
configPollingIntervalMSNumberControls the polling interval in milliseconds to fetch new environment config changes, defaults to 10 seconds, minimum value is 1 second.
configPollingTimeoutMSNumberControls the request timeout to fetch new environment config changes, defaults to 5 seconds, must be less than the configPollingIntervalMS value, minimum value is 1 second.
eventFlushIntervalMSNumberControls the interval between flushing events to the DevCycle servers, defaults to 30 seconds.
disableAutomaticEventLoggingBooleanDisables logging of sdk generated events (e.g. aggVariableEvaluated, aggVariableDefaulted) to DevCycle.
disableCustomEventLoggingBooleanDisables logging of custom events, from track() method, and user data to DevCycle.
flushEventQueueSizeNumberControls the maximum size the event queue can grow to until a flush is forced. Defaults to 1000.
maxEventQueueSizeNumberControls the maximum size the event queue can grow to until events are dropped. Defaults to 2000.
apiProxyURLStringAllows the SDK to communicate with a proxy of DevCycle bucketing API / client SDK API.
enableBetaRealtimeUpdatesBooleanEnables the usage of Beta Realtime Updates for DevCycle. This feature is currently in beta.