Class: Contrast::Agent::Response
- Extended by:
- Forwardable
- Defined in:
- lib/contrast/agent/response/response.rb
Overview
This class is the Contrast representation of the Rack::Response object. It provides access to the original Rack::Response object as well as extracts data in a format that the Agent expects, caching those transformations in order to avoid repeatedly creating Strings & thrashing GC.
Instance Attribute Summary collapse
-
#rack_response ⇒ Array, Rack::Response
readonly
The Rack Response passed by the application & middleware.
Instance Method Summary collapse
-
#body ⇒ String?
The response body can change during the request lifecycle, so we have to look it up every time we need it.
-
#content_type ⇒ String?
The Content-Type of this response, as set in the headers hash under the key Rack::CONTENT_TYPE.
-
#document_type ⇒ Symbol<:XML, :JSON, :NORMAL>?
The document type of the response, based on its Content-Type.
-
#headers ⇒ Hash?
The headers of this response.
-
#initialize(rack_response) ⇒ Response
constructor
A new instance of Response.
-
#response_code ⇒ Integer
The response code of this response.
Methods included from Components::Logger::InstanceMethods
Constructor Details
#initialize(rack_response) ⇒ Response
Returns a new instance of Response.
29 30 31 32 |
# File 'lib/contrast/agent/response/response.rb', line 29 def initialize rack_response @rack_response = rack_response @is_array = !rack_response.is_a?(Rack::Response) end |
Instance Attribute Details
#rack_response ⇒ Array, Rack::Response (readonly)
Returns The Rack Response passed by the application & middleware. It can be an Array in format [response_code, header_hash, response_body] or an instance of Rack::Response.
27 28 29 |
# File 'lib/contrast/agent/response/response.rb', line 27 def rack_response @rack_response end |
Instance Method Details
#body ⇒ String?
The response body can change during the request lifecycle, so we have to look it up every time we need it. We should not extract it out as a variable here, or we’ll miss those changes.
79 80 81 82 83 84 |
# File 'lib/contrast/agent/response/response.rb', line 79 def body return unless rack_response body_content = @is_array ? rack_response[2] : rack_response.body extract_body(body_content) end |
#content_type ⇒ String?
The Content-Type of this response, as set in the headers hash under the key Rack::CONTENT_TYPE
69 70 71 72 73 |
# File 'lib/contrast/agent/response/response.rb', line 69 def content_type return unless rack_response @is_array ? headers[Rack::CONTENT_TYPE] : rack_response.content_type end |
#document_type ⇒ Symbol<:XML, :JSON, :NORMAL>?
The document type of the response, based on its Content-Type. Can be one of :JSON, :NORMAL, :XML, or nothing.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/contrast/agent/response/response.rb', line 37 def document_type case content_type when /xml/i :XML when /json/i :JSON when /html/i :NORMAL end end |
#headers ⇒ Hash?
The headers of this response
60 61 62 63 64 |
# File 'lib/contrast/agent/response/response.rb', line 60 def headers return unless rack_response @is_array ? rack_response[1] : rack_response.headers end |
#response_code ⇒ Integer
The response code of this response
51 52 53 54 55 |
# File 'lib/contrast/agent/response/response.rb', line 51 def response_code return unless rack_response @is_array ? rack_response[0].to_i : rack_response.status end |