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
-
#check_data_type(raw_data) ⇒ String
Checks whether #valid_data_type is
true
. -
#check_hash(hash) ⇒ Hash
As #valid_hash, but raises exception if hash is not valid.
-
#check_hash_keys(hash) ⇒ Hash
As #valid_hash_keys, but raises exception if hash is not valid.
-
#check_hash_msg_type(hash) ⇒ Object
Checks whether message type character in hash is valid.
-
#check_hash_values(hash) ⇒ Hash
As #valid_hash_values, but raises exception if hash values are not valid.
-
#check_msg(msg) ⇒ Object
As #valid_msg, but raises exception if message is invalid.
-
#check_msg_msg_type(msg) ⇒ Object
Checks whether message type character of message is valid.
-
#check_msg_type(msg_type) ⇒ String
Checks whether message type character is valid.
-
#msg_type_hash_keys(msg_type) ⇒ Object
Returns hash keys that are related to given message type.
-
#msg_type_hash_opt_keys(msg_type) ⇒ Object
Returns optional hash keys that are related to given message type.
-
#valid_data_type(raw_data) ⇒ Boolean
Checks whether raw data string is
String
. -
#valid_hash(hash) ⇒ Boolean
Validates if given hash satisfies basic conditions for all hashes being used with messages.
-
#valid_hash_keys(hash) ⇒ Object
Validates if given hash contain valid keys, depending on its message type (according to #msg_type_which_hash_keys).
-
#valid_hash_msg_type(hash) ⇒ Object
Validates whether message type character in hash is valid.
-
#valid_hash_values(hash) ⇒ Boolean
Validates if values of given hash satisfies basic conditions for all hashes being used with messages.
-
#valid_msg(msg) ⇒ Object
Validates whether whole message is valid from general point of view, independently of parser/sserializer.
-
#valid_msg_msg_type(msg) ⇒ Object
Validates whether message type character of message is valid.
-
#valid_msg_type(msg_type) ⇒ Boolean
Validates whether message type character is valid.
Instance Method Details
#check_data_type(raw_data) ⇒ String
Checks whether #valid_data_type is true
. If not, exception is raised.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
.
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.
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.
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.
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.
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.
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).
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.
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 |