Sha256: 537297754a4adb5a29681672174f9821596d57b582f687fd2be1d15f3e20ef53

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

require 'httpclient'
# httpclient would require something (cookie manager) while initialized,
# so we should try to force requiring them to avoid require deadlock!
HTTPClient.new

require 'rest-builder/engine'

module RestBuilder
  class HttpClient < Engine
    private
    def request env
      client = ::HTTPClient.new
      client.cookie_manager = nil
      client.follow_redirect_count = 0
      client.transparent_gzip_decompression = true
      config = config_engine(env) and config.call(client)
      payload, headers = payload_and_headers(env)

      if env[HIJACK]
        request_async(client, payload, headers, env)
      else
        request_sync(client, payload, headers, env)
      end
    end

    def request_sync client, payload, headers, env
      res = client.request(env[REQUEST_METHOD], env[REQUEST_URI], nil,
              payload, {'User-Agent' => 'Ruby'}.merge(headers))

      {RESPONSE_STATUS  => res.status,
       RESPONSE_HEADERS => normalize_headers(res.header.all),
       RESPONSE_BODY    => res.content}
    end

    def request_async client, payload, headers, env
      res = client.request_async(env[REQUEST_METHOD], env[REQUEST_URI], nil,
              payload, {'User-Agent' => 'Ruby'}.merge(headers)).pop

      {RESPONSE_STATUS  => res.status,
       RESPONSE_HEADERS => normalize_headers(res.header.all),
       RESPONSE_BODY    => '',
       RESPONSE_SOCKET  => res.content}
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rest-builder-0.9.2 lib/rest-builder/engine/http-client.rb
rest-builder-0.9.1 lib/rest-builder/engine/http-client.rb
rest-builder-0.9.0 lib/rest-builder/engine/http-client.rb