Class: Rack::VCR

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/vcr.rb,
lib/rack/vcr/version.rb,
lib/rack/vcr/transaction.rb

Defined Under Namespace

Classes: Transaction

Constant Summary collapse

VERSION =
"0.1.6"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ VCR

Returns a new instance of VCR.



7
8
9
10
11
12
# File 'lib/rack/vcr.rb', line 7

def initialize(app, options = {})
  @app = app
  @replay = options[:replay]
  @cassette = options[:cassette]
  @record   = options[:record] || :new_episodes
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/rack/vcr.rb', line 14

def call(env)
  if @cassette
    ::VCR.use_cassette(@cassette, record: @record) do
      run_request(env)
    end
  else
    run_request(env)
  end
end

#run_request(env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rack/vcr.rb', line 24

def run_request(env)
  req = Rack::Request.new(env)
  transaction = Transaction.new(req)

  if @replay && transaction.can_replay?
    transaction.replay
  else
    status, headers, body = @app.call(env)
    res = Rack::Response.new(body, status, headers)
    transaction.capture(res)
    [status, headers, body]
  end
end