Module: RestClient

Defined in:
lib/restclient/components.rb

Defined Under Namespace

Modules: Payload, Rack Classes: MockNetHTTPResponse, Request

Constant Summary collapse

RACK_APP =
Proc.new { |env|
  begin
    # get the original request, replace headers with those of env, and execute it
    request = env['restclient.hash'][:request]
    env_headers = (env.keys.select{|k| k=~/^HTTP_/}).inject({}){|accu, k|
      accu[k.gsub("HTTP_", "").split("_").map{|s| s.downcase.capitalize}.join("-")] = env[k]
      accu
    }
    env_headers['Content-Type'] = env['CONTENT_TYPE'] if env['CONTENT_TYPE']
    env_headers['Content-Length'] = env['CONTENT_LENGTH'] if env['CONTENT_LENGTH']

    env['rack.input'].rewind
    payload = env['rack.input'].read
    payload = (payload.empty? ? nil : Payload.generate(payload))
    request.instance_variable_set "@payload", payload

    headers = request.make_headers(env_headers)
    request.processed_headers.update(headers)
    response = request.original_execute
  rescue RestClient::ExceptionWithResponse => e
    # re-raise the error if the response is nil (RestClient does not
    # differentiate between a RestClient::RequestTimeout due to a 408 status
    # code, and a RestClient::RequestTimeout due to a Timeout::Error)
    raise e if e.response.nil?
    env['restclient.hash'][:error] = e
    response = e.response
  end
  # to satisfy Rack::Lint
  response.headers.delete(:status)
  header = RestClient.debeautify_headers( response.headers )
  body = response.to_s
  # return the real content-length since RestClient does not do it when
  # decoding gzip responses
  header['Content-Length'] = body.length.to_s if header.has_key?('Content-Length')
  [response.code, header, [body]]
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.componentsObject (readonly)

Returns the value of attribute components.



34
35
36
# File 'lib/restclient/components.rb', line 34

def components
  @components
end

Class Method Details

.debeautify_headers(headers = {}) ⇒ Object

:nodoc:



87
88
89
90
91
92
# File 'lib/restclient/components.rb', line 87

def self.debeautify_headers(headers = {})   # :nodoc:
  headers.inject({}) do |out, (key, value)|
    out[key.to_s.gsub(/_/, '-').split("-").map{|w| w.capitalize}.join("-")] = value.to_s
    out
  end
end

.disable(component) ⇒ Object

Disable a component

RestClient.disable Rack::Cache
=> array of remaining components


69
70
71
# File 'lib/restclient/components.rb', line 69

def self.disable(component)
  @components.delete_if{|(existing_component, options)| component == existing_component}
end

.enable(component, *args) ⇒ Object

Enable a Rack component. You may enable as many components as you want.

Examples:

Transparent HTTP caching:

RestClient.enable Rack::Cache,
                    :verbose     => true,
                    :metastore   => 'file:/var/cache/rack/meta'
                    :entitystore => 'file:/var/cache/rack/body'

Transparent logging of HTTP requests (commonlog format):

RestClient.enable Rack::CommonLogger, STDOUT

Please refer to the documentation of each rack component for the list of available options.



55
56
57
58
59
60
61
62
63
# File 'lib/restclient/components.rb', line 55

def self.enable(component, *args)
  # remove any existing component of the same class
  disable(component)
  if component == RestClient::Rack::Compatibility
    @components.push [component, args]
  else
    @components.unshift [component, args]
  end
end

.enabled?(component) ⇒ Boolean

Returns true if the given component is enabled, false otherwise.

RestClient.enable Rack::Cache
RestClient.enabled?(Rack::Cache)
=> true

Returns:

  • (Boolean)


78
79
80
# File 'lib/restclient/components.rb', line 78

def self.enabled?(component)
  !@components.detect{|(existing_component, options)| component == existing_component}.nil?
end

.resetObject



82
83
84
85
# File 'lib/restclient/components.rb', line 82

def self.reset
  # hash of the enabled components
  @components = [[RestClient::Rack::Compatibility]]
end