module VCR::Archive::Persister

Attributes

storage_location[R]

Public Instance Methods

[](file_name) click to toggle source
# File lib/vcr/archive.rb, line 34
def [](file_name)
  path = absolute_path_to_file(file_name)
  files = Dir.glob("#{path}/**/*.yml")
  return nil unless files.any?
  interactions = files.map do |f|
    meta = YAML.load_file(f)
    body = File.binread(f.sub(/\.yml$/, '.html'))
    meta['response']['body']['string'] = body
    meta
  end
  {
    'http_interactions' => interactions,
  }
end
[]=(file_name, meta) click to toggle source
# File lib/vcr/archive.rb, line 49
def []=(file_name, meta)
  path = absolute_path_to_file(file_name)
  meta['http_interactions'].each do |interaction|
    uri = URI.parse(interaction['request']['uri'])
    file_path = File.join(path, uri.host, Digest::SHA1.hexdigest(uri.to_s))
    directory = File.dirname(file_path)
    FileUtils.mkdir_p(directory) unless File.exist?(directory)
    body = interaction['response']['body'].delete('string')
    File.binwrite("#{file_path}.yml", YAML.dump(interaction))
    File.binwrite("#{file_path}.html", body)
  end
end
absolute_path_to_file(file_name) click to toggle source
# File lib/vcr/archive.rb, line 62
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
# File lib/vcr/archive.rb, line 29
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/archive.rb, line 69
def absolute_path_for(path)
  Dir.chdir(path) { Dir.pwd }
end
sanitized_file_name_from(file_name) click to toggle source
# File lib/vcr/archive.rb, line 73
def sanitized_file_name_from(file_name)
  parts = file_name.to_s.split('.')

  # Get rid of the unneeded extension on the file_name
  if parts.size > 1 && parts.last == Serializer.file_extension
    parts.pop
  end

  parts.join('.').gsub(/[^\w\-\/]+/, '_')
end