Skip to main content

Ruby SDK

Ruby client for the beachcomber daemon. Communicates over a Unix domain socket using newline-delimited JSON. No external dependencies — stdlib only (socket, json, etc). Ruby 3.0+. Published on RubyGems.

Installation

gem install libbeachcomber

Quick start

require 'beachcomber'

client = Beachcomber::Client.new # auto-discovers socket
# client = Beachcomber::Client.new(socket_path: '/custom/path')
# client = Beachcomber::Client.new(timeout: 0.5) # 500 ms

result = client.get('git.branch', path: '/path/to/repo')
if result.hit?
puts result.data # "main"
puts result.age_ms # 42
puts result.stale? # false
end

# Hash data: full provider query
result = client.get('git', path: '/path/to/repo')
puts result['branch'] if result.hit?

Sessions

Persistent connections avoid per-call socket overhead when making multiple queries:

client.session do |s|
s.set_context('/path/to/repo') # sets default path for this connection
r1 = s.get('git.branch')
r2 = s.get('git.dirty')
s.refresh('git')
end
# connection closed automatically

API reference

Beachcomber::Client

Opens a fresh socket connection for each call. Simple and stateless.

MethodDescription
get(key, path: nil)Read a cached value. Returns a Result.
get_with_flags(key, path: nil, force: false, wait: false)Read with protocol flags. Returns a Result.
refresh(key, path: nil)Force recomputation. Returns nil.
helloHandshake. Returns a HelloInfo.
put(key, data = nil, ttl: nil, path: nil)Write a value into the cache. Returns nil.
introspect(subject, duration_secs: nil)Inspect a daemon subsystem. Returns an IntrospectResponse.
watch(key, path: nil)Subscribe to live updates. Returns a WatchStream (Enumerable).
statusDaemon cache status as typed rows. Returns Array<CacheRow>.
session { |s| }Open a persistent connection (see above).

Beachcomber::Session

MethodDescription
set_context(path)Set default path for subsequent queries.
get(key, path: nil)Read a cached value.
get_with_flags(key, path: nil, force: false, wait: false)Read with protocol flags.
refresh(key, path: nil)Force recomputation.
helloHandshake. Returns a HelloInfo.
put(key, data = nil, ttl: nil, path: nil)Write a value into the cache.
introspect(subject, duration_secs: nil)Inspect a daemon subsystem. Returns an IntrospectResponse.
statusDaemon cache status as typed rows. Returns Array<CacheRow>.
closeClose the connection (called automatically by Client#session).

Beachcomber::Result

MethodReturnsDescription
ok?BooleanDaemon reported success.
hit?BooleanSuccess and data is present.
miss?BooleanSuccess but no data (cache miss).
stale?BooleanData exists but is stale.
dataObject/nilDecoded payload (String, Hash, Array, …).
age_msIntegerAge of the cached value in milliseconds.
errorString/nilError message when ok? is false.
[](key)Object/nilDelegates to data hash. Raises TypeError if data is not a Hash.

Key format

  • "git" — full provider, returns a Hash of all fields
  • "git.branch" — single field, returns a scalar

Errors

ClassWhen raised
Beachcomber::DaemonNotRunningSocket unreachable (daemon not started).
Beachcomber::ServerErrorDaemon returns ok: false.
Beachcomber::ProtocolErrorResponse is not valid JSON or unexpected format.

All inherit from Beachcomber::Error < StandardError.

Socket discovery

  1. $BEACHCOMBER_SOCKET (if set and non-empty)
  2. $XDG_RUNTIME_DIR/beachcomber/sock (if XDG_RUNTIME_DIR is set)
  3. /tmp/beachcomber-<uid>/sock

This mirrors the daemon's bind path; $TMPDIR is not consulted.