Class: PatientHttp::Response
- Inherits:
-
Object
- Object
- PatientHttp::Response
- Defined in:
- lib/patient_http/response.rb
Overview
Represents an HTTP response from an async request.
This class encapsulates the response data including status, headers, body, and metadata about the request that generated it.
Instance Attribute Summary collapse
-
#duration ⇒ Float
readonly
Request duration in seconds.
-
#headers ⇒ HttpHeaders
readonly
Response headers.
-
#http_method ⇒ Symbol
readonly
HTTP method.
-
#redirects ⇒ Array<String>
readonly
URLs visited during redirect chain (empty if no redirects).
-
#request_id ⇒ String
readonly
Request ID.
-
#status ⇒ Integer
readonly
HTTP status code.
-
#url ⇒ String
readonly
Request URL.
Class Method Summary collapse
-
.load(hash) ⇒ Response
Reconstruct a Response from a hash.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Serialize to JSON hash.
-
#body ⇒ String?
Returns the response body, decoding it from the payload if necessary.
-
#callback_args ⇒ CallbackArgs
Returns the callback arguments as a CallbackArgs object.
-
#client_error? ⇒ Boolean
Check if response is a client error (4xx status).
-
#content_type ⇒ String?
Get the Content-Type header.
-
#error? ⇒ Boolean
Check if response is any error (4xx or 5xx status).
-
#initialize(status:, headers:, body:, duration:, request_id:, url:, http_method:, callback_args: nil, redirects: nil) ⇒ Response
constructor
Initialize a Response from an Async::HTTP::Response.
-
#json ⇒ Hash, Array
Parse response body as JSON.
-
#json? ⇒ Boolean
Return true if Content-Type indicates JSON.
-
#redirect? ⇒ Boolean
Check if response is a redirect (3xx status).
-
#server_error? ⇒ Boolean
Check if response is a server error (5xx status).
-
#success? ⇒ Boolean
Check if response is successful (2xx status).
-
#to_json(options = nil) ⇒ String
Serialize to JSON string.
Constructor Details
#initialize(status:, headers:, body:, duration:, request_id:, url:, http_method:, callback_args: nil, redirects: nil) ⇒ Response
Initialize a Response from an Async::HTTP::Response
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/patient_http/response.rb', line 67 def initialize(status:, headers:, body:, duration:, request_id:, url:, http_method:, callback_args: nil, redirects: nil) @status = status @headers = HttpHeaders.new(headers) encoding, encoded_body, charset = Payload.encode(body, @headers["content-type"]) @payload = Payload.new(encoding, encoded_body, charset) unless body.nil? @body = UNDEFINED @duration = duration @request_id = request_id @url = url @http_method = http_method @callback_args_data = callback_args || {} @redirects = redirects || [] end |
Instance Attribute Details
#duration ⇒ Float (readonly)
Returns request duration in seconds.
22 23 24 |
# File 'lib/patient_http/response.rb', line 22 def duration @duration end |
#headers ⇒ HttpHeaders (readonly)
Response headers. Headers that appeared multiple times in the response (such as set-cookie) are flattened into a single joined string value.
19 20 21 |
# File 'lib/patient_http/response.rb', line 19 def headers @headers end |
#http_method ⇒ Symbol (readonly)
Returns HTTP method.
31 32 33 |
# File 'lib/patient_http/response.rb', line 31 def http_method @http_method end |
#redirects ⇒ Array<String> (readonly)
Returns URLs visited during redirect chain (empty if no redirects).
34 35 36 |
# File 'lib/patient_http/response.rb', line 34 def redirects @redirects end |
#request_id ⇒ String (readonly)
Returns request ID.
25 26 27 |
# File 'lib/patient_http/response.rb', line 25 def request_id @request_id end |
#status ⇒ Integer (readonly)
Returns HTTP status code.
13 14 15 |
# File 'lib/patient_http/response.rb', line 13 def status @status end |
#url ⇒ String (readonly)
Returns request URL.
28 29 30 |
# File 'lib/patient_http/response.rb', line 28 def url @url end |
Class Method Details
.load(hash) ⇒ Response
Reconstruct a Response from a hash
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/patient_http/response.rb', line 41 def load(hash) new( status: hash["status"], headers: hash["headers"], body: Payload.load(hash["body"])&.value, duration: hash["duration"], request_id: hash["request_id"], url: hash["url"], http_method: hash["http_method"]&.to_sym, callback_args: hash["callback_args"], redirects: hash["redirects"] ) end |
Instance Method Details
#as_json ⇒ Hash
Serialize to JSON hash.
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/patient_http/response.rb', line 164 def as_json { "status" => status, "headers" => headers.to_h, "body" => @payload&.as_json, "duration" => duration, "request_id" => request_id, "url" => url, "http_method" => http_method.to_s, "callback_args" => @callback_args_data, "redirects" => @redirects } end |
#body ⇒ String?
Returns the response body, decoding it from the payload if necessary.
93 94 95 96 |
# File 'lib/patient_http/response.rb', line 93 def body @body = @payload&.value if @body.equal?(UNDEFINED) @body end |
#callback_args ⇒ CallbackArgs
Returns the callback arguments as a CallbackArgs object.
86 87 88 |
# File 'lib/patient_http/response.rb', line 86 def callback_args @callback_args ||= CallbackArgs.load(@callback_args_data) end |
#client_error? ⇒ Boolean
Check if response is a client error (4xx status)
115 116 117 |
# File 'lib/patient_http/response.rb', line 115 def client_error? status >= 400 && status < 500 end |
#content_type ⇒ String?
Get the Content-Type header
136 137 138 |
# File 'lib/patient_http/response.rb', line 136 def content_type headers["content-type"] end |
#error? ⇒ Boolean
Check if response is any error (4xx or 5xx status)
129 130 131 |
# File 'lib/patient_http/response.rb', line 129 def error? status >= 400 && status < 600 end |
#json ⇒ Hash, Array
Parse response body as JSON
153 154 155 156 157 158 159 |
# File 'lib/patient_http/response.rb', line 153 def json unless json? raise "Response Content-Type is not application/json (got: #{content_type.inspect})" end JSON.parse(body) end |
#json? ⇒ Boolean
Return true if Content-Type indicates JSON.
143 144 145 146 |
# File 'lib/patient_http/response.rb', line 143 def json? type = content_type.to_s.downcase type.match?(%r{\Aapplication/[^ ]*json\b}) || type == "text/json" end |
#redirect? ⇒ Boolean
Check if response is a redirect (3xx status)
108 109 110 |
# File 'lib/patient_http/response.rb', line 108 def redirect? status >= 300 && status < 400 end |
#server_error? ⇒ Boolean
Check if response is a server error (5xx status)
122 123 124 |
# File 'lib/patient_http/response.rb', line 122 def server_error? status >= 500 && status < 600 end |
#success? ⇒ Boolean
Check if response is successful (2xx status)
101 102 103 |
# File 'lib/patient_http/response.rb', line 101 def success? status >= 200 && status < 300 end |
#to_json(options = nil) ⇒ String
Serialize to JSON string.
182 183 184 |
# File 'lib/patient_http/response.rb', line 182 def to_json( = nil) JSON.generate(as_json, ) end |