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
-
.components ⇒ Object
readonly
Returns the value of attribute components.
Class Method Summary collapse
-
.debeautify_headers(headers = {}) ⇒ Object
:nodoc:.
-
.disable(component) ⇒ Object
Disable a component.
-
.enable(component, *args) ⇒ Object
Enable a Rack component.
-
.enabled?(component) ⇒ Boolean
Returns true if the given component is enabled, false otherwise.
- .reset ⇒ Object
Class Attribute Details
.components ⇒ Object (readonly)
Returns the value of attribute components.
30 31 32 |
# File 'lib/restclient/components.rb', line 30 def components @components end |
Class Method Details
.debeautify_headers(headers = {}) ⇒ Object
:nodoc:
83 84 85 86 87 88 |
# File 'lib/restclient/components.rb', line 83 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
65 66 67 |
# File 'lib/restclient/components.rb', line 65 def self.disable(component) @components.delete_if{|(existing_component, )| 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.
51 52 53 54 55 56 57 58 59 |
# File 'lib/restclient/components.rb', line 51 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
74 75 76 |
# File 'lib/restclient/components.rb', line 74 def self.enabled?(component) !@components.detect{|(existing_component, )| component == existing_component}.nil? end |
.reset ⇒ Object
78 79 80 81 |
# File 'lib/restclient/components.rb', line 78 def self.reset # hash of the enabled components @components = [[RestClient::Rack::Compatibility]] end |