Class: Contrast::Agent::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Components::Logger::InstanceMethods, Utils::ResponseUtils
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

Instance Method Summary collapse

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

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_responseArray, 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.

Returns:

  • (Array, Rack::Response)

    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

#bodyString?

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.

Returns:



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_typeString?

The Content-Type of this response, as set in the headers hash under the key Rack::CONTENT_TYPE

Returns:



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_typeSymbol<:XML, :JSON, :NORMAL>?

The document type of the response, based on its Content-Type. Can be one of :JSON, :NORMAL, :XML, or nothing.

Returns:

  • (Symbol<:XML, :JSON, :NORMAL>, nil)


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

#headersHash?

The headers of this response

Returns:



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_codeInteger

The response code of this response

Returns:

  • (Integer)


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