Class: DuoRuby::Message
- Inherits:
-
Object
- Object
- DuoRuby::Message
- 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).
Constant Summary collapse
- REPLY_EVENT =
"$reply"- ERROR_EVENT =
"$error"
Instance Attribute Summary collapse
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#reply_to ⇒ Object
readonly
Returns the value of attribute reply_to.
Class Method Summary collapse
-
.coerce(value) ⇒ Message
Coerces
valueinto a Message. - .error(code:, message:, details: nil, reply_to: nil) ⇒ Object
- .reply(reply_to, result) ⇒ Object
- .request(event, request_id, **params) ⇒ Object
Instance Method Summary collapse
-
#initialize(event, params = nil, id: nil, reply_to: nil, **keyword_params) ⇒ Message
constructor
A new instance of Message.
-
#to_h ⇒ Hash
Serializes the message to a plain Hash suitable for JSON encoding.
Constructor Details
#initialize(event, params = nil, id: nil, reply_to: nil, **keyword_params) ⇒ Message
Returns a new instance of Message.
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
#event ⇒ Object (readonly)
Returns the value of attribute event.
24 25 26 |
# File 'lib/duoruby/message.rb', line 24 def event @event end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
24 25 26 |
# File 'lib/duoruby/message.rb', line 24 def id @id end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
24 25 26 |
# File 'lib/duoruby/message.rb', line 24 def params @params end |
#reply_to ⇒ Object (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.
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: .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_h ⇒ Hash
Serializes the message to a plain Hash suitable for JSON encoding. Both the event key and all params keys are strings.
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 || ["id"] = id if id ["reply_to"] = reply_to if reply_to end end |