← All languages

Servirtium for

Ruby

No shipped package: vendor the binding's source at a release tag, with the native library from the release. Release v2.0.0-alpha.20.

FFI mechanism
Fiddle. A pure-Ruby gem that loads the native library at run time through Ruby's Fiddle (stdlib). No compiler needed.
Toolchain
Ruby 3.3+ (servirtium.gemspec required_ruby_version), with Fiddle; add gem 'fiddle' to your Gemfile on Ruby 3.4+ (a LoadError under Bundler on Ruby 4)
Distribution
No RubyGems package: Bundler git source on the release tag (glob: 'ruby/*.gemspec'), plus the release native library
Finding the library
SERVIRTIUM_VCR_LIB (absolute path), or native_lib: on Servirtium.playback / Servirtium.record. Set it in whatever process runs the tests.
On GitHub
ruby/ · README

Getting it

There is no published package for Ruby. You vendor the binding's source into your project at the release tag v2.0.0-alpha.20 and take the native library from the same release.

  1. Fetch the native library for your OS/arch from the release into a directory outside your git tree and verify the checksum.
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/v2.0.0-alpha.20
    LIBDIR="$HOME/.servirtium-vcr/v2.0.0-alpha.20"; mkdir -p "$LIBDIR"
    LIB=libservirtium_vcr-v2.0.0-alpha.20-macos-arm64.dylib      # or -linux-x86_64.so, -linux-arm64.so, ...
    curl -fsSL -o "$LIBDIR/$LIB" $REL/$LIB
    curl -fsSL -o "$LIBDIR/$LIB.sha256" $REL/$LIB.sha256
    ( cd "$LIBDIR" && { sha256sum -c $LIB.sha256 || shasum -a 256 -c $LIB.sha256; } )
    
  2. Add the gem to your Gemfile as a git source on the tag. glob: is needed because the gemspec lives in the ruby/ subdirectory; the gem does not declare fiddle itself, so list it.
    gem 'fiddle'
    gem 'servirtium', git: 'https://github.com/servirtium/servirtium-vcr.git', tag: 'v2.0.0-alpha.20', glob: 'ruby/*.gemspec'
    
  3. Install, and point the gem at the library wherever your tests run. If your gem directory is not writable use a local path (bundle config set --local path vendor/bundle); never sudo bundle.
    bundle install
    export SERVIRTIUM_VCR_LIB="$HOME/.servirtium-vcr/v2.0.0-alpha.20/libservirtium_vcr-v2.0.0-alpha.20-macos-arm64.dylib"
    

Record

Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.

require 'net/http'
require 'servirtium'

Servirtium.record('spec/tapes/climate_api.md', 'https://climatedataapi.worldbank.org')
          .port(0)
          .start do |server|
  Net::HTTP.get_response(URI.join(server.base_url, '/api/v1/countries'))
  server.commit   # the only thing that writes the tape
end
# Leaving the block WITHOUT commit (or discard) writes nothing and raises.
# A block that raises discards the recording and re-raises your error.

Playback

The same tape, replayed with no network.

require 'net/http'
require 'servirtium'

Servirtium.playback('spec/tapes/climate_api.md').port(0).start do |server|
  Net::HTTP.get_response(URI.join(server.base_url, '/api/v1/countries'))
  raise server.last_error unless server.last_kind == :ok   # optional: assert a clean match
end

Idiomatic extras

A raising block discards the recording

The block form of start discards a recording when the block raises, so a failed run never mints a tape.

begin
  Servirtium.record('x.md', upstream).port(0).start do |s|
    Net::HTTP.get(URI(s.base_url + '/x'))
    raise 'boom'                 # -> no x.md
  end
rescue RuntimeError
end

Redact credentials and JSON fields at record time

Applied when the tape is written; the live test still sees real bytes.

Servirtium.record(tape, upstream)
          .redact_credentials
          .redact_json_field('api_key', '[REDACTED]', field: Servirtium::Field::RESPONSE_BODY)
          .port(0)
          .start do |s|
  # drive the SUT
  s.commit
end

Drift detection

With fail_if_changed, commit writes the fresh tape and raises if it differs from the one on disk.

Servirtium.record(tape, upstream).fail_if_changed.port(0).start do |s|
  # drive the SUT
  s.commit   # raises on drift
end

Gotchas

  • Use tag: (or ref:), not branch: 'main'; the library must be at least as new as the Ruby source.
  • Permission denied @ dir_s_mkdir during bundle install means a root-owned system gem directory: use a local path or a Ruby manager, never sudo bundle.
  • Without git: in the Gemfile Bundler says 'Could not find gem servirtium'; without glob: it says 'There are no gemspecs'.
  • Ruby 3.4 warns that fiddle will leave the default gems; on Ruby 4 it is a LoadError under Bundler unless the Gemfile lists it.
  • Windows: take the .dll asset and give SERVIRTIUM_VCR_LIB its full path.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your Ruby project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.

Hey Gemini or Claude or Codex,

I want a test in this Ruby project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.

1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.

2. Add the Servirtium binding for Ruby. Follow https://servirtium.dev/languages/ruby/ exactly. Servirtium ships no package for this language. Vendor the binding's source into the project at the release tag v2.0.0-alpha.20 and put the native library from the same release where the page says: No RubyGems package: Bundler git source on the release tag (glob: 'ruby/*.gemspec'), plus the release native library.
   Toolchain and minimum version: Ruby 3.3+ (servirtium.gemspec required_ruby_version), with Fiddle; add gem 'fiddle' to your Gemfile on Ruby 3.4+ (a LoadError under Bundler on Ruby 4).
   How the binding finds the native library: SERVIRTIUM_VCR_LIB (absolute path), or native_lib: on Servirtium.playback / Servirtium.record. Set it in whatever process runs the tests.

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Ruby way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.

4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for Ruby is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.

5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.

6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.

7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.

Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- Use tag: (or ref:), not branch: 'main'; the library must be at least as new as the Ruby source.
- Permission denied @ dir_s_mkdir during bundle install means a root-owned system gem directory: use a local path or a Ruby manager, never sudo bundle.
- Without git: in the Gemfile Bundler says 'Could not find gem servirtium'; without glob: it says 'There are no gemspecs'.
- Ruby 3.4 warns that fiddle will leave the default gems; on Ruby 4 it is a LoadError under Bundler unless the Gemfile lists it.
- Windows: take the .dll asset and give SERVIRTIUM_VCR_LIB its full path.

References:
- This language's page: https://servirtium.dev/languages/ruby/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/ruby
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)

How these snippets were checked

Real run on macOS arm64 (Ruby 3.3.14, Bundler 4.1.0): Gemfile with gem 'fiddle' and the git/tag/glob line resolved and installed v2.0.0-alpha.20 into vendor/bundle; release macos-arm64 dylib via SERVIRTIUM_VCR_LIB (sha256 verified; loads unmodified under Fiddle). Ran the README 'Getting it' playback check (ok-body, clean match); the full README 'Record and replay a local service' script (records hello.md with commit, replays with the upstream stopped); a raising block (no tape written); redact_credentials + redact_json_field (secret and bearer token absent, [REDACTED] present). fail_if_changed extra checked against vcr.rb, not run.

They mirror these files in the repository:

See also the feature reference, llms.txt and the ruby/ directory.