class HTTPI::Adapter::EmHttpRequest

An HTTPI adapter for ‘EventMachine::HttpRequest`. Due to limitations of the em-httprequest library, not all features are supported. In particular,

are supported by HTTPI but not em-httprequest.

In addition, some features of em-httprequest are not represented in HTTPI and are therefore not supported. In particular,

are supported by em-httprequest but not HTTPI.

Attributes

cert_directory[W]
client[R]

Public Class Methods

new(request) click to toggle source
# File lib/httpi/adapter/em_http.rb, line 34
def initialize(request)
  @request = request
  @client = EventMachine::HttpRequest.new build_request_url(request.url), connection_options
end

Public Instance Methods

cert_directory() click to toggle source
# File lib/httpi/adapter/em_http.rb, line 41
def cert_directory
  @cert_directory ||= "/tmp"
end
request(method) click to toggle source

Executes arbitrary HTTP requests. @see HTTPI.request

# File lib/httpi/adapter/em_http.rb, line 49
def request(method)
  _request { |options| @client.send method, options }
end

Private Instance Methods

_request() { |options| ... } click to toggle source
# File lib/httpi/adapter/em_http.rb, line 55
def _request
  options = client_options
  setup_http_auth(options) if @request.auth.http?

  if @request.auth.ssl?
    raise NotSupportedError, "EM-HTTP-Request does not support SSL client auth"
  end

  if @request.on_body
    raise NotSupportedError, "EM-HTTP-Request does not support response streaming"
  end

  start_time = Time.now
  respond_with yield(options), start_time
end
build_request_url(url) click to toggle source
# File lib/httpi/adapter/em_http.rb, line 115
def build_request_url(url)
  "%s://%s:%s%s" % [url.scheme, url.host, url.port, url.path]
end
client_options() click to toggle source
# File lib/httpi/adapter/em_http.rb, line 83
def client_options
  {
    :query => @request.url.query,
    :head  => @request.headers.to_hash,
    :body  => @request.body
  }
end
connection_options() click to toggle source
# File lib/httpi/adapter/em_http.rb, line 71
def connection_options
  options = {}

  read_or_write_timeout = @request.read_timeout || @request.write_timeout
  options[:inactivity_timeout] = read_or_write_timeout if read_or_write_timeout
  options[:connect_timeout] = @request.open_timeout if @request.open_timeout

  options[:proxy] = proxy_options if @request.proxy

  options
end
convert_headers(headers) click to toggle source

Takes any header names with an underscore as a word separator and converts the name to camel case, where words are separated by a dash.

E.g. CONTENT_TYPE becomes Content-Type.

# File lib/httpi/adapter/em_http.rb, line 123
def convert_headers(headers)
  return headers unless headers.keys.any? { |k| k =~ /_/ }

  result = {}

  headers.each do |k, v|
    words = k.split("_")
    key = words.map { |w| w.downcase.capitalize }.join("-")
    result[key] = v
  end

  result
end
proxy_options() click to toggle source
# File lib/httpi/adapter/em_http.rb, line 91
def proxy_options
  {
    :host          => @request.proxy.host,
    :port          => @request.proxy.port,
    :authorization => [@request.proxy.user, @request.proxy.password]
  }
end
respond_with(http, start_time) click to toggle source
# File lib/httpi/adapter/em_http.rb, line 108
def respond_with(http, start_time)
  raise TimeoutError, "EM-HTTP-Request connection timed out: #{Time.now - start_time} sec" if http.response_header.status.zero?

  Response.new http.response_header.status,
    convert_headers(http.response_header), http.response
end
setup_http_auth(options) click to toggle source
# File lib/httpi/adapter/em_http.rb, line 99
def setup_http_auth(options)
  unless @request.auth.type == :basic
    raise NotSupportedError, "EM-HTTP-Request does only support HTTP basic auth"
  end

  options[:head] ||= {}
  options[:head][:authorization] = @request.auth.credentials
end