Module: Takagi::CoAP::CodeHelpers
- Defined in:
- lib/takagi/coap/code_helpers.rb
Overview
Utility methods for working with CoAP codes.
Provides conversion between different code representations and lookups across all registries.
Class Method Summary collapse
-
.all ⇒ Hash
Get all registered codes across all registries.
-
.client_error?(code) ⇒ Boolean
Check if a code represents a client error.
-
.code_type(code) ⇒ Symbol
Get the type of code (method, response, or unknown).
-
.error?(code) ⇒ Boolean
Check if a code represents an error.
-
.find_rfc(code) ⇒ String?
Find RFC reference for a code.
-
.lookup(code) ⇒ Hash?
Lookup code in all registries.
-
.numeric_to_string(code) ⇒ String
Convert numeric code to dotted string format (e.g., 69 => “2.05”).
-
.server_error?(code) ⇒ Boolean
Check if a code represents a server error.
-
.string_to_numeric(code_string) ⇒ Integer
Convert dotted string to numeric code (e.g., “2.05” => 69).
-
.success?(code) ⇒ Boolean
Check if a code represents success.
-
.to_numeric(code) ⇒ Integer
Convert a code to its numeric representation.
-
.to_string(code) ⇒ String
Convert a code to its human-readable string representation.
Class Method Details
.all ⇒ Hash
Get all registered codes across all registries
185 186 187 |
# File 'lib/takagi/coap/code_helpers.rb', line 185 def self.all Registries::Method.all.merge(Registries::Response.all) end |
.client_error?(code) ⇒ Boolean
Check if a code represents a client error
127 128 129 130 |
# File 'lib/takagi/coap/code_helpers.rb', line 127 def self.client_error?(code) numeric = to_numeric(code) Registries::Response.client_error?(numeric) end |
.code_type(code) ⇒ Symbol
Get the type of code (method, response, or unknown)
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/takagi/coap/code_helpers.rb', line 162 def self.code_type(code) if code < 32 :method elsif code.between?(64, 191) :response elsif code.between?(224, 255) :signaling else :unknown end end |
.error?(code) ⇒ Boolean
Check if a code represents an error
118 119 120 121 |
# File 'lib/takagi/coap/code_helpers.rb', line 118 def self.error?(code) numeric = to_numeric(code) Registries::Response.error?(numeric) end |
.find_rfc(code) ⇒ String?
Find RFC reference for a code
178 179 180 |
# File 'lib/takagi/coap/code_helpers.rb', line 178 def self.find_rfc(code) Registries::Method.rfc_for(code) || Registries::Response.rfc_for(code) || Registries::Signaling.rfc_for(code) end |
.lookup(code) ⇒ Hash?
Lookup code in all registries
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/takagi/coap/code_helpers.rb', line 145 def self.lookup(code) numeric = to_numeric(code) return nil if numeric.zero? { value: numeric, string: numeric_to_string(numeric), name: to_string(numeric), type: code_type(numeric), rfc: find_rfc(numeric) } end |
.numeric_to_string(code) ⇒ String
Convert numeric code to dotted string format (e.g., 69 => “2.05”)
87 88 89 90 91 |
# File 'lib/takagi/coap/code_helpers.rb', line 87 def self.numeric_to_string(code) class_num = code / 32 detail_num = code % 32 "#{class_num}.#{detail_num.to_s.rjust(2, '0')}" end |
.server_error?(code) ⇒ Boolean
Check if a code represents a server error
136 137 138 139 |
# File 'lib/takagi/coap/code_helpers.rb', line 136 def self.server_error?(code) numeric = to_numeric(code) Registries::Response.server_error?(numeric) end |
.string_to_numeric(code_string) ⇒ Integer
Convert dotted string to numeric code (e.g., “2.05” => 69)
97 98 99 100 101 102 103 |
# File 'lib/takagi/coap/code_helpers.rb', line 97 def self.string_to_numeric(code_string) return 0 unless code_string =~ /^(\d)\.(\d{2})$/ class_num = ::Regexp.last_match(1).to_i detail_num = ::Regexp.last_match(2).to_i (class_num * 32) + detail_num end |
.success?(code) ⇒ Boolean
Check if a code represents success
109 110 111 112 |
# File 'lib/takagi/coap/code_helpers.rb', line 109 def self.success?(code) numeric = to_numeric(code) Registries::Response.success?(numeric) end |
.to_numeric(code) ⇒ Integer
Convert a code to its numeric representation
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/takagi/coap/code_helpers.rb', line 63 def self.to_numeric(code) case code when Integer code when Symbol Registries::Method.value_for(code) || Registries::Response.value_for(code) || Registries::Signaling.value_for(code) || 0 when String # Handle "2.05" or "2.05 Content" formats if code =~ /^(\d)\.(\d{2})/ string_to_numeric("#{::Regexp.last_match(1)}.#{::Regexp.last_match(2)}") else Registries::Method.value_for(code.downcase.to_sym) || Registries::Response.value_for(code.downcase.to_sym) || 0 end else 0 end end |
.to_string(code) ⇒ String
Convert a code to its human-readable string representation
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/takagi/coap/code_helpers.rb', line 19 def self.to_string(code) case code when Integer # Try method registry first if code < 32 Registries::Method.name_for(code) || numeric_to_string(code) # Try signaling registry for 7.xx codes elsif code >= 224 Registries::Signaling.name_for(code) || numeric_to_string(code) # Try response registry elsif code >= 64 Registries::Response.name_for(code) || numeric_to_string(code) else numeric_to_string(code) end when Symbol # Try method registry Registries::Method.value_for(code)&.then { |v| Registries::Method.name_for(v) } || # Try response registry Registries::Response.value_for(code)&.then { |v| Registries::Response.name_for(v) } || code.to_s when String # If already in dotted format, try to lookup if code =~ /^(\d)\.(\d{2})$/ val = string_to_numeric(code) Registries::Response.name_for(val) || code else code end else code.to_s end end |