Ruby on Rails integration
This guide targets Ruby on Rails 7.1+ with Ruby 3.2 or later. The avsb gem ships a Railtie that auto-connects the SDK and a Rack middleware that scopes flag evaluation to each request. By the end you will be reading flags in controllers and views, tracking conversions, and shutting down cleanly on SIGTERM.
Install
1gem 'avsb'1bundle installObtain your SDK key
Open your A vs B project, go to Settings → Environments, and copy the Server SDK key. Store it in your credentials file or an environment variable — never commit it to source control.
1# Using Rails credentials2rails credentials:edit3# Add: avsb_sdk_key: sdk_production_...4
5# Or use an environment variable6export AVSB_SDK_KEY=sdk_production_...Configure the SDK
Create an initialiser. The Railtie picks this up automatically — you do not need to register middleware manually; the gem inserts Avsb::Rack::Middleware into the Rack stack for you.
1Avsb.configure do |config|2 config.sdk_key = Rails.application.credentials.avsb_sdk_key ||3 ENV['AVSB_SDK_KEY']4
5 # Optional: build the eval context from every Rack request.6 # The proc receives the Rack env hash and must return a Hash.7 config.context_from = lambda do |rack_env|8 request = ActionDispatch::Request.new(rack_env)9 {10 kind: 'user',11 key: request.session[:user_id]&.to_s || 'anon',12 plan: request.headers['X-User-Plan'] || 'free',13 }14 end15endRead a flag in a controller
The middleware makes a scoped client available via the Rails helper methodavsb_client inside controllers. You can also use the view helperavsb_flag in ERB templates.
1class CheckoutController < ApplicationController2 def show3 # avsb_client is a per-request scoped client4 @show_new_checkout = avsb_client.get_bool_flag('checkout-v2', false)5 @ui_theme = avsb_client.get_string_flag('ui-theme', 'default')6 end7end1<%# In views, use the helper directly %>2<% if avsb_flag('checkout-v2', default: false) %>3 <%= render 'checkout/new' %>4<% else %>5 <%= render 'checkout/legacy' %>6<% end %>Track an event
1def complete2 avsb_client.track('purchase', value: 149.99, properties: { currency: 'usd' })3 redirect_to root_path4endIdentify a user
After login you may want to re-scope to the authenticated user without relying on the middleware's context. Call Avsb.server.for_userdirectly:
1scoped = Avsb.server.for_user(2 kind: 'user',3 key: current_user.id.to_s,4 plan: current_user.plan,5 org: current_user.org_key,6)7show_new = scoped.get_bool_flag('checkout-v2', false)kind: 'multi' shape described in the multi-context guide.Graceful shutdown
Rails forwards SIGTERM to Puma, which waits for in-flight requests to complete before shutting down. Register an at_exit hook or a Puma plugin to flush the SDK before the process exits:
1at_exit { Avsb.server&.close }Testing
Use the Avsb::TestUtils module to stub flag values without network access. The with_flag helper scopes overrides to a single test.
1require 'rails_helper'2require 'avsb/test_utils'3
4RSpec.describe CheckoutController, type: :controller do5 include Avsb::TestUtils6
7 it 'renders new checkout when flag is on' do8 with_flag('checkout-v2', true) do9 get :show10 expect(assigns(:show_new_checkout)).to be true11 end12 end13
14 it 'renders legacy checkout by default' do15 with_flag('checkout-v2', false) do16 get :show17 expect(assigns(:show_new_checkout)).to be false18 end19 end20endWhat's next
- Ruby SDK reference — full
Avsb::ClientAPI including sticky bucketing and decision logging. - Multi-context targeting — combine user and organisation targeting attributes.
- Sticky bucketing — guarantee consistent variation assignment across requests using Redis.