Class: Messaging::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/rdb/messaging.rb

Class Method Summary collapse

Class Method Details

.broadcast(message, params) ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'lib/rdb/messaging.rb', line 257

def self.broadcast(message, params)
  obj = {
    type: :broadcast,
    message: message,
    params: params
  }

  MessagePack.pack(obj)
end

.command(id, command, params) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/rdb/messaging.rb', line 236

def self.command(id, command, params)
  obj = {
    type: :command,
    id: id,
    command: command,
    params: params
  }

  MessagePack.pack(obj)
end

.result(id, result) ⇒ Object



247
248
249
250
251
252
253
254
255
# File 'lib/rdb/messaging.rb', line 247

def self.result(id, result)
  obj = {
    type: :result,
    id: id,
    result: result
  }

  MessagePack.pack(obj)
end

.unpack(payload) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rdb/messaging.rb', line 267

def self.unpack(payload)
  obj = MessagePack.unpack(payload)
  type = obj['type'].to_sym

  id = obj['id']

  params = nil
  if obj['params']
    params = Hash[obj['params'].map { |k, v| [k.to_sym, v] }]
  end

  case type
  when :broadcast
    { type: type, message: obj['message'].to_sym, params: params }
  when :result
    { type: type, id: id, result: obj['result'] }
  when :command
    { type: type, id: id, command: obj['command'].to_sym, params: params }
  end
end