Class: DuoRuby::Message

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

Overview

Represents a single WebSocket message: an event name and a keyword params hash.

Messages are the protocol unit shared between backend and frontend. They serialize to a plain Hash for JSON transport and can be coerced back from that same Hash (with either string or symbol keys).

Examples:

Creating a message

msg = Message.new("chat", text: "hello")
msg.event   # => "chat"
msg.params  # => {text: "hello"}
msg.to_h    # => {"event" => "chat", "params" => {"text" => "hello"}}

Coercing from a parsed JSON hash

Message.coerce("event" => "chat", "params" => {"text" => "hello"})

Constant Summary collapse

REPLY_EVENT =
"$reply"
ERROR_EVENT =
"$error"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, params = nil, id: nil, reply_to: nil, **keyword_params) ⇒ Message

Returns a new instance of Message.

Parameters:

  • event (String, Symbol)

    the event name; stored as a String

  • params (Hash) (defaults to: nil)

    keyword params accompanying the event



62
63
64
65
66
67
# File 'lib/duoruby/message.rb', line 62

def initialize(event, params = nil, id: nil, reply_to: nil, **keyword_params)
  @event = event.to_s
  @params = params || keyword_params
  @id = id
  @reply_to = reply_to
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



24
25
26
# File 'lib/duoruby/message.rb', line 24

def event
  @event
end

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/duoruby/message.rb', line 24

def id
  @id
end

#paramsObject (readonly)

Returns the value of attribute params.



24
25
26
# File 'lib/duoruby/message.rb', line 24

def params
  @params
end

#reply_toObject (readonly)

Returns the value of attribute reply_to.



24
25
26
# File 'lib/duoruby/message.rb', line 24

def reply_to
  @reply_to
end

Class Method Details

.coerce(value) ⇒ Message

Coerces value into a Message.

If value is already a Message it is returned unchanged. Otherwise value is treated as a Hash with string or symbol keys containing an event key and an optional params key.

Parameters:

  • value (Message, Hash)

    the value to coerce

Returns:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/duoruby/message.rb', line 48

def self.coerce(value)
  return value if value.is_a?(Message)

  params = value.fetch("params") { value.fetch(:params, {}) }
  new(
    value.fetch("event") { value.fetch(:event) },
    params.transform_keys(&:to_sym),
    id: value.fetch("id") { value.fetch(:id, nil) },
    reply_to: value.fetch("reply_to") { value.fetch(:reply_to, nil) }
  )
end

.error(code:, message:, details: nil, reply_to: nil) ⇒ Object



34
35
36
37
38
# File 'lib/duoruby/message.rb', line 34

def self.error(code:, message:, details: nil, reply_to: nil)
  params = {code: code.to_s, message: message.to_s}
  params[:details] = details if details
  new(ERROR_EVENT, params, reply_to: reply_to)
end

.reply(reply_to, result) ⇒ Object



30
31
32
# File 'lib/duoruby/message.rb', line 30

def self.reply(reply_to, result)
  new(REPLY_EVENT, {result: result}, reply_to: reply_to)
end

.request(event, request_id, **params) ⇒ Object



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

def self.request(event, request_id, **params)
  new(event, params, id: request_id)
end

Instance Method Details

#to_hHash

Serializes the message to a plain Hash suitable for JSON encoding. Both the event key and all params keys are strings.

Returns:

  • (Hash)


73
74
75
76
77
78
# File 'lib/duoruby/message.rb', line 73

def to_h
  {"event" => event, "params" => params.transform_keys(&:to_s)}.tap do |message|
    message["id"] = id if id
    message["reply_to"] = reply_to if reply_to
  end
end