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

Class Method Details

.allHash

Get all registered codes across all registries

Returns:

  • (Hash)

    Map of value => name for all codes



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

Parameters:

  • code (Integer, String, Symbol)

    Code to check

Returns:

  • (Boolean)

    true if client error (4.xx)



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)

Parameters:

  • code (Integer)

    Numeric code

Returns:

  • (Symbol)

    :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

Parameters:

  • code (Integer, String, Symbol)

    Code to check

Returns:

  • (Boolean)

    true if error code (4.xx or 5.xx)



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

Parameters:

  • code (Integer)

    Numeric code

Returns:

  • (String, nil)

    RFC reference



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

Parameters:

  • code (Integer, String, Symbol)

    Code to lookup

Returns:

  • (Hash, nil)

    Registry information



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”)

Parameters:

  • code (Integer)

    Numeric code

Returns:

  • (String)

    Dotted format string



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

Parameters:

  • code (Integer, String, Symbol)

    Code to check

Returns:

  • (Boolean)

    true if server error (5.xx)



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)

Parameters:

  • code_string (String)

    Dotted format string

Returns:

  • (Integer)

    Numeric code



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

Parameters:

  • code (Integer, String, Symbol)

    Code to check

Returns:

  • (Boolean)

    true if success code (2.xx)



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

Examples:

CodeHelpers.to_numeric(:get)       # => 1
CodeHelpers.to_numeric("2.05")     # => 69
CodeHelpers.to_numeric("2.05 Content")   # => 69
CodeHelpers.to_numeric(:content)   # => 69

Parameters:

  • code (Integer, String, Symbol)

    Code to convert

Returns:

  • (Integer)

    Numeric code



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

Examples:

CodeHelpers.to_string(69)      # => "2.05 Content"
CodeHelpers.to_string(:get)    # => "GET"
CodeHelpers.to_string("2.05")  # => "2.05 Content"

Parameters:

  • code (Integer, String, Symbol)

    Code to convert

Returns:

  • (String)

    Human-readable code string



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