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