Class: VCR::Middleware::Rack

Inherits:
Object
  • Object
show all
Includes:
VariableArgsBlockCaller
Defined in:
lib/vcr/middleware/rack.rb

Overview

Note:

This will record/replay outbound HTTP requests made by your rack app.

Rack middleware that uses a VCR cassette for each incoming HTTP request.

Examples:

app = Rack::Builder.new do
  use VCR::Middleware::Rack do |cassette, env|
    cassette.name "rack/#{env['SERVER_NAME']}"
    cassette.options :record => :new_episodes
  end

  run MyRackApp
end

Instance Method Summary collapse

Methods included from VariableArgsBlockCaller

#call_block

Constructor Details

#initialize(app) {|cassette, env| ... } ⇒ Rack

Constructs a new instance of VCR's rack middleware.

Parameters:

  • app (#call)

    the rack app

Yields:

  • the cassette configuration block

Yield Parameters:

  • cassette (CassetteArguments)

    the cassette configuration object

  • env ((optional) Hash)

    the rack env hash

Raises:

  • (ArgumentError)

    if no configuration block is provided


53
54
55
56
# File 'lib/vcr/middleware/rack.rb', line 53

def initialize(app, &block)
  raise ArgumentError.new("You must provide a block to set the cassette options") unless block
  @app, @cassette_arguments_block, @mutex = app, block, Mutex.new
end

Instance Method Details

#call(env) ⇒ Array(Integer, Hash, #each)

Implements the rack middleware interface.

Parameters:

  • env (Hash)

    the rack env hash

Returns:

  • (Array(Integer, Hash, #each))

    the rack response


62
63
64
65
66
67
68
# File 'lib/vcr/middleware/rack.rb', line 62

def call(env)
  @mutex.synchronize do
    VCR.use_cassette(*cassette_arguments(env)) do
      @app.call(env)
    end
  end
end