module VCR::Cassette::Persisters::FileSystem

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

Attributes

storage_location[R]

@private

Public Instance Methods

[](file_name) click to toggle source

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

@param [String] file_name the file name @return [String] the cassette content

# 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) click to toggle source

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

@param [String] file_name the file name @param [String] content the content to store

# 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) click to toggle source

@private

# 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
storage_location=(dir) click to toggle source

@private

# File lib/vcr/cassette/persisters/file_system.rb, line 14
def storage_location=(dir)
  FileUtils.mkdir_p(dir) if dir
  @storage_location = dir ? absolute_path_for(dir) : nil
end

Private Instance Methods

absolute_path_for(path) click to toggle source
# File lib/vcr/cassette/persisters/file_system.rb, line 48
def absolute_path_for(path)
  Dir.chdir(path) { Dir.pwd }
end
sanitized_file_name_from(file_name) click to toggle source
# File lib/vcr/cassette/persisters/file_system.rb, line 52
def sanitized_file_name_from(file_name)
  parts = file_name.to_s.split('.')
  file_extension = '.' + parts.pop if parts.size > 1
  parts.join('.').gsub(/[^\w\-\/]+/, '_') + file_extension.to_s
end