Class: Takagi::Message::Inbound
- Defined in:
- lib/takagi/message/inbound.rb
Overview
Class for inbound message that is coming to server
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#response_code ⇒ Object
readonly
Returns the value of attribute response_code.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Attributes inherited from Base
#code, #message_id, #options, #payload, #token, #type, #version
Instance Method Summary collapse
-
#accept ⇒ Integer?
Get Accept option.
-
#accept?(format) ⇒ Boolean
Check if request accepts a specific content format.
-
#content_format ⇒ Integer?
Get Content-Format option.
-
#delete? ⇒ Boolean
Check if request is a DELETE.
-
#get? ⇒ Boolean
Check if request is a GET.
-
#initialize(data, transport: :udp) ⇒ Inbound
constructor
A new instance of Inbound.
-
#observe? ⇒ Boolean
Check if request is an OBSERVE.
-
#option(option_number) ⇒ Object?
Get CoAP option by number.
-
#option?(option_number) ⇒ Boolean
Check if request has a specific CoAP option.
- #parse_coap_uri ⇒ Object
-
#post? ⇒ Boolean
Check if request is a POST.
-
#put? ⇒ Boolean
Check if request is a PUT.
-
#query_params ⇒ Hash<String, String>
Get query parameters as a hash.
- #to_response(code, payload, options: {}) ⇒ Object
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
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/takagi/message/inbound.rb', line 7 def method @method end |
#response_code ⇒ Object (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 |
#uri ⇒ Object (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
#accept ⇒ Integer?
Get Accept option
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
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_format ⇒ Integer?
Get Content-Format option
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
123 124 125 |
# File 'lib/takagi/message/inbound.rb', line 123 def delete? method == 'DELETE' end |
#get? ⇒ Boolean
Check if request is a GET
105 106 107 |
# File 'lib/takagi/message/inbound.rb', line 105 def get? method == 'GET' end |
#observe? ⇒ Boolean
Check if request is an OBSERVE
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
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
66 67 68 |
# File 'lib/takagi/message/inbound.rb', line 66 def option?(option_number) @options.key?(option_number) end |
#parse_coap_uri ⇒ Object
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 || {} @logger.debug "Options received by parse_coap_uri: #{.inspect}" host = [CoAP::Registries::Option::URI_HOST] || 'localhost' path_segments = Array([CoAP::Registries::Option::URI_PATH]).flatten query_segments = Array([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
111 112 113 |
# File 'lib/takagi/message/inbound.rb', line 111 def post? method == 'POST' end |
#put? ⇒ Boolean
Check if request is a PUT
117 118 119 |
# File 'lib/takagi/message/inbound.rb', line 117 def put? method == 'PUT' end |
#query_params ⇒ Hash<String, String>
Get query parameters as a hash
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: , transport: @transport ) end |