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'] # hack, should probably avoid to call #read on rack.input.. payload = if (env['rack.input'].size > 0) env['rack.input'].rewind Payload.generate(env['rack.input'].read) else nil end 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 RestClient.disable Rack::Cache => array of remaining components.
-
.enable(component, *args) ⇒ Object
Enable a Rack component.
-
.enabled?(component) ⇒ Boolean
Returns true if the given component is enabled, false otherwise RestClient.enable Rack::Cache RestClient.enabled?(Rack::Cache) => true.
- .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:
76 77 78 79 80 81 |
# File 'lib/restclient/components.rb', line 76 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
59 60 61 |
# File 'lib/restclient/components.rb', line 59 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. e.g. 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.
46 47 48 49 50 51 52 53 54 |
# File 'lib/restclient/components.rb', line 46 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
67 68 69 |
# File 'lib/restclient/components.rb', line 67 def self.enabled?(component) !@components.detect{|(existing_component, )| component == existing_component}.nil? end |
.reset ⇒ Object
71 72 73 74 |
# File 'lib/restclient/components.rb', line 71 def self.reset # hash of the enabled components @components = [[RestClient::Rack::Compatibility]] end |