Module: VCR::Archive::Persister

Extended by:
Persister
Included in:
Persister
Defined in:
lib/vcr/archive.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#storage_locationObject

Returns the value of attribute storage_location.



27
28
29
# File 'lib/vcr/archive.rb', line 27

def storage_location
  @storage_location
end

Instance Method Details

#[](file_name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



62
63
64
65
# 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