class HTTPI::Adapter::Excon

HTTPI::Adapter::Excon

Adapter for the Excon client. github.com/geemus/excon

Attributes

client[R]

Public Class Methods

new(request) click to toggle source
# File lib/httpi/adapter/excon.rb, line 15
def initialize(request)
  @request = request
  @client  = ::Excon::Connection.new client_opts
end

Public Instance Methods

request(method) click to toggle source

Executes arbitrary HTTP requests. @see HTTPI.request

# File lib/httpi/adapter/excon.rb, line 24
def request(method)
  respond_with @client.send(method)
rescue ::Excon::Errors::SocketError => e
  case e.message
  when /verify certificate/
    raise SSLError
  else
    $!.extend ConnectionError
  end
  raise
end

Private Instance Methods

client_opts() click to toggle source
# File lib/httpi/adapter/excon.rb, line 38
def client_opts
  url  = @request.url
  ssl  = @request.auth.ssl

  opts = {
    :host => url.host,
    :hostname => url.hostname,
    :path => url.path,
    :port => url.port,
    :query => url.query,
    :scheme => url.scheme,
    :headers => @request.headers,
    :body => @request.body
  }

  if @request.auth.digest?
    raise NotSupportedError, "excon does not support HTTP digest authentication"
  elsif @request.auth.ntlm?
    raise NotSupportedError, "excon does not support NTLM authentication"
  end

  opts[:user], opts[:password] = *@request.auth.credentials if @request.auth.basic?
  opts[:connect_timeout] = @request.open_timeout if @request.open_timeout
  opts[:read_timeout]    = @request.read_timeout if @request.read_timeout
  opts[:write_timeout]   = @request.write_timeout if @request.write_timeout
  opts[:response_block]  = @request.on_body if @request.on_body
  opts[:proxy]           = @request.proxy if @request.proxy

  case ssl.verify_mode
  when :peer
    opts[:ssl_verify_peer] = true
    opts[:ssl_ca_file] = ssl.ca_cert_file if ssl.ca_cert_file
    opts[:certificate] = ssl.cert.to_pem     if ssl.cert
    opts[:private_key] = ssl.cert_key.to_pem if ssl.cert_key
  when :none
    opts[:ssl_verify_peer] = false
  end

  opts[:ciphers] = ssl.ciphers if ssl.ciphers
  opts[:ssl_version] = ssl.ssl_version if ssl.ssl_version
  opts[:ssl_min_version] = ssl.min_version if ssl.min_version
  opts[:ssl_max_version] = ssl.max_version if ssl.max_version

  opts
end
respond_with(response) click to toggle source
# File lib/httpi/adapter/excon.rb, line 84
def respond_with(response)
  headers = response.headers.dup
  if (cookies = response.data[:cookies]) && !cookies.empty?
    headers["Set-Cookie"] = cookies
  end
  Response.new response.status, headers, response.body
end