Skip to main content

Ruby SDK Reference

This page is a comprehensive single-language reference for the Scope Ruby SDK. It covers installation, authentication, all client methods, prompt version properties, error handling, caching, and telemetry.

For concept-oriented guides with side-by-side Python/Ruby examples, see the tabbed pages in the SDK section.

Installation

Requirements: Ruby 2.7+

Add to your Gemfile:

gem "scope_client",
git: "https://github.com/base14/scope-sdk.git",
glob: "sdks/ruby/*.gemspec"

Then run:

bundle install

Verify:

require "scope_client"
puts ScopeClient::VERSION

Authentication

Environment Variables

Set the following environment variables:

VariableRequiredDescription
SCOPE_ORG_IDYesYour organization/tenant ID
SCOPE_API_KEYYesAPI key identifier
SCOPE_API_SECRETYesAPI key secret
SCOPE_API_URLYesScope API base URL
SCOPE_AUTH_API_URLYesAuth service URL
SCOPE_ENVIRONMENTNoEnvironment name (default: production)
SCOPE_TOKEN_REFRESH_BUFFERNoSeconds before expiry to refresh token (default: 60)

Create Credentials

require "scope_client"

# From environment variables
credentials = ScopeClient::Credentials::ApiKey.from_env

# Or explicitly
credentials = ScopeClient::Credentials::ApiKey.new(
org_id: "org_01ABC",
api_key: "key_01XYZ",
api_secret: "secret_01DEF",
api_url: "https://scope.example.com/api/v1",
auth_api_url: "https://auth.example.com"
)

Create a Client

# Basic
client = ScopeClient::Client.new(credentials: credentials)

# With options
client = ScopeClient::Client.new(
credentials: credentials,
timeout: 30,
cache_enabled: true,
cache_ttl: 600,
max_retries: 3,
telemetry_enabled: true
)

Global Configuration

ScopeClient.configure do |config|
config.credentials = credentials
config.cache_ttl = 600
config.max_retries = 5
end

# Use the global client
client = ScopeClient.client
version = client.get_prompt_version("greeting")

Client Configuration Options

OptionTypeDefaultDescription
credentialsCredentials::ApiKeyRequired. Authentication credentials
base_urlStringFrom envScope API base URL
auth_api_urlStringFrom envAuth service URL
api_versionString"v1"API version
timeoutInteger30Request timeout in seconds
open_timeoutInteger10Connection timeout in seconds
cache_enabledBooleantrueEnable in-memory TTL cache
cache_ttlInteger300Cache TTL in seconds
max_retriesInteger3Max retry attempts
retry_base_delayFloat0.5Initial retry delay in seconds
retry_max_delayFloat30.0Maximum retry delay in seconds
telemetry_enabledBooleantrueEnable telemetry hooks
environmentString"production"Environment name
token_refresh_bufferInteger60Seconds before token expiry to refresh

Methods

get_prompt_version(name, label: nil, version: nil, cache: true, cache_ttl: nil)

Fetches a prompt version from the Scope API.

Parameters:

ParameterTypeDefaultDescription
nameStringPrompt name (required)
labelStringnil"production" or "latest"
versionStringnilSpecific version ID (e.g., "v_01ABC")
cacheBooleantrueUse cache for this request
cache_ttlIntegernilOverride cache TTL for this request

Returns: PromptVersion

Behavior:

  • No label or version → fetches the production version
  • label: "production" → fetches the production version
  • label: "latest" → fetches the most recent version (any status)
  • version: "v_01ABC" → fetches a specific version by ID
# Production version (default)
version = client.get_prompt_version("greeting")

# Latest version
version = client.get_prompt_version("greeting", label: "latest")

# Specific version
version = client.get_prompt_version("greeting", version: "v_01ABC123")

# Skip cache
version = client.get_prompt_version("greeting", cache: false)

# Custom cache TTL
version = client.get_prompt_version("greeting", cache_ttl: 60)

render_prompt(name, variables, label: nil, version: nil, cache: true, cache_ttl: nil)

Fetches a prompt version and renders it with variables in a single call.

Parameters:

ParameterTypeDefaultDescription
nameStringPrompt name (required)
variablesHashVariable values as key-value pairs (required)
labelStringnil"production" or "latest"
versionStringnilSpecific version ID
cacheBooleantrueUse cache for this request
cache_ttlIntegernilOverride cache TTL

Returns: String (rendered prompt content)

rendered = client.render_prompt(
"greeting",
{ name: "Alice", app: "Scope" },
label: "production"
)

clear_cache

Clears all cached prompt versions.

client.clear_cache

PromptVersion Properties

PropertyTypeDescription
idStringVersion ID (e.g., "v_01ABC")
prompt_idStringParent prompt ID
version_numberIntegerSequential version number
contentStringRaw template content with {{variable}} placeholders
variablesArray<String>Detected variable names
statusString"draft", "published", or "archived"
production?BooleanWhether this is the production version
typeStringPrompt type ("text" or "chat")
metadataHashArbitrary key-value metadata
created_atTimeCreation timestamp
updated_atTimeLast update timestamp

Status Helpers

version.draft?       # true if status == "draft"
version.published? # true if status == "published"
version.archived? # true if status == "archived"
version.production? # true if this is the active production version

Metadata Access

model = version.get_metadata("model")                     # Returns nil if missing
model = version.get_metadata("model", default: "gpt-4o") # Returns default if missing

PromptVersion Methods

render(**variables)

Renders the template by substituting {{variable}} placeholders.

version = client.get_prompt_version("greeting")
rendered = version.render(name: "Alice", app: "Scope")

Raises MissingVariableError if any required variables are not provided.

Error Handling

Error Hierarchy

ScopeClient::Error
├── ConfigurationError
│ └── MissingApiKeyError
├── ApiError
│ ├── AuthenticationError
│ ├── AuthorizationError
│ ├── NotFoundError
│ ├── ConflictError
│ ├── RateLimitError
│ └── ServerError
├── ConnectionError
│ └── TimeoutError
└── ResourceError
├── ValidationError
├── RenderError
│ └── MissingVariableError
└── NoProductionVersionError

Common Patterns

begin
version = client.get_prompt_version("greeting")
rendered = version.render(name: "Alice")
rescue ScopeClient::AuthenticationError
# Invalid or expired credentials
rescue ScopeClient::NotFoundError
# Prompt not found
rescue ScopeClient::NoProductionVersionError
# No version promoted to production
rescue ScopeClient::RateLimitError => e
# Rate limited — check e.retry_after
rescue ScopeClient::MissingVariableError => e
# Missing template variables
puts "Missing: #{e.missing_variables}"
rescue ScopeClient::Error
# Catch-all for any SDK error
end

ApiError Properties

begin
version = client.get_prompt_version("greeting")
rescue ScopeClient::ApiError => e
puts e.message # Error description
puts e.http_status # HTTP status code (e.g., 404)
puts e.error_code # API error code
puts e.request_id # Request ID for support
end

Caching

The SDK caches prompt versions in memory using a TTL cache.

# Default: cache enabled, 300s TTL
client = ScopeClient::Client.new(credentials: credentials)

# Custom TTL
client = ScopeClient::Client.new(credentials: credentials, cache_ttl: 600)

# Disable caching entirely
client = ScopeClient::Client.new(credentials: credentials, cache_enabled: false)

# Per-request cache control
version = client.get_prompt_version("greeting", cache: false) # Skip cache
version = client.get_prompt_version("greeting", cache_ttl: 60) # Short TTL

# Clear cache
client.clear_cache

Cache keys follow the pattern: prompt:{name}:{label|version}.

Telemetry

Register hooks to observe SDK HTTP activity via Faraday middleware.

# Request hook
ScopeClient::Middleware::Telemetry.on_request = ->(info) {
puts "→ #{info.method} #{info.url}"
}

# Response hook
ScopeClient::Middleware::Telemetry.on_response = ->(info) {
puts "← #{info.status} in #{info.duration}ms"
}

# Error hook
ScopeClient::Middleware::Telemetry.on_error = ->(info) {
puts "✗ #{info.error}"
}

# Clear hooks
ScopeClient::Middleware::Telemetry.on_request = nil
ScopeClient::Middleware::Telemetry.on_response = nil
ScopeClient::Middleware::Telemetry.on_error = nil

Hook Data

HookFields
RequestInforequest_id, method, url, headers
ResponseInforequest_id, status, headers, duration
ErrorInforequest_id, error, duration

OpenTelemetry Integration

require "opentelemetry-sdk"

tracer = OpenTelemetry.tracer_provider.tracer("scope-client")

ScopeClient::Middleware::Telemetry.on_request = ->(info) {
span = tracer.start_span("scope.request")
span.set_attribute("http.method", info.method)
span.set_attribute("http.url", info.url)
}

ScopeClient::Middleware::Telemetry.on_response = ->(info) {
span = OpenTelemetry::Trace.current_span
span.set_attribute("http.status_code", info.status)
span.finish
}

End-to-End Example

require "scope_client"
require "anthropic"

# 1. Set up Scope client
credentials = ScopeClient::Credentials::ApiKey.from_env
client = ScopeClient::Client.new(credentials: credentials)

# 2. Fetch the production prompt
begin
version = client.get_prompt_version("customer-support")
rescue ScopeClient::NotFoundError
puts "Prompt not found"
exit 1
end

# 3. Render with variables
begin
rendered = version.render(
customer_name: "Alice",
issue: "billing discrepancy",
account_id: "ACC-12345"
)
rescue ScopeClient::MissingVariableError => e
puts "Missing variables: #{e.missing_variables}"
exit 1
end

# 4. Send to Anthropic
anthropic = Anthropic::Client.new
response = anthropic.messages.create(
model: "claude-sonnet-4-5-20250929",
max_tokens: 1024,
messages: [{ role: "user", content: rendered }]
)

puts response.content.first.text
Was this page helpful?