Ruby
Installation and Setup
- Install the Ruby gem:
Or add it to your Gemfile:
Then run:
-
Issue an API key for the appropriate environment using the Schematic app. Be sure to capture the secret key when you issue the API key; you’ll only see this key once, and this is what you’ll use with schematic-ruby.
-
Using this secret key, initialize a client in your application:
By default, the client will do some local caching for flag checks. If you would like to change this behavior, you can do so using an initialization option to specify the max size of the cache (in terms of number of records) and the max age of the cache (in seconds):
You can also disable local caching entirely with an initialization option; bear in mind that, in this case, every flag check will result in a network request:
You may want to specify default flag values for your application, which will be used if there is a service interruption or if the client is running in offline mode (see below). You can do this using an initialization option:
Custom Logging
You can provide your own logger implementation to control how the Schematic client logs messages. The logger must implement the Schematic::Logger module with error, warn, info, and debug methods:
You can also adjust the log level of the built-in console logger:
If no logger is provided, the client will use a default console logger at the :info level that outputs to stderr.
Usage Examples
A number of these examples use keys to identify companies and users. Learn more about keys here.
Sending identify events
Create or update users and companies using identify events.
This call is non-blocking and there is no response to check.
Sending track events
Track activity in your application using track events; these events can later be used to produce metrics for targeting.
This call is non-blocking and there is no response to check.
If you want to record large numbers of the same event at once, or perhaps measure usage in terms of a unit like tokens or memory, you can optionally specify a quantity for your event:
Creating and updating companies
Although it is faster to create companies and users via identify events, if you need to handle a response, you can use the companies API to upsert companies. Because you use your own identifiers to identify companies, rather than a Schematic company ID, creating and updating companies are both done via the same upsert operation:
You can define any number of company keys; these are used to address the company in the future, for example by updating the company’s traits or checking a flag for the company.
You can also define any number of company traits; these can then be used as targeting parameters.
Creating and updating users
Similarly, you can upsert users using the Schematic API, as an alternative to using identify events. Because you use your own identifiers to identify users, rather than a Schematic user ID, creating and updating users are both done via the same upsert operation:
You can define any number of user keys; these are used to address the user in the future, for example by updating the user’s traits or checking a flag for the user.
You can also define any number of user traits; these can then be used as targeting parameters.
Checking flags
When checking a flag, you’ll provide keys for a company and/or keys for a user. You can also provide no keys at all, in which case you’ll get the default value for the flag.
Checking flags with entitlement details
If you need more detail about how a flag check was resolved, including any entitlement associated with the check, use check_flag_with_entitlement. This returns a response object with the flag value, the reason for the evaluation result, and entitlement details such as usage, allocation, and credit balances when applicable.
Other API operations
The Schematic API supports many operations beyond these, accessible via the API modules on the client: accounts, billing, companies, credits, entitlements, events, features, and plans.
Webhook Verification
Schematic can send webhooks to notify your application of events. To ensure the security of these webhooks, Schematic signs each request using HMAC-SHA256. The SDK provides utility functions to verify these signatures.
Verifying Webhook Signatures
When your application receives a webhook request from Schematic, you should verify its signature to ensure it’s authentic:
Computing Signatures
You can compute a hex-encoded signature for a given payload:
Rack / Rails Integration
The SDK can verify webhook signatures from Rack-compatible request objects (including Rails controllers):
For a Sinatra application:
Webhook Test Server
The SDK includes a standalone webhook test server for local development:
This starts a WEBrick server on port 8080 (configurable via PORT env var) that listens for POST requests at /webhook, verifies signatures, and prints the payload.
DataStream
DataStream enables local flag evaluation by maintaining a WebSocket connection to Schematic and caching flag rules, company, and user data locally. Flag checks are evaluated using a local WASM rules engine, significantly reducing latency and network calls.
Setup
Configuration Options
You can customize DataStream behavior via the datastream_options hash:
Replicator Mode
When running the schematic-datastream-replicator service, you can configure the Schematic client to operate in Replicator Mode. In this mode, the client reads from a shared cache populated by the external replicator rather than establishing its own WebSocket connection.
Setup
When running in Replicator Mode, the client will:
- Skip establishing WebSocket connections
- Periodically check if the replicator service is ready
- Use cached data populated by the external replicator service
- Fall back to direct API calls if the replicator is not available
Testing
Offline Mode
In development or testing environments, you may want to avoid making network requests to the Schematic API. You can run Schematic in offline mode by specifying the offline option; in this case, it does not matter what API key you specify:
You can also enable offline mode by not providing an API key:
Offline mode works well with flag defaults:
In an automated testing context, you may also want to use offline mode and specify single flag responses for test cases:
Errors
Failed API calls will raise errors that can be rescued from granularly:
Note that check_flag and check_flag_with_entitlement never raise exceptions — errors are logged and default values are returned.
Advanced
Retries
The SDK is instrumented with automatic retries. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).
A request is deemed retryable when any of the following HTTP status codes is returned:
Use the max_retries option to configure this behavior:
Timeouts
The SDK defaults to a 60 second timeout. Use the timeout option to configure this behavior: