Sha256: c4585bed51b6531a1f83f4d68c0f588f750d75684f9128bc7bb2ce24341d2084

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

require 'rest-core/middleware'

require 'restclient'

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

class RestCore::RestClient
  include RestCore::Middleware
  def call env
    process(env,
            ::RestClient::Request.execute(:method  => env[REQUEST_METHOD ],
                                          :url     => request_uri(env)    ,
                                          :payload => env[REQUEST_PAYLOAD],
                                          :headers => env[REQUEST_HEADERS],
                                          :max_redirects => 0))

  rescue ::RestClient::Exception => e
    process(env, e.response)
  end

  def process env, response
    result = env.merge(RESPONSE_BODY    => response.body,
                       RESPONSE_STATUS  => response.code,
                       RESPONSE_HEADERS => normalize_headers(
                                             response.raw_headers))
    result[ASYNC].call(result) if result[ASYNC]
    result
  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

4 entries across 4 versions & 1 rubygems

Version Path
rest-core-1.0.3 lib/rest-core/app/rest-client.rb
rest-core-1.0.2 lib/rest-core/app/rest-client.rb
rest-core-1.0.1 lib/rest-core/app/rest-client.rb
rest-core-1.0.0 lib/rest-core/app/rest-client.rb