Ruby Integration Guide
Add AI safety guardrails to your Ruby applications using the official tork-governance gem. Includes Rails, Sinatra, and standalone examples.
Prerequisites
- Ruby 2.7 or higher (3.0+ recommended)
- A Tork API key (get one here)
- Bundler for dependency management
Installation
Install the gem using Bundler or directly with gem.
Never commit your API key. Add .env to .gitignore and use dotenv gem to load environment variables.
Configuration
Configure the SDK globally or per-client.
Basic Usage
Evaluate content with a few lines of code.
Content Evaluation
Different ways to evaluate content for safety.
PII Detection & Redaction
Detect and redact personally identifiable information.
Jailbreak Detection
Detect prompt injection and jailbreak attempts.
Policy Management
Create, update, and manage content policies.
Metrics & Analytics
Access TORKING-X scores, usage statistics, and more.
Batch Evaluation
Evaluate multiple items in a single request.
RAG Validation
Validate retrieval-augmented generation responses.
Error Handling
Handle API errors gracefully with specific exception types.
Exception Types
| Tork::AuthenticationError | Invalid or missing API key |
| Tork::RateLimitError | Rate limit exceeded (check retry_after) |
| Tork::ValidationError | Invalid request parameters |
| Tork::PolicyViolationError | Content blocked by policy |
| Tork::NotFoundError | Resource not found |
| Tork::ServerError | Server error (retry with backoff) |
| Tork::TimeoutError | Request timed out |
| Tork::ConnectionError | Network connection failed |
Rails Integration
Ready-to-use examples for Ruby on Rails applications
Sinatra Integration
Protect your Sinatra API endpoints with Tork.
Thread Safety
The SDK is thread-safe with connection pooling.
Best Practices
Use global configuration
Configure Tork once in an initializer for Rails, or at application startup for other frameworks.
Reuse client instances
Create one Tork::Client per application and reuse it. It's thread-safe with connection pooling.
Handle rate limits gracefully
Catch RateLimitError and use the retry_after attribute to implement proper backoff.
Use background jobs for bulk operations
For high-volume content moderation, use Sidekiq or ActiveJob to process asynchronously.
Log request IDs
Always log the request_id from responses for debugging and support inquiries.
Evaluate both input and output
Check user messages before sending to AI, and AI responses before showing to users.
Automatic Retry Behavior
The SDK automatically retries failed requests with exponential backoff. Retryable status codes: 408, 500, 502, 503, 504. Default: 3 retries with 0.5s base delay and 2x backoff factor. Configure via max_retries and retry_base_delay.
Next Steps
Explore more endpoints for jailbreak detection, RAG validation, and multi-agent orchestration.