Class: KStor::Message
- Inherits:
-
Object
- Object
- KStor::Message
- Defined in:
- lib/kstor/message.rb
Overview
A user request or response.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.parse_request(str) ⇒ LoginRequest, SessionRequest
Parse a user request, freshly arrived from the network.
Instance Method Summary collapse
-
#initialize(type, args) ⇒ Message
constructor
A new instance of Message.
- #serialize ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(type, args) ⇒ Message
Returns a new instance of Message.
22 23 24 25 |
# File 'lib/kstor/message.rb', line 22 def initialize(type, args) @type = type @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
20 21 22 |
# File 'lib/kstor/message.rb', line 20 def args @args end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
19 20 21 |
# File 'lib/kstor/message.rb', line 19 def type @type end |
Class Method Details
.parse_request(str) ⇒ LoginRequest, SessionRequest
Parse a user request, freshly arrived from the network.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/kstor/message.rb', line 40 def self.parse_request(str) data = JSON.parse(str) if data.key?('login') && data.key?('password') LoginRequest.new( data['login'], data['password'], data['type'], data['args'] ) elsif data.key?('session_id') SessionRequest.new( data['session_id'], data['type'], data['args'] ) else raise RequestMissesAuthData end end |
Instance Method Details
#serialize ⇒ Object
31 32 33 |
# File 'lib/kstor/message.rb', line 31 def serialize to_h.to_json end |
#to_h ⇒ Object
27 28 29 |
# File 'lib/kstor/message.rb', line 27 def to_h { 'type' => @type, 'args' => @args } end |