Class: Takagi::Client::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/client/response.rb

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.

Examples:

Basic usage

client.get('/temperature') do |response|
  if response.success?
    puts "Temperature: #{response.payload}"
  else
    puts "Error: #{response.code_name}"
  end
end

Checking specific codes

response.ok?          # 2.05 Content
response.created?     # 2.01 Created
response.not_found?   # 4.04 Not Found
response.bad_request? # 4.00 Bad Request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data) ⇒ Response

Creates a new Response wrapper

Parameters:

  • raw_data (String)

    Raw binary response data



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.options
  @token = @inbound.token
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



25
26
27
# File 'lib/takagi/client/response.rb', line 25

def code
  @code
end

#inboundObject (readonly)

Returns the value of attribute inbound.



25
26
27
# File 'lib/takagi/client/response.rb', line 25

def inbound
  @inbound
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/takagi/client/response.rb', line 25

def options
  @options
end

#payloadObject (readonly)

Returns the value of attribute payload.



25
26
27
# File 'lib/takagi/client/response.rb', line 25

def payload
  @payload
end

#raw_dataObject (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

#tokenObject (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

Returns:

  • (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

Returns:

  • (Boolean)


105
106
107
# File 'lib/takagi/client/response.rb', line 105

def bad_option?
  @code == CoAP::Registries::Response::BAD_OPTION
end

#bad_request?Boolean

Common 4.xx client error codes (using registry)

Returns:

  • (Boolean)


97
98
99
# File 'lib/takagi/client/response.rb', line 97

def bad_request?
  @code == CoAP::Registries::Response::BAD_REQUEST
end

#changed?Boolean

Returns:

  • (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)

Returns:

  • (Boolean)


58
59
60
# File 'lib/takagi/client/response.rb', line 58

def client_error?
  CoAP::Registries::Response.client_error?(@code)
end

#code_classInteger

Get the numeric code class (2 = Success, 4 = Client Error, 5 = Server Error)

Returns:

  • (Integer)

    Code class



46
47
48
# File 'lib/takagi/client/response.rb', line 46

def code_class
  CoAP::Registries::Response.class_for(@code)
end

#code_nameString

Get the human-readable code name using CoAP registry

Returns:

  • (String)

    Code name (e.g., “2.05 Content”, “4.04 Not Found”)



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?

Returns:

  • (Boolean)


91
92
93
# File 'lib/takagi/client/response.rb', line 91

def content?
  @code == CoAP::Registries::Response::CONTENT
end

#content_formatInteger?

Get content-format option value

Returns:

  • (Integer, nil)

    Content-format code



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?Boolean

Common 2.xx success codes (using registry)

Returns:

  • (Boolean)


75
76
77
# File 'lib/takagi/client/response.rb', line 75

def created?
  @code == CoAP::Registries::Response::CREATED
end

#dataObject?

Deserialize payload using content-format

Automatically deserializes payload based on Content-Format option. Falls back to JSON for unknown formats or if deserialization fails.

Examples:

JSON response

response.data  # => { "temp" => 25 }

CBOR response

response.data  # => { "temp" => 25 }

Text response

response.data  # => "Hello World"

Returns:

  • (Object, nil)

    Deserialized data or nil



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

#deleted?Boolean

Returns:

  • (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)

Returns:

  • (Boolean)


70
71
72
# File 'lib/takagi/client/response.rb', line 70

def error?
  CoAP::Registries::Response.error?(@code)
end

#forbidden?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/takagi/client/response.rb', line 109

def forbidden?
  @code == CoAP::Registries::Response::FORBIDDEN
end

#gateway_timeout?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/takagi/client/response.rb', line 154

def gateway_timeout?
  @code == CoAP::Registries::Response::GATEWAY_TIMEOUT
end

#inspectString

Detailed inspection

Returns:

  • (String)


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?Boolean

Common 5.xx server error codes (using registry)

Returns:

  • (Boolean)


138
139
140
# File 'lib/takagi/client/response.rb', line 138

def internal_server_error?
  @code == CoAP::Registries::Response::INTERNAL_SERVER_ERROR
end

#json?Boolean

Check if response has JSON content-format

Returns:

  • (Boolean)


193
194
195
# File 'lib/takagi/client/response.rb', line 193

def json?
  content_format == CoAP::Registries::ContentFormat::JSON
end

#method_not_allowed?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


158
159
160
# File 'lib/takagi/client/response.rb', line 158

def proxying_not_supported?
  @code == CoAP::Registries::Response::PROXYING_NOT_SUPPORTED
end

#request_entity_too_large?Boolean

Returns:

  • (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)

Returns:

  • (Boolean)


64
65
66
# File 'lib/takagi/client/response.rb', line 64

def server_error?
  CoAP::Registries::Response.server_error?(@code)
end

#service_unavailable?Boolean

Returns:

  • (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)

Returns:

  • (Boolean)


52
53
54
# File 'lib/takagi/client/response.rb', line 52

def success?
  CoAP::Registries::Response.success?(@code)
end

#to_sString

String representation for debugging

Returns:

  • (String)


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

Returns:

  • (Boolean)


101
102
103
# File 'lib/takagi/client/response.rb', line 101

def unauthorized?
  @code == CoAP::Registries::Response::UNAUTHORIZED
end

#unsupported_content_format?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


83
84
85
# File 'lib/takagi/client/response.rb', line 83

def valid?
  @code == CoAP::Registries::Response::VALID
end