Class: Takagi::Client::Response
- Inherits:
-
Object
- Object
- Takagi::Client::Response
- Defined in:
- lib/takagi/client/response.rb,
sig/takagi/client/response.rbs
Overview
Wrapper for CoAP responses providing convenient access to response data and status checking methods.
Uses the CoAP registry system for all code checking and naming.
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#inbound ⇒ Object
readonly
Returns the value of attribute inbound.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #bad_gateway? ⇒ Boolean
- #bad_option? ⇒ Boolean
-
#bad_request? ⇒ Object
Common 4.xx client error codes (using registry).
- #changed? ⇒ Boolean
-
#client_error? ⇒ Boolean
Check if response is a client error (4.xx).
-
#code_class ⇒ Integer
Get the numeric code class (2 = Success, 4 = Client Error, 5 = Server Error).
-
#code_name ⇒ String
Get the human-readable code name using CoAP registry.
- #content? ⇒ Boolean (also: #ok?)
-
#content_format ⇒ Integer?
Get content-format option value.
-
#created? ⇒ Object
Common 2.xx success codes (using registry).
-
#data ⇒ Object?
Deserialize payload using content-format.
-
#decode_integer_value(bytes) ⇒ nil, untyped
Decode a binary string to an integer.
- #deleted? ⇒ Boolean
-
#error? ⇒ Boolean
Check if response has an error (4.xx or 5.xx).
- #forbidden? ⇒ Boolean
- #gateway_timeout? ⇒ Boolean
-
#initialize(raw_data) ⇒ Response
constructor
Creates a new Response wrapper.
-
#inspect ⇒ String
Detailed inspection.
-
#internal_server_error? ⇒ Object
Common 5.xx server error codes (using registry).
-
#json ⇒ Hash, ...
Parse payload as JSON.
-
#json? ⇒ Boolean
Check if response has JSON content-format.
- #method_not_allowed? ⇒ Boolean
- #not_acceptable? ⇒ Boolean
- #not_found? ⇒ Boolean
- #not_implemented? ⇒ Boolean
- #precondition_failed? ⇒ Boolean
- #proxying_not_supported? ⇒ Boolean
- #request_entity_too_large? ⇒ Boolean
-
#server_error? ⇒ Boolean
Check if response is a server error (5.xx).
- #service_unavailable? ⇒ Boolean
-
#success? ⇒ Boolean
Check if response is successful (2.xx).
-
#to_s ⇒ String
String representation for debugging.
- #unauthorized? ⇒ Boolean
- #unsupported_content_format? ⇒ Boolean
- #valid? ⇒ Boolean
Constructor Details
#initialize(raw_data) ⇒ Response
Creates a new Response wrapper
29 30 31 32 33 34 35 36 |
# File 'lib/takagi/client/response.rb', line 29 def initialize(raw_data) @raw_data = raw_data @inbound = Takagi::Message::Inbound.new(raw_data) @code = @inbound.code @payload = @inbound.payload @options = @inbound. @token = @inbound.token end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
25 26 27 |
# File 'lib/takagi/client/response.rb', line 25 def code @code end |
#inbound ⇒ Object (readonly)
Returns the value of attribute inbound.
25 26 27 |
# File 'lib/takagi/client/response.rb', line 25 def inbound @inbound end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
25 26 27 |
# File 'lib/takagi/client/response.rb', line 25 def @options end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
25 26 27 |
# File 'lib/takagi/client/response.rb', line 25 def payload @payload end |
#raw_data ⇒ Object (readonly)
Returns the value of attribute raw_data.
25 26 27 |
# File 'lib/takagi/client/response.rb', line 25 def raw_data @raw_data end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
25 26 27 |
# File 'lib/takagi/client/response.rb', line 25 def token @token end |
Instance Method Details
#bad_gateway? ⇒ Boolean
146 147 148 |
# File 'lib/takagi/client/response.rb', line 146 def bad_gateway? @code == CoAP::Registries::Response::BAD_GATEWAY end |
#bad_option? ⇒ Boolean
105 106 107 |
# File 'lib/takagi/client/response.rb', line 105 def bad_option? @code == CoAP::Registries::Response::BAD_OPTION end |
#bad_request? ⇒ Object
Common 4.xx client error codes (using registry)
97 98 99 |
# File 'lib/takagi/client/response.rb', line 97 def bad_request? @code == CoAP::Registries::Response::BAD_REQUEST end |
#changed? ⇒ Boolean
87 88 89 |
# File 'lib/takagi/client/response.rb', line 87 def changed? @code == CoAP::Registries::Response::CHANGED end |
#client_error? ⇒ Boolean
Check if response is a client error (4.xx)
58 59 60 |
# File 'lib/takagi/client/response.rb', line 58 def client_error? CoAP::Registries::Response.client_error?(@code) end |
#code_class ⇒ Integer
Get the numeric code class (2 = Success, 4 = Client Error, 5 = Server Error)
46 47 48 |
# File 'lib/takagi/client/response.rb', line 46 def code_class CoAP::Registries::Response.class_for(@code) end |
#code_name ⇒ String
Get the human-readable code name using CoAP registry
40 41 42 |
# File 'lib/takagi/client/response.rb', line 40 def code_name CoAP::CodeHelpers.to_string(@code) end |
#content? ⇒ Boolean Also known as: ok?
91 92 93 |
# File 'lib/takagi/client/response.rb', line 91 def content? @code == CoAP::Registries::Response::CONTENT end |
#content_format ⇒ Integer?
Get content-format option value
199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/takagi/client/response.rb', line 199 def content_format return nil unless @options value = @options[CoAP::Registries::Option::CONTENT_FORMAT] return nil if value.nil? # Handle both array and non-array values value = value.first if value.is_a?(Array) # Convert to integer (content-format is numeric) value.is_a?(String) ? decode_integer_value(value) : value end |
#created? ⇒ Object
Common 2.xx success codes (using registry)
75 76 77 |
# File 'lib/takagi/client/response.rb', line 75 def created? @code == CoAP::Registries::Response::CREATED end |
#data ⇒ Object?
Deserialize payload using content-format
Automatically deserializes payload based on Content-Format option. Falls back to JSON for unknown formats or if deserialization fails.
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/takagi/client/response.rb', line 177 def data return nil unless @payload format = content_format || CoAP::Registries::ContentFormat::JSON Serialization::Registry.decode(@payload, format) rescue Serialization::UnknownFormatError # Unknown format - try JSON as fallback Serialization::Registry.decode(@payload, CoAP::Registries::ContentFormat::JSON) rescue Serialization::DecodeError # Decoding failed - return raw payload @payload end |
#decode_integer_value(bytes) ⇒ nil, untyped
Decode a binary string to an integer
229 230 231 232 233 |
# File 'lib/takagi/client/response.rb', line 229 def decode_integer_value(bytes) return nil if bytes.nil? || bytes.empty? bytes.bytes.reduce(0) { |acc, byte| (acc << 8) | byte } end |
#deleted? ⇒ Boolean
79 80 81 |
# File 'lib/takagi/client/response.rb', line 79 def deleted? @code == CoAP::Registries::Response::DELETED end |
#error? ⇒ Boolean
Check if response has an error (4.xx or 5.xx)
70 71 72 |
# File 'lib/takagi/client/response.rb', line 70 def error? CoAP::Registries::Response.error?(@code) end |
#forbidden? ⇒ Boolean
109 110 111 |
# File 'lib/takagi/client/response.rb', line 109 def forbidden? @code == CoAP::Registries::Response::FORBIDDEN end |
#gateway_timeout? ⇒ Boolean
154 155 156 |
# File 'lib/takagi/client/response.rb', line 154 def gateway_timeout? @code == CoAP::Registries::Response::GATEWAY_TIMEOUT end |
#inspect ⇒ String
Detailed inspection
220 221 222 223 224 |
# File 'lib/takagi/client/response.rb', line 220 def inspect "#<Takagi::Client::Response code=#{code_name} " \ "success=#{success?} " \ "payload=#{@payload&.byteslice(0, 50)&.inspect}#{@payload && @payload.bytesize > 50 ? '...' : ''}>" end |
#internal_server_error? ⇒ Object
Common 5.xx server error codes (using registry)
138 139 140 |
# File 'lib/takagi/client/response.rb', line 138 def internal_server_error? @code == CoAP::Registries::Response::INTERNAL_SERVER_ERROR end |
#json ⇒ Hash, ...
Parse payload as JSON
124 |
# File 'sig/takagi/client/response.rbs', line 124
def json: () -> untyped
|
#json? ⇒ Boolean
Check if response has JSON content-format
193 194 195 |
# File 'lib/takagi/client/response.rb', line 193 def json? content_format == CoAP::Registries::ContentFormat::JSON end |
#method_not_allowed? ⇒ Boolean
117 118 119 |
# File 'lib/takagi/client/response.rb', line 117 def method_not_allowed? @code == CoAP::Registries::Response::METHOD_NOT_ALLOWED end |
#not_acceptable? ⇒ Boolean
121 122 123 |
# File 'lib/takagi/client/response.rb', line 121 def not_acceptable? @code == CoAP::Registries::Response::NOT_ACCEPTABLE end |
#not_found? ⇒ Boolean
113 114 115 |
# File 'lib/takagi/client/response.rb', line 113 def not_found? @code == CoAP::Registries::Response::NOT_FOUND end |
#not_implemented? ⇒ Boolean
142 143 144 |
# File 'lib/takagi/client/response.rb', line 142 def not_implemented? @code == CoAP::Registries::Response::NOT_IMPLEMENTED end |
#precondition_failed? ⇒ Boolean
125 126 127 |
# File 'lib/takagi/client/response.rb', line 125 def precondition_failed? @code == CoAP::Registries::Response::PRECONDITION_FAILED end |
#proxying_not_supported? ⇒ Boolean
158 159 160 |
# File 'lib/takagi/client/response.rb', line 158 def @code == CoAP::Registries::Response::PROXYING_NOT_SUPPORTED end |
#request_entity_too_large? ⇒ Boolean
129 130 131 |
# File 'lib/takagi/client/response.rb', line 129 def request_entity_too_large? @code == CoAP::Registries::Response::REQUEST_ENTITY_TOO_LARGE end |
#server_error? ⇒ Boolean
Check if response is a server error (5.xx)
64 65 66 |
# File 'lib/takagi/client/response.rb', line 64 def server_error? CoAP::Registries::Response.server_error?(@code) end |
#service_unavailable? ⇒ Boolean
150 151 152 |
# File 'lib/takagi/client/response.rb', line 150 def service_unavailable? @code == CoAP::Registries::Response::SERVICE_UNAVAILABLE end |
#success? ⇒ Boolean
Check if response is successful (2.xx)
52 53 54 |
# File 'lib/takagi/client/response.rb', line 52 def success? CoAP::Registries::Response.success?(@code) end |
#to_s ⇒ String
String representation for debugging
214 215 216 |
# File 'lib/takagi/client/response.rb', line 214 def to_s "#<Takagi::Client::Response code=#{code_name} payload_size=#{@payload&.bytesize || 0}>" end |
#unauthorized? ⇒ Boolean
101 102 103 |
# File 'lib/takagi/client/response.rb', line 101 def @code == CoAP::Registries::Response::UNAUTHORIZED end |
#unsupported_content_format? ⇒ Boolean
133 134 135 |
# File 'lib/takagi/client/response.rb', line 133 def unsupported_content_format? @code == CoAP::Registries::Response::UNSUPPORTED_CONTENT_FORMAT end |
#valid? ⇒ Boolean
83 84 85 |
# File 'lib/takagi/client/response.rb', line 83 def valid? @code == CoAP::Registries::Response::VALID end |