Skip to content

Ruby SDK Guide

This guide covers adding Liteguard to a Ruby application.


Prerequisites

  • A Project Client Token from your Liteguard project. Follow the Getting Started guide through the Copy a Project Client Token step if you do not have one yet.
  • Ruby 3.1 or later.

Install the SDK

Add to your Gemfile:

ruby
gem 'liteguard'

then run:

bash
bundle install

Or install directly:

bash
gem install liteguard

Initialize the client

Create a single Liteguard::Client instance during application startup and call start to fetch the initial bundle.

ruby
require 'liteguard'

client = Liteguard::Client.new(
  ENV['LITEGUARD_TOKEN'],
  environment: ENV.fetch('LITEGUARD_ENV', 'default')
)
client.start

start fetches the bundle and launches background threads for periodic refresh and telemetry flush.

Do not commit your token to source control. Read it from environment variables or a secrets manager.

Rails initializer

ruby
# config/initializers/liteguard.rb
LITEGUARD = Liteguard::Client.new(
  ENV['LITEGUARD_TOKEN'],
  environment: Rails.env
)
LITEGUARD.start

The client is available as the LITEGUARD constant throughout the app.


Evaluate a guard

client.is_open(guard_name) reads from the locally cached bundle. No network call occurs per check.

ruby
if client.is_open('payments.checkout')
  # guarded code path
end

Passing properties

Pass a hash of properties to match against rules configured in the Liteguard UI:

ruby
if client.is_open('payments.checkout',
  user_id: current_user.id,
  plan: current_user.plan
)
  # guarded code path
end

Scopes

Create a Liteguard::Scope once per request with the caller's properties and reuse it for multiple guard checks:

ruby
scope = client.create_scope(
  user_id: current_user.id,
  plan: current_user.plan
)

if scope.is_open('payments.checkout')
  # ...
end

if scope.is_open('billing.invoice_download')
  # ...
end

Scopes are immutable. Use scope.with_properties(...) to derive a modified variant.

Block-scoped context

Use client.with_scope to attach a scope for the duration of a block:

ruby
client.with_scope(scope) do
  process_checkout
end

Calls to client.is_open inside the block use the attached scope automatically.


Shut down cleanly

Flush buffered telemetry before process exit:

ruby
at_exit { client.shutdown }

Or call it explicitly:

ruby
client.shutdown

shutdown flushes pending signals and stops background threads.


Verify in Liteguard

After calling is_open at least once, open the Guards tab and confirm your guard appears. See Your First Guard to configure its behavior.


Next steps