Class: HTTPSpec::Clients::VCRProxy::Recording

Inherits:
Object
  • Object
show all
Defined in:
lib/http_spec/clients/vcr_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(cassette, dir) ⇒ Recording

Returns a new instance of Recording.



23
24
25
26
# File 'lib/http_spec/clients/vcr_proxy.rb', line 23

def initialize(cassette, dir)
  filename = Digest::SHA1.hexdigest(cassette)
  @filepath = File.join(dir, filename)
end

Instance Method Details

#cacheObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/http_spec/clients/vcr_proxy.rb', line 48

def cache
  @cache ||=
    if File.exists?(@filepath)
      File.open(@filepath, "r+") do |file|
        Marshal.load(file)
      end
    else
      []
    end
end

#new?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/http_spec/clients/vcr_proxy.rb', line 28

def new?
  @new ||= !File.exists?(@filepath)
end

#play(request) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/http_spec/clients/vcr_proxy.rb', line 39

def play(request)
  next_request, next_response = cache.shift
  if next_request == request
    next_response
  else
    raise "Request does not match recording."
  end
end

#record(request, response) ⇒ Object



32
33
34
35
36
37
# File 'lib/http_spec/clients/vcr_proxy.rb', line 32

def record(request, response)
  cache << [request, response]
  File.open(@filepath, "w+") do |file|
    Marshal.dump(@cache, file)
  end
end