Ruby SDK

Use Flipswitch with Ruby via OpenFeature OFREP

Flipswitch works with Ruby applications through the OpenFeature SDK and its native OFREP provider — no custom Flipswitch package needed.

GitHub: open-feature/ruby-sdk · open-feature/ruby-sdk-contrib

This integration uses the OpenFeature Remote Evaluation Protocol (OFREP), which evaluates flags via polling. Real-time SSE streaming is available in official Flipswitch SDKs.

Requirements

  • Ruby 3.1+
  • Bundler

Installation

gem install openfeature-sdk openfeature-ofrep-provider

Or add to your Gemfile:

gem 'openfeature-sdk'
gem 'openfeature-ofrep-provider'

Quick Start

require 'openfeature/sdk'
require 'openfeature/contrib/providers/ofrep'

provider = OpenFeature::Contrib::Providers::OFREP.new(
  base_url: 'https://api.flipswitch.io',
  headers: { 'X-API-Key' => 'YOUR_API_KEY' }
)

OpenFeature::SDK.configure do |config|
  config.set_provider(provider)
end

client = OpenFeature::SDK.build_client
dark_mode = client.fetch_boolean_value(flag_key: 'dark-mode', default_value: false)

Evaluation Context

Pass user attributes for targeting:

context = OpenFeature::SDK::EvaluationContext.new(
  targeting_key: 'user-123',
  email: 'user@example.com',
  plan: 'premium',
  country: 'SE'
)

show_feature = client.fetch_boolean_value(
  flag_key: 'new-feature',
  default_value: false,
  evaluation_context: context
)

Detailed Evaluation

Get full evaluation details including variant and reason:

details = client.fetch_boolean_details(
  flag_key: 'feature-flag',
  default_value: false,
  evaluation_context: context
)

puts "Value: #{details.value}"
puts "Variant: #{details.variant}"
puts "Reason: #{details.reason}"

Rails Integration

# config/initializers/flipswitch.rb
require 'openfeature/sdk'
require 'openfeature/contrib/providers/ofrep'

provider = OpenFeature::Contrib::Providers::OFREP.new(
  base_url: ENV.fetch('FLIPSWITCH_URL', 'https://api.flipswitch.io'),
  headers: { 'X-API-Key' => ENV.fetch('FLIPSWITCH_API_KEY') }
)

OpenFeature::SDK.configure do |config|
  config.set_provider(provider)
end

Use it in a controller:

# app/controllers/features_controller.rb
class FeaturesController < ApplicationController
  def show
    client = OpenFeature::SDK.build_client

    context = OpenFeature::SDK::EvaluationContext.new(
      targeting_key: current_user.id.to_s,
      plan: current_user.plan
    )

    enabled = client.fetch_boolean_value(
      flag_key: 'new-feature',
      default_value: false,
      evaluation_context: context
    )

    render json: { feature_enabled: enabled }
  end
end

Direct OFREP API

You can also call the OFREP endpoints directly via HTTP — see the OFREP protocol reference for details.

On this page