Class: Takagi::Message::Inbound

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

Overview

Class for inbound message that is coming to server

Instance Attribute Summary collapse

Attributes inherited from Base

#code, #message_id, #options, #payload, #token, #type, #version

Instance Method Summary collapse

Methods inherited from Base

#coap_code_to_method, #coap_method_to_code

Constructor Details

#initialize(data, transport: :udp) ⇒ Inbound

Returns a new instance of Inbound.



9
10
11
12
13
14
15
16
# File 'lib/takagi/message/inbound.rb', line 9

def initialize(data, transport: :udp)
  super(data, transport: transport)
  @method = coap_code_to_method(@code)
  @response_code = coap_code_to_method(@code) if @code >= CoAP::Registries::Response::CREATED # Response
  @uri = parse_coap_uri
  @logger.debug "CoAP Options: #{@options.inspect}"
  @logger.debug "Parsed CoAP URI: #{@uri}"
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#response_codeObject (readonly)

Returns the value of attribute response_code.



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

def response_code
  @response_code
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#acceptInteger?

Get Accept option

Returns:

  • (Integer, nil)

    The Accept content format



72
73
74
# File 'lib/takagi/message/inbound.rb', line 72

def accept
  option(CoAP::Registries::Option::ACCEPT)
end

#accept?(format) ⇒ Boolean

Check if request accepts a specific content format

Parameters:

  • format (String, Integer)

    Format name or number

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/takagi/message/inbound.rb', line 79

def accept?(format)
  return false unless accept

  format_number = format.is_a?(Integer) ? format : content_format_to_number(format)
  accept == format_number
end

#content_formatInteger?

Get Content-Format option

Returns:

  • (Integer, nil)

    The Content-Format



88
89
90
# File 'lib/takagi/message/inbound.rb', line 88

def content_format
  option(CoAP::Registries::Option::CONTENT_FORMAT)
end

#delete?Boolean

Check if request is a DELETE

Returns:

  • (Boolean)


123
124
125
# File 'lib/takagi/message/inbound.rb', line 123

def delete?
  method == 'DELETE'
end

#get?Boolean

Check if request is a GET

Returns:

  • (Boolean)


105
106
107
# File 'lib/takagi/message/inbound.rb', line 105

def get?
  method == 'GET'
end

#observe?Boolean

Check if request is an OBSERVE

Returns:

  • (Boolean)


129
130
131
# File 'lib/takagi/message/inbound.rb', line 129

def observe?
  method == 'OBSERVE'
end

#option(option_number) ⇒ Object?

Get CoAP option by number

Parameters:

  • option_number (Integer)

    The CoAP option number

Returns:

  • (Object, nil)

    The option value



59
60
61
# File 'lib/takagi/message/inbound.rb', line 59

def option(option_number)
  @options[option_number]
end

#option?(option_number) ⇒ Boolean

Check if request has a specific CoAP option

Parameters:

  • option_number (Integer)

    The CoAP option number

Returns:

  • (Boolean)


66
67
68
# File 'lib/takagi/message/inbound.rb', line 66

def option?(option_number)
  @options.key?(option_number)
end

#parse_coap_uriObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/takagi/message/inbound.rb', line 41

def parse_coap_uri
  options = @options || {}
  @logger.debug "Options received by parse_coap_uri: #{options.inspect}"

  host = options[CoAP::Registries::Option::URI_HOST] || 'localhost'
  path_segments = Array(options[CoAP::Registries::Option::URI_PATH]).flatten
  query_segments = Array(options[CoAP::Registries::Option::URI_QUERY]).flatten

  path = path_segments.empty? ? '/' : "/#{path_segments.join('/')}"
  query = query_segments.empty? ? nil : query_segments.join('&')
  URI::Generic.build(scheme: 'coap', host: host, path: path, query: query)
end

#post?Boolean

Check if request is a POST

Returns:

  • (Boolean)


111
112
113
# File 'lib/takagi/message/inbound.rb', line 111

def post?
  method == 'POST'
end

#put?Boolean

Check if request is a PUT

Returns:

  • (Boolean)


117
118
119
# File 'lib/takagi/message/inbound.rb', line 117

def put?
  method == 'PUT'
end

#query_paramsHash<String, String>

Get query parameters as a hash

Returns:

  • (Hash<String, String>)


94
95
96
97
98
99
100
101
# File 'lib/takagi/message/inbound.rb', line 94

def query_params
  return {} unless @uri.query

  @uri.query.split('&').each_with_object({}) do |param, hash|
    key, value = param.split('=', 2)
    hash[key] = value || ''
  end
end

#to_response(code, payload, options: {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/takagi/message/inbound.rb', line 18

def to_response(code, payload, options: {})
  # For TCP transport, type is not used (RFC 8323)
  response_type = if @transport == :tcp
                    0  # No type field in TCP CoAP
                  else
                    case @type
                    when CoAP::Registries::MessageType::CON then CoAP::Registries::MessageType::ACK  # CON → ACK
                    when CoAP::Registries::MessageType::NON then CoAP::Registries::MessageType::NON  # NON → NON
                    else CoAP::Registries::MessageType::RST # fallback → RST
                    end
                  end

  Outbound.new(
    code: code,
    payload: payload,
    token: @token,
    message_id: @message_id,
    type: response_type,
    options: options,
    transport: @transport
  )
end