Class: Ably::Models::Message
- Inherits:
-
Object
- Object
- Ably::Models::Message
- Includes:
- Ably::Modules::Conversions, Ably::Modules::Encodeable, Ably::Modules::ModelCommon, Ably::Modules::SafeDeferrable
- Defined in:
- lib/submodules/ably-ruby/lib/ably/models/message.rb,
lib/ably-rest/modules/eventmachine_deferrable.rb
Overview
Contains an individual message that is sent to, or received from, Ably.
Defined Under Namespace
Modules: EventMachine
Instance Attribute Summary
Attributes included from Ably::Modules::ModelCommon
Instance Method Summary collapse
-
#assign_to_protocol_message(protocol_message) ⇒ Object
private
Assign this message to a ProtocolMessage before delivery to the Ably system.
-
#assigned_to_protocol_message? ⇒ Boolean
private
True if this message is assigned to a ProtocolMessage for delivery to Ably, or received from Ably.
- #attributes ⇒ Object
-
#client_id ⇒ String
The client ID of the publisher of this message.
-
#connection_id ⇒ String
The connection ID of the publisher of this message.
-
#data ⇒ Hash?
The message payload, if provided.
-
#delta_extras ⇒ DeltaExtras?
private
Delta extras extension (TM2i).
-
#encoding ⇒ String
This is typically empty, as all messages received from Ably are automatically decoded client-side using this value.
-
#extras ⇒ Object
private
Contains any arbitrary key value pairs which may also contain other primitive JSON types, JSON-encodable objects or JSON-encodable arrays.
-
#id ⇒ String
A Unique ID assigned by Ably to this message.
-
#initialize(attributes, options = {}) ⇒ Message
constructor
Message initializer.
-
#name ⇒ String
The event name.
-
#protocol_message ⇒ Ably::Models::ProtocolMessage
private
The optional ProtocolMessage this message is assigned to.
- #protocol_message_index ⇒ Object
-
#size ⇒ Object
The size is the sum over name, data, clientId, and extras in bytes (TO3l8a).
-
#timestamp ⇒ Integer
Timestamp of when the message was received by Ably, as milliseconds since the Unix epoch.
- #to_json(*args) ⇒ Object
Methods included from Ably::Modules::SafeDeferrable
#callback, #errback, #fail, #succeed
Methods included from Ably::Modules::ModelCommon
#==, #[], #as_json, included, #to_s
Methods included from Ably::Modules::MessagePack
Methods included from Ably::Modules::Encodeable
#decode, #encode, included, #original_encoding
Constructor Details
#initialize(attributes, options = {}) ⇒ Message
Ably::Models::Message initializer
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 42 def initialize(attributes, = {}) @logger = [:logger] # Logger expected for SafeDeferrable @protocol_message = [:protocol_message] @raw_hash_object = attributes set_attributes_object attributes self.attributes[:name] = ensure_utf_8(:name, name, allow_nil: true) if name self.attributes[:client_id] = ensure_utf_8(:client_id, client_id, allow_nil: true) if client_id self.attributes[:encoding] = ensure_utf_8(:encoding, encoding, allow_nil: true) if encoding self.attributes.freeze end |
Instance Method Details
#assign_to_protocol_message(protocol_message) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Assign this message to a ProtocolMessage before delivery to the Ably system
158 159 160 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 158 def () @protocol_message = end |
#assigned_to_protocol_message? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
True if this message is assigned to a ProtocolMessage for delivery to Ably, or received from Ably
167 168 169 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 167 def !!@protocol_message end |
#attributes ⇒ Object
132 133 134 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 132 def attributes @attributes end |
#client_id ⇒ String
The client ID of the publisher of this message.
62 63 64 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 62 def client_id attributes[:client_id] end |
#connection_id ⇒ String
The connection ID of the publisher of this message.
114 115 116 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 114 def connection_id attributes.fetch(:connection_id) { .connection_id if } end |
#data ⇒ Hash?
The message payload, if provided.
94 95 96 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 94 def data @data ||= attributes[:data].freeze end |
#delta_extras ⇒ DeltaExtras?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Delta extras extension (TM2i)
200 201 202 203 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 200 def delta_extras return nil if attributes[:extras][:delta].nil? @delta_extras ||= DeltaExtras.new(attributes[:extras][:delta]).freeze end |
#encoding ⇒ String
This is typically empty, as all messages received from Ably are automatically decoded client-side using this value. However, if the message encoding cannot be processed, this attribute contains the remaining transformations not applied to the data payload.
74 75 76 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 74 def encoding attributes[:encoding] end |
#extras ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Contains any arbitrary key value pairs which may also contain other primitive JSON types, JSON-encodable objects or JSON-encodable arrays. The extras field is provided to contain message metadata and/or ancillary payloads in support of specific functionality, e.g. push 1.2 adds the delta extension which is of type DeltaExtras, and the headers extension, which contains arbitrary string->string key-value pairs, settable at publish time. Unless otherwise specified, the client library should not attempt to do any filtering or validation of the extras field itself, but should treat it opaquely, encoding it and passing it to realtime unaltered.
187 188 189 190 191 192 193 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 187 def extras attributes[:extras].tap do |val| unless val.kind_of?(IdiomaticRubyWrapper) || val.kind_of?(Array) || val.kind_of?(Hash) || val.nil? raise ArgumentError, "extras contains an unsupported type #{val.class}" end end end |
#id ⇒ String
A Unique ID assigned by Ably to this message.
104 105 106 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 104 def id attributes.fetch(:id) { "#{.id!}:#{}" } end |
#name ⇒ String
The event name.
84 85 86 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 84 def name attributes[:name] end |
#protocol_message ⇒ Ably::Models::ProtocolMessage
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The optional ProtocolMessage this message is assigned to. If ProtocolMessage is nil, an error will be raised.
176 177 178 179 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 176 def raise RuntimeError, 'Message is not yet published with a ProtocolMessage. ProtocolMessage is nil' if @protocol_message.nil? @protocol_message end |
#protocol_message_index ⇒ Object
205 206 207 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 205 def ..map(&:object_id).index(self.object_id) end |
#size ⇒ Object
The size is the sum over name, data, clientId, and extras in bytes (TO3l8a)
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 144 def size %w(name data client_id extras).map do |attr| if (value = attributes[attr.to_sym]).is_a?(String) value.bytesize elsif value.nil? 0 else value.to_json.bytesize end end.sum end |
#timestamp ⇒ Integer
Timestamp of when the message was received by Ably, as milliseconds since the Unix epoch.
124 125 126 127 128 129 130 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 124 def if attributes[:timestamp] as_time_from_epoch(attributes[:timestamp]) else . end end |
#to_json(*args) ⇒ Object
136 137 138 139 140 |
# File 'lib/submodules/ably-ruby/lib/ably/models/message.rb', line 136 def to_json(*args) as_json(*args).tap do || decode_binary_data_before_to_json end.to_json end |