Class: Contrast::Agent::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Components::Interface
Defined in:
lib/contrast/agent/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::Interface

included

Constructor Details

#initialize(rack_response) ⇒ Response

Returns a new instance of Response.



26
27
28
29
# File 'lib/contrast/agent/response.rb', line 26

def initialize rack_response
  @rack_response = rack_response
  @is_array = !rack_response.is_a?(Rack::Response)
end

Instance Attribute Details

#rack_responseObject (readonly)

Returns the value of attribute rack_response.



24
25
26
# File 'lib/contrast/agent/response.rb', line 24

def rack_response
  @rack_response
end

Instance Method Details

#bodyObject

The response body can change during the request lifecycle We should not extract it out as a variable here, or we’ll miss those changes.



86
87
88
89
90
91
# File 'lib/contrast/agent/response.rb', line 86

def body
  return unless rack_response

  body_content = @is_array ? rack_response[2] : rack_response.body
  extract_body(body_content)
end

#content_typeObject



73
74
75
76
77
78
79
80
81
# File 'lib/contrast/agent/response.rb', line 73

def content_type
  return unless rack_response

  if @is_array
    headers[Rack::CONTENT_TYPE]
  else
    rack_response.content_type
  end
end

#document_typeObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/contrast/agent/response.rb', line 31

def document_type
  case content_type
  when /xml/i
    :XML
  when /json/i
    :JSON
  when /html/i
    :NORMAL
  end
end

#dtmObject

B/c the response can change, we can’t memoize this :(



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/contrast/agent/response.rb', line 43

def dtm
  context_response = Contrast::Api::Dtm::HttpResponse.new
  context_response.response_code = response_code.to_i
  headers&.each_pair do |key, value|
    append_pair(context_response.normalized_response_headers, key, value)
  end
  context_response.response_body_binary = Contrast::Utils::StringUtils.force_utf8(body)

  doc_type = document_type
  context_response.document_type = doc_type if doc_type

  context_response
end

#headersObject



63
64
65
66
67
68
69
70
71
# File 'lib/contrast/agent/response.rb', line 63

def headers
  return unless rack_response

  if @is_array
    rack_response[1]
  else
    rack_response.headers
  end
end

#response_codeObject



57
58
59
60
61
# File 'lib/contrast/agent/response.rb', line 57

def response_code
  return unless rack_response

  @is_array ? rack_response[0].to_i : rack_response.status
end