class VCR::Middleware::Excon::RequestHandler

Handles a single Excon request.

@private

Attributes

request_params[R]
response_body_reader[R]

Public Class Methods

new() click to toggle source
# File lib/vcr/middleware/excon.rb, line 55
def initialize
  @request_params       = nil
  @response_body_reader = nil
  @should_record        = false
end

Public Instance Methods

after_request(response) click to toggle source

Performs after_request processing based on the provided response.

@private

# File lib/vcr/middleware/excon.rb, line 74
def after_request(response)
  vcr_response = vcr_response_for(response)

  if vcr_response && should_record?
    VCR.record_http_interaction(VCR::HTTPInteraction.new(vcr_request, vcr_response))
  end

  invoke_after_request_hook(vcr_response)
end
before_request(request_params) click to toggle source

Performs before_request processing based on the provided request_params.

@private

# File lib/vcr/middleware/excon.rb, line 65
def before_request(request_params)
  @request_params       = request_params
  @response_body_reader = create_response_body_reader
  handle
end
ensure_response_body_can_be_read_for_error_case() click to toggle source
# File lib/vcr/middleware/excon.rb, line 84
def ensure_response_body_can_be_read_for_error_case
  # Excon does not invoke the `:response_block` when an error
  # has occurred, so we need to be sure to use the non-streaming
  # body reader.
  @response_body_reader = NonStreamingResponseBodyReader
end

Private Instance Methods

create_response_body_reader() click to toggle source
# File lib/vcr/middleware/excon.rb, line 115
def create_response_body_reader
  block = request_params[:response_block]
  return NonStreamingResponseBodyReader unless block

  StreamingResponseBodyReader.new(block).tap do |response_block_wrapper|
    request_params[:response_block] = response_block_wrapper
  end
end
externally_stubbed?() click to toggle source
# File lib/vcr/middleware/excon.rb, line 95
def externally_stubbed?
  !!::Excon.stub_for(request_params)
end
normalized_headers(headers) click to toggle source
# File lib/vcr/middleware/excon.rb, line 148
def normalized_headers(headers)
  normalized = {}
  headers.each do |k, v|
    v = v.join(', ') if v.respond_to?(:join)
    normalized[k] = v
  end
  normalized
end
on_recordable_request() click to toggle source
# File lib/vcr/middleware/excon.rb, line 111
def on_recordable_request
  @should_record = true
end
on_stubbed_by_vcr_request() click to toggle source
# File lib/vcr/middleware/excon.rb, line 103
def on_stubbed_by_vcr_request
  request_params[:response] = {
    :body     => stubbed_response.body.dup, # Excon mutates the body, so we must dup it :-(
    :headers  => normalized_headers(stubbed_response.headers || {}),
    :status   => stubbed_response.status.code
  }
end
should_record?() click to toggle source
# File lib/vcr/middleware/excon.rb, line 99
def should_record?
  @should_record
end
uri() click to toggle source
# File lib/vcr/middleware/excon.rb, line 158
def uri
  @uri ||= "#{::Excon::Utils.request_uri(request_params)}"
end
vcr_request() click to toggle source
# File lib/vcr/middleware/excon.rb, line 124
def vcr_request
  @vcr_request ||= begin
    headers = request_params[:headers].dup
    headers.delete("Host")

    VCR::Request.new \
      request_params[:method],
      uri,
      request_params[:body],
      headers
  end
end
vcr_response_for(response) click to toggle source
# File lib/vcr/middleware/excon.rb, line 137
def vcr_response_for(response)
  return nil if response.nil?

  VCR::Response.new(
    VCR::ResponseStatus.new(response.fetch(:status), nil),
    response.fetch(:headers),
    response_body_reader.read_body_from(response),
    nil
  )
end