Class: Rooibos::Message::HttpResponse
- Inherits:
-
Data
- Object
- Data
- Rooibos::Message::HttpResponse
- Includes:
- Predicates
- Defined in:
- lib/rooibos/message/http_response.rb
Overview
Response from an HTTP command.
HTTP requests return status codes, bodies, and headers. Errors may occur. Without structured responses, handling success vs. error requires manual checks on raw data.
This response includes predicates for common checks and deconstructs for pattern matching. Include Predicates for safe predicate calls on any message.
Use it to handle Command.http completions.
Example
case msg
in { type: :http, envelope: :users, status:, body: }
model.with(users: JSON.parse(body))
in { type: :http, envelope: :users, error: }
model.with(error:)
end
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#envelope ⇒ Object
readonly
Returns the value of attribute envelope.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#deconstruct_keys(_keys) ⇒ Object
Deconstructs for pattern matching.
-
#error? ⇒ Boolean
Returns
trueif an error occurred. -
#http? ⇒ Boolean
Returns
truefor HTTP responses. -
#success? ⇒ Boolean
Returns
trueif status is 2xx.
Methods included from Predicates
#==, #method_missing, #respond_to_missing?, #to_sym
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rooibos::Message::Predicates
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body
30 31 32 |
# File 'lib/rooibos/message/http_response.rb', line 30 def body @body end |
#envelope ⇒ Object (readonly)
Returns the value of attribute envelope
30 31 32 |
# File 'lib/rooibos/message/http_response.rb', line 30 def envelope @envelope end |
#error ⇒ Object (readonly)
Returns the value of attribute error
30 31 32 |
# File 'lib/rooibos/message/http_response.rb', line 30 def error @error end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers
30 31 32 |
# File 'lib/rooibos/message/http_response.rb', line 30 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status
30 31 32 |
# File 'lib/rooibos/message/http_response.rb', line 30 def status @status end |
Instance Method Details
#deconstruct_keys(_keys) ⇒ Object
Deconstructs for pattern matching.
Returns a hash with :type, :envelope, and either :error or :status, :body, :headers.
52 53 54 55 56 57 58 |
# File 'lib/rooibos/message/http_response.rb', line 52 def deconstruct_keys(_keys) if error { type: :http, envelope:, error: } else { type: :http, envelope:, status:, body:, headers: } end end |
#error? ⇒ Boolean
Returns true if an error occurred.
44 45 46 |
# File 'lib/rooibos/message/http_response.rb', line 44 def error? !!error end |
#http? ⇒ Boolean
Returns true for HTTP responses.
34 35 36 |
# File 'lib/rooibos/message/http_response.rb', line 34 def http? true end |
#success? ⇒ Boolean
Returns true if status is 2xx.
39 40 41 |
# File 'lib/rooibos/message/http_response.rb', line 39 def success? status&.between?(200, 299) end |