Class: HttpCapture::Response

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/http_capture.rb

Overview

Represents a captured response.

Direct Known Subclasses

HTTPClientResponse, NetHTTPResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(real_response, request: nil, duration: nil) ⇒ Response

Returns a new instance of Response.



49
50
51
52
53
# File 'lib/http_capture.rb', line 49

def initialize(real_response, request: nil, duration: nil)
  @real_response = real_response
  @request = request
  @duration = duration
end

Instance Attribute Details

#durationObject (readonly)

The duration of the request in seconds.



47
48
49
# File 'lib/http_capture.rb', line 47

def duration
  @duration
end

#requestObject (readonly)

The request that this is a response to.



44
45
46
# File 'lib/http_capture.rb', line 44

def request
  @request
end

Instance Method Details

#[](key) ⇒ Object

Provides access to the response headers.



56
57
58
59
60
61
62
63
64
# File 'lib/http_capture.rb', line 56

def [](key)
  if @real_response.respond_to?(:[])
    @real_response[key]
  elsif @real_response.respond_to?(:headers)
    @real_response.headers[key]
  elsif @real_response.respond_to?(:header)
    @real_response.header[key]
  end
end

#bodyObject

The default body accessor



90
91
92
# File 'lib/http_capture.rb', line 90

def body
  @real_response.body
end

#each(&block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/http_capture.rb', line 66

def each(&block)
  # some of the libraries return just header names in the each method; others return the header name and value
  # as an array. some downcase the header name but others leave them as they were. this adapts them to the most
  # useful and consistent which is an array of [name, value] with the name in lower case.
  action = Proc.new { |item| block.(item.is_a?(String) ? [item.downcase, self[item]] : item) }
  if @real_response.respond_to?(:each)
    @real_response.each &action
  elsif @real_response.respond_to?(:headers)
    @real_response.headers.each &action
  elsif @real_response.respond_to?(:header)
    @real_response.header.each &action
  end        
end

#statusObject

The default status code accessor



81
82
83
84
85
86
87
# File 'lib/http_capture.rb', line 81

def status
  if @real_response.respond_to?(:code)
    @real_response.code.to_i
  else
    @real_response.status.to_i
  end
end

#successful?Boolean

Whether the request was successful.

Returns:

  • (Boolean)


95
96
97
# File 'lib/http_capture.rb', line 95

def successful?
  status >= 200 && status < 300
end