Class: Takagi::Message::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/message/base.rb

Overview

Base class for message

Direct Known Subclasses

Inbound, Outbound, Request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, transport: :udp) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/takagi/message/base.rb', line 9

def initialize(data = nil, transport: :udp)
  @transport = transport
  if data.is_a?(String) || data.is_a?(IO)
    case transport
    when :tcp
      parse_tcp(data)
    else
      parse(data)
    end
  end
  @data = data
  @logger = Takagi.logger
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/takagi/message/base.rb', line 7

def code
  @code
end

#message_idObject (readonly)

Returns the value of attribute message_id.



7
8
9
# File 'lib/takagi/message/base.rb', line 7

def message_id
  @message_id
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/takagi/message/base.rb', line 7

def options
  @options
end

#payloadObject (readonly)

Returns the value of attribute payload.



7
8
9
# File 'lib/takagi/message/base.rb', line 7

def payload
  @payload
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/takagi/message/base.rb', line 7

def token
  @token
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/takagi/message/base.rb', line 7

def type
  @type
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/takagi/message/base.rb', line 7

def version
  @version
end

Instance Method Details

#coap_code_to_method(code) ⇒ String

Convert CoAP code number to method name using registry

Parameters:

  • code (Integer)

    CoAP code number

Returns:

  • (String)

    Method name (e.g., ‘GET’, ‘OBSERVE’)



26
27
28
29
30
31
32
33
34
# File 'lib/takagi/message/base.rb', line 26

def coap_code_to_method(code)
  # Check if it's an OBSERVE request (GET with Observe option)
  if code == CoAP::Registries::Method::GET && @options && @options[CoAP::Registries::Option::OBSERVE]
    'OBSERVE'
  else
    # Use CoAP registry to convert code to string
    CoAP::CodeHelpers.to_string(code)
  end
end

#coap_method_to_code(method) ⇒ Integer

Convert method name to CoAP code number using registry

Parameters:

  • method (String, Symbol)

    Method name (e.g., ‘GET’, :post)

Returns:

  • (Integer)

    CoAP code number



39
40
41
# File 'lib/takagi/message/base.rb', line 39

def coap_method_to_code(method)
  CoAP::CodeHelpers.to_numeric(method)
end