class VCR::Middleware::Faraday::RequestHandler

@private

Attributes

app[R]
env[R]

Public Class Methods

new(app, env) click to toggle source
# File lib/vcr/middleware/faraday.rb, line 36
def initialize(app, env)
  @app, @env = app, env
  @has_on_complete_hook = false
end

Public Instance Methods

handle() click to toggle source
Calls superclass method VCR::RequestHandler#handle
# File lib/vcr/middleware/faraday.rb, line 41
def handle
  # Faraday must be exlusive here in case another library hook is being used.
  # We don't want double recording/double playback.
  VCR.library_hooks.exclusive_hook = :faraday
  super
ensure
  invoke_after_request_hook(response_for(env)) unless delay_finishing?
end

Private Instance Methods

delay_finishing?() click to toggle source
# File lib/vcr/middleware/faraday.rb, line 52
def delay_finishing?
  !!env[:parallel_manager] && @has_on_complete_hook
end
invoke_after_request_hook(response) click to toggle source
# File lib/vcr/middleware/faraday.rb, line 106
def invoke_after_request_hook(response)
  super
  VCR.library_hooks.exclusive_hook = nil
end
on_ignored_request() click to toggle source
# File lib/vcr/middleware/faraday.rb, line 84
def on_ignored_request
  app.call(env)
end
on_recordable_request() click to toggle source
# File lib/vcr/middleware/faraday.rb, line 98
def on_recordable_request
  @has_on_complete_hook = true
  app.call(env).on_complete do |env|
    VCR.record_http_interaction(VCR::HTTPInteraction.new(vcr_request, response_for(env)))
    invoke_after_request_hook(response_for(env)) if delay_finishing?
  end
end
on_stubbed_by_vcr_request() click to toggle source
# File lib/vcr/middleware/faraday.rb, line 88
def on_stubbed_by_vcr_request
  headers = env[:response_headers] ||= ::Faraday::Utils::Headers.new
  headers.update stubbed_response.headers if stubbed_response.headers
  env.update :status => stubbed_response.status.code, :body => stubbed_response.body

  faraday_response = ::Faraday::Response.new
  faraday_response.finish(env)
  env[:response] = faraday_response
end
raw_body_from(body) click to toggle source
# File lib/vcr/middleware/faraday.rb, line 64
def raw_body_from(body)
  return body unless body.respond_to?(:read)

  body.read.tap do |b|
    body.rewind if body.respond_to?(:rewind)
  end
end
response_for(env) click to toggle source
# File lib/vcr/middleware/faraday.rb, line 72
def response_for(env)
  response = env[:response]
  return nil unless response

  VCR::Response.new(
    VCR::ResponseStatus.new(response.status, nil),
    response.headers,
    raw_body_from(response.body),
    nil
  )
end
vcr_request() click to toggle source
# File lib/vcr/middleware/faraday.rb, line 56
def vcr_request
  @vcr_request ||= VCR::Request.new              env[:method],
    env[:url].to_s,
    raw_body_from(env[:body]),
    env[:request_headers]
end