Class: KStor::Message::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kstor/message/base.rb

Overview

A user request or response.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, auth = {}) ⇒ KStor::Message::Base

Create new message.

Parameters:

  • args (Hash)

    message arguments

  • auth (Hash) (defaults to: {})

    authentication data



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_namesObject (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

.registryObject (readonly)

Returns the value of attribute registry.



101
102
103
# File 'lib/kstor/message/base.rb', line 101

def registry
  @registry
end

.requestObject (readonly)

Returns the value of attribute request.



100
101
102
# File 'lib/kstor/message/base.rb', line 100

def request
  @request
end

.typeObject (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

#argsObject (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 message_type(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)
    message_type = subclass.type
    old_klass = @registry[message_type]
    raise "duplicate message type #{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.

Returns:

  • (Boolean)


132
133
134
# File 'lib/kstor/message/base.rb', line 132

def type?(name)
  @registry.key?(name.to_sym)
end

.typesObject

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.

Returns:

  • (Boolean)


51
52
53
# File 'lib/kstor/message/base.rb', line 51

def error?
  response? && type == 'error'
end

#inspectObject

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 
    
  elsif session_request? || response?
    inspect_session_request_or_response
  else
    raise 'WTFBBQ?!???1!11!'
  end
end

#loginObject

User login



26
27
28
# File 'lib/kstor/message/base.rb', line 26

def 
  @auth[:login]
end

#login_request?Boolean

True if this message is a request and has login and password arguments.

Returns:

  • (Boolean)


56
57
58
# File 'lib/kstor/message/base.rb', line 56

def 
  request? && @auth.key?(:login) && @auth.key?(:password)
end

#passwordObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


46
47
48
# File 'lib/kstor/message/base.rb', line 46

def response?
  !request?
end

#serializeString

Serialize this message to JSON.

Returns:

  • (String)

    JSON data.



94
95
96
# File 'lib/kstor/message/base.rb', line 94

def serialize
  to_h.to_json
end

#session_idObject

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.

Returns:

  • (Boolean)


61
62
63
# File 'lib/kstor/message/base.rb', line 61

def session_request?
  request? && @auth.key?(:session_id)
end

#to_hHash

Convert this message to a Hash

Returns:

  • (Hash)

    this message as 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 
    h['login'] = @auth[:login]
    h['password'] = @auth[:password]
  elsif session_request? || response?
    h['session_id'] = @auth[:session_id]
  end

  h
end

#typeObject

Message type.



21
22
23
# File 'lib/kstor/message/base.rb', line 21

def type
  self.class.type
end