class VCR::LibraryHooks::Excon::RequestHandler

Constants

PARAMS_TO_DELETE

Attributes

params[R]

Public Class Methods

new(params) click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 13
def initialize(params)
  @vcr_response = nil
  @params = params
end

Public Instance Methods

handle() click to toggle source
Calls superclass method VCR::RequestHandler#handle
# File lib/vcr/library_hooks/excon.rb, line 18
def handle
  super
ensure
  invoke_after_request_hook(@vcr_response)
end

Private Instance Methods

http_interaction_for(response) click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 123
def http_interaction_for(response)
  VCR::HTTPInteraction.new              vcr_request,
    vcr_response_from(response)
end
new_connection() click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 61
def new_connection
  # Ensure the connection is constructed with the exact same args
  # that the orginal connection was constructed with.
  args, options = params.fetch(:__construction_args)
  options = scrub_params_from(options) if options.is_a?(Hash)
  ::Excon::Connection.new(*[args, options].compact)
end
normalized_headers(headers) click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 150
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_ignored_request() click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 35
def on_ignored_request
  perform_real_request
end
on_recordable_request() click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 89
def on_recordable_request
  perform_real_request do |response|
    http_interaction = http_interaction_for(response)
    VCR.record_http_interaction(http_interaction)
  end
end
on_stubbed_by_vcr_request() click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 26
def on_stubbed_by_vcr_request
  @vcr_response = stubbed_response
  {
    :body     => stubbed_response.body,
    :headers  => normalized_headers(stubbed_response.headers || {}),
    :status   => stubbed_response.status.code
  }
end
perform_real_request() { |response| ... } click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 75
def perform_real_request
  begin
    response = new_connection.request(real_request_params)
  rescue ::Excon::Errors::Error => excon_error
    response = response_from_excon_error(excon_error)
  end

  @vcr_response = vcr_response_from(response)
  yield response if block_given?
  raise excon_error if excon_error

  response.attributes
end
query() click to toggle source

based on: github.com/geemus/excon/blob/v0.7.8/lib/excon/connection.rb#L117-132

# File lib/vcr/library_hooks/excon.rb, line 102
def query
  @query ||= case params[:query]
    when String
      "?#{params[:query]}"
    when Hash
      qry = '?'
      for key, values in params[:query]
        if values.nil?
          qry << key.to_s << '&'
        else
          for value in [*values]
            qry << key.to_s << '=' << CGI.escape(value.to_s) << '&'
          end
        end
      end
      qry.chop! # remove trailing '&'
    else
      ''
  end
end
real_request_params() click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 53
def real_request_params
  # Excon supports a variety of options that affect how it handles failure
  # and retry; we don't want to use any options here--we just want to get
  # a raw response, and then the main request (with :mock => true) can
  # handle failure/retry on its own with its set options.
  scrub_params_from params.merge(:mock => false, :retry_limit => 0)
end
response_from_excon_error(error) click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 39
def response_from_excon_error(error)
  if error.respond_to?(:response)
    error.response
  elsif error.respond_to?(:socket_error)
    response_from_excon_error(error.socket_error)
  else
    warn "WARNING: VCR could not extract a response from Excon error (#{error.inspect})"
  end
end
scrub_params_from(hash) click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 69
def scrub_params_from(hash)
  hash = hash.dup
  PARAMS_TO_DELETE.each { |key| hash.delete(key) }
  hash
end
uri() click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 96
def uri
  @uri ||= "#{params[:scheme]}://#{params[:host]}:#{params[:port]}#{params[:path]}#{query}"
end
vcr_request() click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 129
def vcr_request
  @vcr_request ||= begin
    headers = params[:headers].dup
    headers.delete("Host")

    VCR::Request.new                params[:method],
      uri,
      params[:body],
      headers
  end
end
vcr_response_from(response) click to toggle source
# File lib/vcr/library_hooks/excon.rb, line 142
def vcr_response_from(response)
  VCR::Response.new              VCR::ResponseStatus.new(response.status, nil),
    response.headers,
    response.body,
    nil
end