Async::HTTP::CaptureSourceAsyncHTTPCaptureCassetteStore

class CassetteStore

Store implementation that saves interactions to timestamped files in a directory.

Each interaction is saved as a separate JSON file named by timestamp, using atomic write + move operations for parallel-safe recording.

Definitions

def initialize(directory_path)

Initialize the cassette store.

Signature

parameter directory_path String

The directory path where interactions should be saved.

Implementation

def initialize(directory_path)
	@directory_path = directory_path
	@suffix = "#{Process.pid}-#{self.object_id}"
end

def cassette

Signature

returns Cassette

A cassette object representing the recorded interactions.

Implementation

def cassette
	Cassette.load(@directory_path)
end

def call(interaction)

Save an interaction to a timestamped file using PID and object ID for uniqueness.

Signature

parameter interaction Interaction

The interaction to save.

Implementation

def call(interaction)
	FileUtils.mkdir_p(@directory_path)
	
	# Create filename with timestamp, PID, and object ID for complete uniqueness:
	timestamp = Time.now.strftime("%Y%m%d-%H%M%S-%6N")  # Include microseconds
	filename = "#{timestamp}-#{@suffix}.json"
	file_path = File.join(@directory_path, filename)
	
	File.write(file_path, JSON.pretty_generate(interaction.serialize))
end