Module: VCR::Cassette::Persisters::FileSystem

Extended by:
FileSystem
Included in:
FileSystem
Defined in:
lib/vcr/cassette/persisters/file_system.rb

Overview

The only built-in cassette persister. Persists cassettes to the file system.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#storage_locationObject



11
12
13
# File 'lib/vcr/cassette/persisters/file_system.rb', line 11

def storage_location
  @storage_location
end

Instance Method Details

#[](file_name) ⇒ String

Gets the cassette for the given storage key (file name).

Parameters:

  • file_name (String)

    the file name

Returns:

  • (String)

    the cassette content



23
24
25
26
27
# File 'lib/vcr/cassette/persisters/file_system.rb', line 23

def [](file_name)
  path = absolute_path_to_file(file_name)
  return nil unless File.exist?(path)
  File.read(path)
end

#[]=(file_name, content) ⇒ Object

Sets the cassette for the given storage key (file name).

Parameters:

  • file_name (String)

    the file name

  • content (String)

    the content to store



33
34
35
36
37
38
# File 'lib/vcr/cassette/persisters/file_system.rb', line 33

def []=(file_name, content)
  path = absolute_path_to_file(file_name)
  directory = File.dirname(path)
  FileUtils.mkdir_p(directory) unless File.exist?(directory)
  File.open(path, 'w') { |f| f.write(content) }
end

#absolute_path_to_file(file_name) ⇒ Object



41
42
43
44
# File 'lib/vcr/cassette/persisters/file_system.rb', line 41

def absolute_path_to_file(file_name)
  return nil unless storage_location
  File.join(storage_location, sanitized_file_name_from(file_name))
end