Class: Forem::ForemResponse
- Inherits:
-
Object
- Object
- Forem::ForemResponse
- Defined in:
- lib/forem/forem_response.rb
Overview
Wraps a raw HTTP response from the Forem API.
ForemResponse is an internal value object created by APIRequestor after every successful round-trip to the server. It normalises the Net::HTTP response into a simple struct and provides lazy JSON parsing via #parsed_body.
Instance Attribute Summary collapse
-
#http_body ⇒ String
readonly
The raw, unparsed HTTP response body as a string.
-
#http_headers ⇒ Hash
readonly
A hash of downcased HTTP response header names to their string values (e.g. { "content-type" => "application/json" }).
-
#http_status ⇒ Integer
readonly
The HTTP status code of the response (e.g.
200,404).
Instance Method Summary collapse
-
#initialize(http_status:, http_body:, http_headers:) ⇒ ForemResponse
constructor
Create a new ForemResponse.
-
#parsed_body ⇒ Hash, ...
Parse the response body as JSON, caching the result after the first call.
Constructor Details
#initialize(http_status:, http_body:, http_headers:) ⇒ ForemResponse
Create a new ForemResponse.
32 33 34 35 36 |
# File 'lib/forem/forem_response.rb', line 32 def initialize(http_status:, http_body:, http_headers:) @http_status = http_status @http_body = http_body @http_headers = http_headers end |
Instance Attribute Details
#http_body ⇒ String (readonly)
Returns the raw, unparsed HTTP response body as a string.
20 21 22 |
# File 'lib/forem/forem_response.rb', line 20 def http_body @http_body end |
#http_headers ⇒ Hash (readonly)
Returns a hash of downcased HTTP response header names to their string values (e.g. { "content-type" => "application/json" }).
24 25 26 |
# File 'lib/forem/forem_response.rb', line 24 def http_headers @http_headers end |
#http_status ⇒ Integer (readonly)
Returns the HTTP status code of the response (e.g. 200, 404).
17 18 19 |
# File 'lib/forem/forem_response.rb', line 17 def http_status @http_status end |
Instance Method Details
#parsed_body ⇒ Hash, ...
Parse the response body as JSON, caching the result after the first call.
Returns nil when the body is absent or blank (e.g. HTTP 204 No Content).
45 46 47 48 |
# File 'lib/forem/forem_response.rb', line 45 def parsed_body return nil if http_body.nil? || http_body.empty? @parsed_body ||= JSON.parse(http_body) end |