Class: KStor::Message::Base
- Inherits:
-
Object
- Object
- KStor::Message::Base
- Defined in:
- lib/kstor/message/base.rb
Overview
A user request or response.
Direct Known Subclasses
Error, GroupCreate, GroupCreated, Ping, Pong, SecretCreate, SecretCreated, SecretDelete, SecretDeleted, SecretList, SecretSearch, SecretUnlock, SecretUpdateMeta, SecretUpdateValue, SecretUpdated, SecretValue
Class Attribute Summary collapse
-
.arg_names ⇒ Object
readonly
Returns the value of attribute arg_names.
-
.registry ⇒ Object
readonly
Returns the value of attribute registry.
-
.request ⇒ Object
readonly
Returns the value of attribute request.
-
.type ⇒ Object
readonly
Returns the value of attribute type.
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Class Method Summary collapse
-
.arg(name) ⇒ Object
Declare an argument to this message type.
-
.for_type(name, args, auth) ⇒ Object
Create a new message of the given type.
-
.message_type(name, request: nil, response: nil) ⇒ Object
Declare message type and direction (request or response).
-
.parse(str) ⇒ Object
Parse message.
-
.register(klass) ⇒ Object
Register new message type.
-
.type?(name) ⇒ Boolean
True if message type “name” is known.
-
.types ⇒ Object
List of known types.
Instance Method Summary collapse
-
#error? ⇒ Boolean
True if this message is an error response.
-
#initialize(args, auth = {}) ⇒ KStor::Message::Base
constructor
Create new message.
-
#inspect ⇒ Object
Hide sensitive information when debugging.
-
#login ⇒ Object
User login.
-
#login_request? ⇒ Boolean
True if this message is a request and has login and password arguments.
-
#password ⇒ Object
User password.
-
#request? ⇒ Boolean
True if this message is a request.
-
#response? ⇒ Boolean
True if this message is a response.
-
#serialize ⇒ String
Serialize this message to JSON.
-
#session_id ⇒ Object
User session ID.
-
#session_request? ⇒ Boolean
True if this message is a request and has a session ID.
-
#to_h ⇒ Hash
Convert this message to a Hash.
-
#type ⇒ Object
Message type.
Constructor Details
#initialize(args, auth = {}) ⇒ KStor::Message::Base
Create new message.
15 16 17 18 |
# File 'lib/kstor/message/base.rb', line 15 def initialize(args, auth = {}) @args = check_args!(args) @auth = check_auth!(auth.compact) end |
Class Attribute Details
.arg_names ⇒ Object (readonly)
Returns the value of attribute arg_names.
102 103 104 |
# File 'lib/kstor/message/base.rb', line 102 def arg_names @arg_names end |
.registry ⇒ Object (readonly)
Returns the value of attribute registry.
101 102 103 |
# File 'lib/kstor/message/base.rb', line 101 def registry @registry end |
.request ⇒ Object (readonly)
Returns the value of attribute request.
100 101 102 |
# File 'lib/kstor/message/base.rb', line 100 def request @request end |
.type ⇒ Object (readonly)
Returns the value of attribute type.
99 100 101 |
# File 'lib/kstor/message/base.rb', line 99 def type @type end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
8 9 10 |
# File 'lib/kstor/message/base.rb', line 8 def args @args end |
Class Method Details
.arg(name) ⇒ Object
Declare an argument to this message type.
115 116 117 118 119 120 121 |
# File 'lib/kstor/message/base.rb', line 115 def arg(name) @arg_names ||= [] @arg_names << name.to_s define_method(name) do @args[name.to_s] end end |
.for_type(name, args, auth) ⇒ Object
Create a new message of the given type
124 125 126 127 128 129 |
# File 'lib/kstor/message/base.rb', line 124 def for_type(name, args, auth) klass = @registry[name.to_sym] raise "unknown message type #{name.inspect}" unless klass klass.new(args, auth) end |
.message_type(name, request: nil, response: nil) ⇒ Object
Declare message type and direction (request or response).
105 106 107 108 109 110 111 112 |
# File 'lib/kstor/message/base.rb', line 105 def (name, request: nil, response: nil) @type = name if (request && response) || (!request && !response) raise 'Is it a request or a response type?!?' end @request = !!request end |
.parse(str) ⇒ Object
Parse message.
142 143 144 145 146 147 148 149 150 |
# File 'lib/kstor/message/base.rb', line 142 def parse(str) data = JSON.parse(str) type = data.delete('type').to_sym args = data.delete('args').transform_keys(&:to_s) auth = data.transform_keys(&:to_sym) for_type(type, args, auth) rescue JSON::ParserError raise UnparsableResponse end |
.register(klass) ⇒ Object
Register new message type.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/kstor/message/base.rb', line 153 def register(klass) @registry ||= {} unless klass.respond_to?(:type) && klass.respond_to?(:request) raise "#{klass} is not a subclass of #{self}" end if @registry.key?(klass.type) = subclass.type old_klass = @registry[] raise "duplicate message type #{} in #{klass}, " \ "already defined in #{old_klass}" end @registry[klass.type] = klass end |
.type?(name) ⇒ Boolean
True if message type “name” is known.
132 133 134 |
# File 'lib/kstor/message/base.rb', line 132 def type?(name) @registry.key?(name.to_sym) end |
.types ⇒ Object
List of known types.
137 138 139 |
# File 'lib/kstor/message/base.rb', line 137 def types @registry.types end |
Instance Method Details
#error? ⇒ Boolean
True if this message is an error response.
51 52 53 |
# File 'lib/kstor/message/base.rb', line 51 def error? response? && type == 'error' end |
#inspect ⇒ Object
Hide sensitive information when debugging
81 82 83 84 85 86 87 88 89 |
# File 'lib/kstor/message/base.rb', line 81 def inspect if login_request? inspect_login_request elsif session_request? || response? inspect_session_request_or_response else raise 'WTFBBQ?!???1!11!' end end |
#login ⇒ Object
User login
26 27 28 |
# File 'lib/kstor/message/base.rb', line 26 def login @auth[:login] end |
#login_request? ⇒ Boolean
True if this message is a request and has login and password arguments.
56 57 58 |
# File 'lib/kstor/message/base.rb', line 56 def login_request? request? && @auth.key?(:login) && @auth.key?(:password) end |
#password ⇒ Object
User password
31 32 33 |
# File 'lib/kstor/message/base.rb', line 31 def password @auth[:password] end |
#request? ⇒ Boolean
True if this message is a request.
41 42 43 |
# File 'lib/kstor/message/base.rb', line 41 def request? self.class.request end |
#response? ⇒ Boolean
True if this message is a response.
46 47 48 |
# File 'lib/kstor/message/base.rb', line 46 def response? !request? end |
#serialize ⇒ String
Serialize this message to JSON.
94 95 96 |
# File 'lib/kstor/message/base.rb', line 94 def serialize to_h.to_json end |
#session_id ⇒ Object
User session ID
36 37 38 |
# File 'lib/kstor/message/base.rb', line 36 def session_id @auth[:session_id] end |
#session_request? ⇒ Boolean
True if this message is a request and has a session ID.
61 62 63 |
# File 'lib/kstor/message/base.rb', line 61 def session_request? request? && @auth.key?(:session_id) end |
#to_h ⇒ Hash
Convert this message to a Hash
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/kstor/message/base.rb', line 68 def to_h h = { 'type' => type, 'args' => @args } if login_request? h['login'] = @auth[:login] h['password'] = @auth[:password] elsif session_request? || response? h['session_id'] = @auth[:session_id] end h end |
#type ⇒ Object
Message type.
21 22 23 |
# File 'lib/kstor/message/base.rb', line 21 def type self.class.type end |