Sha256: eb2478ba215196c5af3d254077122218a6a657381db9af2fd42eb8aa4688444c

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

require 'em-http-request'
require 'restclient/payload'

require 'rest-core/engine/future/future'
require 'rest-core/middleware'

class RestCore::EmHttpRequest
  include RestCore::Middleware
  def call env, &k
    future  = Future.create(env, k, env[ASYNC])
    payload = ::RestClient::Payload.generate(env[REQUEST_PAYLOAD])
    client  = ::EventMachine::HttpRequest.new(request_uri(env)).send(
                 env[REQUEST_METHOD],
                 :body => payload && payload.read,
                 :head => payload && payload.headers.
                                               merge(env[REQUEST_HEADERS]))

    client.callback{
      future.on_load(client.response,
                     client.response_header.status,
                     client.response_header)}

    client.errback{future.on_error(client.error) }

    env[TIMER].on_timeout{
      (client.instance_variable_get(:@callbacks)||[]).clear
      (client.instance_variable_get(:@errbacks )||[]).clear
      client.close
      future.on_error(env[TIMER].error)
    } if env[TIMER]

    env.merge(RESPONSE_BODY    => future.proxy_body,
              RESPONSE_STATUS  => future.proxy_status,
              RESPONSE_HEADERS => future.proxy_headers,
              FUTURE           => future)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rest-core-2.0.3 lib/rest-core/engine/em-http-request.rb
rest-core-2.0.2 lib/rest-core/engine/em-http-request.rb
rest-core-2.0.1 lib/rest-core/engine/em-http-request.rb