Sha256: f39351d682dedfe139d8fa239ad7b13acbd29b549d41921c5e499c0d7f123ad1

Contents?: true

Size: 1.78 KB

Versions: 5

Compression:

Stored size: 1.78 KB

Contents

require 'restclient'
require 'rest-core/patch/rest-client'

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

class RestCore::RestClient
  include RestCore::Middleware
  def call env, &k
    future  = Future::FutureThread.new(env, k, env[ASYNC])

    t = future.wrap{ # we can implement thread pool in the future
      begin
        res = ::RestClient::Request.execute(:method  => env[REQUEST_METHOD ],
                                            :url     => request_uri(env)    ,
                                            :payload => env[REQUEST_PAYLOAD],
                                            :headers => env[REQUEST_HEADERS],
                                            :max_redirects => 0)
        future.on_load(res.body, res.code, normalize_headers(res.raw_headers))

      rescue ::RestClient::Exception => e
        if res = e.response
          # we don't want to raise an exception for 404 requests
          future.on_load(res.body, res.code,
            normalize_headers(res.raw_headers))
        else
          future.on_error(e)
        end
      rescue Exception => e
        future.on_error(e)
      end
    }

    env[TIMER].on_timeout{
      t.kill
      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

  def normalize_headers raw_headers
    raw_headers.inject({}){ |r, (k, v)|
      r[k.to_s.upcase.tr('-', '_')] = if v.kind_of?(Array) && v.size == 1
                                        v.first
                                      else
                                        v
                                      end
      r
    }
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rest-core-2.0.4 lib/rest-core/engine/rest-client.rb
rest-core-2.0.3 lib/rest-core/engine/rest-client.rb
rest-core-2.0.2 lib/rest-core/engine/rest-client.rb
rest-core-2.0.1 lib/rest-core/engine/rest-client.rb
rest-core-2.0.0 lib/rest-core/engine/rest-client.rb