Skip to main content

vcr_custom_persister_and_serializer

⚡️ Custom Persisters and Serializers for VCR

For enterprise use cases you might want to store cassettes in a database or use a custom serialization format (e.g., binary or encrypted JSON). Implement a custom persister and/or serializer by conforming to VCR's persister interface.

# lib/vcr/persisters/db_persister.rb
module VCR
module Persisters
class DbPersister
def initialize(opts)
@db = opts[:db_connection]
end

def [](cassette_name)
record = @db[:cassettes].where(name: cassette_name).first
record ? deserialize(record.payload) : nil
end

def []=(cassette_name, cassette_data)
payload = serialize(cassette_data)
@db[:cassettes].insert_or_replace(name: cassette_name, payload: payload)
end

def file_exists?(cassette_name)
@db[:cassettes].where(name: cassette_name).count > 0
end

private

def serialize(cassette_data)
# e.g., encrypt JSON
JSON.dump(cassette_data).reverse
end

def deserialize(payload)
JSON.parse(payload.reverse)
end
end
end
end

# spec/spec_helper.rb
VCR.configure do |c|
c.cassette_library_dir = nil # disable file system
c.hook_into :webmock
c.persister = VCR::Persisters::DbPersister.new(db_connection: DB)
end

This grants full control over where and how your HTTP interactions are persisted—ideal for multi-team environments, centralized cassette management, or integrating with custom storage backends.