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:
gem 'liteguard'then run:
bundle installOr install directly:
gem install liteguardInitialize the client
Create a single Liteguard::Client instance during application startup and call start to fetch the initial bundle.
require 'liteguard'
client = Liteguard::Client.new(
ENV['LITEGUARD_TOKEN'],
environment: ENV.fetch('LITEGUARD_ENV', 'default')
)
client.startstart 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
# config/initializers/liteguard.rb
LITEGUARD = Liteguard::Client.new(
ENV['LITEGUARD_TOKEN'],
environment: Rails.env
)
LITEGUARD.startThe 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.
if client.is_open('payments.checkout')
# guarded code path
endPassing properties
Pass a hash of properties to match against rules configured in the Liteguard UI:
if client.is_open('payments.checkout',
user_id: current_user.id,
plan: current_user.plan
)
# guarded code path
endScopes
Create a Liteguard::Scope once per request with the caller's properties and reuse it for multiple guard checks:
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')
# ...
endScopes 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:
client.with_scope(scope) do
process_checkout
endCalls to client.is_open inside the block use the attached scope automatically.
Shut down cleanly
Flush buffered telemetry before process exit:
at_exit { client.shutdown }Or call it explicitly:
client.shutdownshutdown 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.