Module: MaxCube::Messages::Parser

Includes:
Handler
Included in:
TCP::Parser, UDP::Parser
Defined in:
lib/maxcube/messages/parser.rb

Constant Summary

Constants included from MaxCube::Messages

DAYS_OF_WEEK, DEVICE_MODE, DEVICE_TYPE, PACK_FORMAT

Instance Method Summary collapse

Methods included from Handler

#check_data_type, #check_hash, #check_hash_keys, #check_hash_msg_type, #check_hash_values, #check_msg, #check_msg_msg_type, #check_msg_type, #msg_type_hash_keys, #msg_type_hash_opt_keys, #valid_data_type, #valid_hash, #valid_hash_keys, #valid_hash_msg_type, #valid_hash_values, #valid_msg, #valid_msg_msg_type, #valid_msg_type

Instance Method Details

#parse_msg_body(body, hash, parser_type_str) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/maxcube/messages/parser.rb', line 21

def parse_msg_body(body, hash, parser_type_str)
  method_str = "parse_#{parser_type_str}_#{@msg_type.downcase}"
  if respond_to?(method_str, true)
    return hash.merge!(send(method_str, body))
  end
  hash[:data] = body
  nil
rescue IOError
  raise InvalidMessageBody
    .new(@msg_type, 'unexpected EOF reached')
end

#read(count = 0, unpack = false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/maxcube/messages/parser.rb', line 8

def read(count = 0, unpack = false)
  str = if count.zero?
          @io.read
        else
          raise IOError if @io.size - @io.pos < count
          @io.read(count)
        end
  return str unless unpack
  str = "\x00".b + str if count == 3
  unpack = PACK_FORMAT[count] unless unpack.is_a?(String)
  str.unpack1(unpack)
end