class VCR::LibraryHooks::Typhoeus::RequestHandler

@private

Attributes

request[R]

Public Class Methods

new(request) click to toggle source
# File lib/vcr/library_hooks/typhoeus.rb, line 12
def initialize(request)
  @request = request
  request.block_connection = false if VCR.turned_on?
end

Public Instance Methods

vcr_request() click to toggle source
# File lib/vcr/library_hooks/typhoeus.rb, line 17
def vcr_request
  @vcr_request ||= VCR::Request.new \
    request.options.fetch(:method, :get),
    request.url,
    request.encoded_body,
    request.options.fetch(:headers, {})
end

Private Instance Methods

externally_stubbed?() click to toggle source
# File lib/vcr/library_hooks/typhoeus.rb, line 27
def externally_stubbed?
  ::Typhoeus::Expectation.find_by(request)
end
on_stubbed_by_vcr_request() click to toggle source
# File lib/vcr/library_hooks/typhoeus.rb, line 41
def on_stubbed_by_vcr_request
  response = ::Typhoeus::Response.new \
    :http_version   => stubbed_response.http_version,
    :code           => stubbed_response.status.code,
    :status_message => stubbed_response.status.message,
    :headers        => stubbed_response_headers,
    :body           => stubbed_response.body,
    :effective_url  => stubbed_response.adapter_metadata.fetch('effective_url', request.url),
    :mock           => true

  first_header_line = "HTTP/#{stubbed_response.http_version} #{response.code} #{response.status_message}\r\n"
  response.instance_variable_set(:@first_header_line, first_header_line)
  response.instance_variable_get(:@options)[:response_headers] =
    first_header_line + response.headers.map { |k,v| "#{k}: #{v}"}.join("\r\n")

  response
end
on_unhandled_request() click to toggle source
# File lib/vcr/library_hooks/typhoeus.rb, line 36
def on_unhandled_request
  invoke_after_request_hook(nil)
  super
end
set_typed_request_for_after_hook(*args) click to toggle source
# File lib/vcr/library_hooks/typhoeus.rb, line 31
def set_typed_request_for_after_hook(*args)
  super
  request.instance_variable_set(:@__typed_vcr_request, @after_hook_typed_request)
end
stubbed_response_headers() click to toggle source
# File lib/vcr/library_hooks/typhoeus.rb, line 59
def stubbed_response_headers
  @stubbed_response_headers ||= {}.tap do |hash|
    stubbed_response.headers.each do |key, values|
      hash[key] = values.size == 1 ? values.first : values
    end if stubbed_response.headers
  end
end