Class: PalavaMachine::ClientMessage
- Inherits:
-
Object
- Object
- PalavaMachine::ClientMessage
- Defined in:
- lib/palava_machine/client_message.rb
Constant Summary collapse
- RULES =
{ info: [], join_room: [:room_id, :status], leave_room: [], send_to_peer: [:peer_id, :data], update_status: [:status], }
Instance Method Summary collapse
- #[](w) ⇒ Object
- #arguments ⇒ Object
- #connection_id ⇒ Object
-
#initialize(message, connection_id = nil) ⇒ ClientMessage
constructor
A new instance of ClientMessage.
- #name ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(message, connection_id = nil) ⇒ ClientMessage
Returns a new instance of ClientMessage.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/palava_machine/client_message.rb', line 15 def initialize(, connection_id = nil) begin @_data = JSON.parse() rescue # TODO find exact json error to catch raise MessageParsingError, 'invalid message' end raise MessageParsingError, 'invalid message: not a hash' unless @_data.instance_of?(Hash) @connection_id = connection_id end |
Instance Method Details
#[](w) ⇒ Object
26 27 28 |
# File 'lib/palava_machine/client_message.rb', line 26 def [](w) @_data[w] end |
#arguments ⇒ Object
42 43 44 |
# File 'lib/palava_machine/client_message.rb', line 42 def arguments valid? && RULES[name].map{ |data_key| @_data[data_key.to_s] } end |
#connection_id ⇒ Object
38 39 40 |
# File 'lib/palava_machine/client_message.rb', line 38 def connection_id @connection_id or raise MessageParsingError, 'connection id used but not set' end |
#name ⇒ Object
34 35 36 |
# File 'lib/palava_machine/client_message.rb', line 34 def name @name ||= @_data['event'] && @_data['event'].to_sym or raise(MessageParsingError, 'no event given') end |
#valid? ⇒ Boolean
30 31 32 |
# File 'lib/palava_machine/client_message.rb', line 30 def valid? RULES.keys.include?(name) or raise MessageParsingError, 'unknown event' end |