Class: RspecMiddlewares::VCR

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-middlewares/vcr.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, cassette_name: nil, record:, match_requests_on:) ⇒ VCR

Returns a new instance of VCR.



3
4
5
6
7
8
# File 'lib/rspec-middlewares/vcr.rb', line 3

def initialize(app, cassette_name: nil, record:, match_requests_on:)
  @app = app
  @cassette_name = cassette_name
  @record = record || :once
  @match_requests_on = match_requests_on || [:method, :uri, :body]
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
# File 'lib/rspec-middlewares/vcr.rb', line 10

def call(env)
  ::VCR.use_cassette((@cassette_name || derived_cassette_name(env)), record: @record, match_requests_on: @match_requests_on) do
    @app.call env
  end
end

#derived_cassette_name(env) ⇒ Object



16
17
18
19
20
21
# File 'lib/rspec-middlewares/vcr.rb', line 16

def derived_cassette_name(env)
  directory = env[:test].file_path.gsub("_spec.rb", "").gsub("./spec/", "") + "/"
  filename = env[:test].full_description.underscore.downcase.gsub(/[^\w]/, "_")

  directory + filename
end