Module: MaxCube::Messages::Handler

Includes:
MaxCube::Messages
Included in:
Parser, Serializer, TCP::Handler, UDP::Handler
Defined in:
lib/maxcube/messages/handler.rb

Overview

This module provides methods that handles with messages regardless whether it is for parse or serialize purposes. It mostly contains methods that validates (returns Boolean) or checks (raises exception on failure) some part of message.

Constant Summary collapse

PACK_FORMAT =

Format characters to String#unpack and Array#pack, for purposes to convert binary string to integer. Elements are sorted by integer size in bytes.

%w[x C n N N].freeze

Constants included from MaxCube::Messages

DAYS_OF_WEEK, DEVICE_MODE, DEVICE_TYPE

Instance Method Summary collapse

Instance Method Details

#check_data_type(raw_data) ⇒ String

Checks whether #valid_data_type is true. If not, exception is raised.

Returns:

  • (String)

    input raw data string.

Raises:

  • (TypeError)

    if argument is not valid.



31
32
33
34
# File 'lib/maxcube/messages/handler.rb', line 31

def check_data_type(raw_data)
  raise TypeError unless valid_data_type(raw_data)
  raw_data
end

#check_hash(hash) ⇒ Hash

As #valid_hash, but raises exception if hash is not valid. Calls #check_hash_msg_type, #check_hash_keys and #check_hash_values.

Parameters:

  • hash (Hash)

    input hash.

Returns:

  • (Hash)

    input hash.



175
176
177
178
179
180
# File 'lib/maxcube/messages/handler.rb', line 175

def check_hash(hash)
  check_hash_msg_type(hash)
  check_hash_keys(hash)
  check_hash_values(hash)
  hash
end

#check_hash_keys(hash) ⇒ Hash

As #valid_hash_keys, but raises exception if hash is not valid. Calls #maybe_check_valid_hash_keys.

Parameters:

  • hash (Hash)

    input hash.

Returns:

  • (Hash)

    input hash.



130
131
132
133
# File 'lib/maxcube/messages/handler.rb', line 130

def check_hash_keys(hash)
  maybe_check_valid_hash_keys(hash, true)
  hash
end

#check_hash_msg_type(hash) ⇒ Object

Checks whether message type character in hash is valid. Calls #check_msg_type. If argument is valid, it assigns the message type to internal variable.

Parameters:

  • hash (Hash)

    input hash.



98
99
100
# File 'lib/maxcube/messages/handler.rb', line 98

def check_hash_msg_type(hash)
  check_msg_type(hash[:type])
end

#check_hash_values(hash) ⇒ Hash

As #valid_hash_values, but raises exception if hash values are not valid.

Parameters:

  • hash (Hash)

    input hash.

Returns:

  • (Hash)

    input hash.

Raises:



149
150
151
152
153
154
155
# File 'lib/maxcube/messages/handler.rb', line 149

def check_hash_values(hash)
  return hash if valid_hash_values(hash)
  hash = hash.dup
  hash.delete(:type)
  raise InvalidMessageBody
    .new(@msg_type, "invalid hash values: #{hash}")
end

#check_msg(msg) ⇒ Object

As #valid_msg, but raises exception if message is invalid. Currently, it just calls #check_msg_msg_type.

Parameters:

  • msg (String)

    input message.



83
84
85
# File 'lib/maxcube/messages/handler.rb', line 83

def check_msg(msg)
  check_msg_msg_type(msg)
end

#check_msg_msg_type(msg) ⇒ Object

Checks whether message type character of message is valid. Calls #check_msg_type and #msg_msg_type (this method depends on conrete end-class). If argument is valid, it assigns the message type to internal variable.

Parameters:

  • msg (String)

    input message.



68
69
70
# File 'lib/maxcube/messages/handler.rb', line 68

def check_msg_msg_type(msg)
  check_msg_type(msg_msg_type(msg))
end

#check_msg_type(msg_type) ⇒ String

Checks whether message type character is valid. Calls #maybe_check_valid_msg_type. If argument is valid, it assigns the message type to internal variable.

Parameters:

  • msg_type (String)

    input message type character.

Returns:

  • (String)

    message type character assigned to internal variable.

Raises:



50
51
52
53
# File 'lib/maxcube/messages/handler.rb', line 50

def check_msg_type(msg_type)
  maybe_check_valid_msg_type(msg_type, true)
  @msg_type
end

#msg_type_hash_keys(msg_type) ⇒ Object

Returns hash keys that are related to given message type. Each hash with a message type should contain these keys. Calls #msg_type_which_hash_keys.

Parameters:

  • msg_type (String)

    message type that is being parsed/serialized.



106
107
108
# File 'lib/maxcube/messages/handler.rb', line 106

def msg_type_hash_keys(msg_type)
  msg_type_which_hash_keys(msg_type, false)
end

#msg_type_hash_opt_keys(msg_type) ⇒ Object

Returns optional hash keys that are related to given message type. Calls #msg_type_which_hash_keys.

Parameters:

  • msg_type (String)

    message type that is being parsed/serialized.



113
114
115
# File 'lib/maxcube/messages/handler.rb', line 113

def msg_type_hash_opt_keys(msg_type)
  msg_type_which_hash_keys(msg_type, true)
end

#valid_data_type(raw_data) ⇒ Boolean

Checks whether raw data string is String.

Parameters:

  • raw_data

    input raw data string.

Returns:

  • (Boolean)

    true if argument is String, false otherwise.



23
24
25
# File 'lib/maxcube/messages/handler.rb', line 23

def valid_data_type(raw_data)
  raw_data.is_a?(String)
end

#valid_hash(hash) ⇒ Boolean

Validates if given hash satisfies basic conditions for all hashes being used with messages. Calls #valid_hash_msg_type, #valid_hash_keys and #valid_hash_values.

Parameters:

  • hash (Hash)

    input hash.

Returns:

  • (Boolean)

    true if hash is valid (from general point of view).



164
165
166
167
168
# File 'lib/maxcube/messages/handler.rb', line 164

def valid_hash(hash)
  valid_hash_msg_type(hash) &&
    valid_hash_keys(hash) &&
    valid_hash_values(hash)
end

#valid_hash_keys(hash) ⇒ Object

Validates if given hash contain valid keys, depending on its message type (according to #msg_type_which_hash_keys). Calls #maybe_check_valid_hash_keys.

Parameters:

  • hash (Hash)

    input hash.



122
123
124
# File 'lib/maxcube/messages/handler.rb', line 122

def valid_hash_keys(hash)
  maybe_check_valid_hash_keys(hash, false)
end

#valid_hash_msg_type(hash) ⇒ Object

Validates whether message type character in hash is valid. Calls #valid_msg_type.

Parameters:

  • hash (Hash)

    input hash.



90
91
92
# File 'lib/maxcube/messages/handler.rb', line 90

def valid_hash_msg_type(hash)
  valid_msg_type(hash[:type])
end

#valid_hash_values(hash) ⇒ Boolean

Validates if values of given hash satisfies basic conditions for all hashes being used with messages.

Parameters:

  • hash (Hash)

    input hash.

Returns:

  • (Boolean)

    true if hash values are valid (from general point of view).



140
141
142
# File 'lib/maxcube/messages/handler.rb', line 140

def valid_hash_values(hash)
  hash.none? { |_, v| v.nil? }
end

#valid_msg(msg) ⇒ Object

Validates whether whole message is valid from general point of view, independently of parser/sserializer. Currently, it just calls #valid_msg_msg_type.

Parameters:

  • msg (String)

    input message.



76
77
78
# File 'lib/maxcube/messages/handler.rb', line 76

def valid_msg(msg)
  valid_msg_msg_type(msg)
end

#valid_msg_msg_type(msg) ⇒ Object

Validates whether message type character of message is valid. Calls #valid_msg_type and #msg_msg_type (this method depends on conrete end-class).

Parameters:

  • msg (String)

    input message.



59
60
61
# File 'lib/maxcube/messages/handler.rb', line 59

def valid_msg_msg_type(msg)
  valid_msg_type(msg_msg_type(msg))
end

#valid_msg_type(msg_type) ⇒ Boolean

Validates whether message type character is valid. Calls #maybe_check_valid_msg_type.

Parameters:

  • msg_type (String)

    input message type character.

Returns:

  • (Boolean)

    true if argument is valid message type.



40
41
42
# File 'lib/maxcube/messages/handler.rb', line 40

def valid_msg_type(msg_type)
  maybe_check_valid_msg_type(msg_type, false)
end