Class: Ably::Models::ProtocolMessage

Inherits:
Object
  • Object
show all
Extended by:
Ably::Modules::Enum
Includes:
Ably::Modules::Encodeable, Ably::Modules::ModelCommon, Ably::Modules::SafeDeferrable
Defined in:
lib/ably/models/protocol_message.rb

Overview

A message sent and received over the Realtime protocol. A ProtocolMessage always relates to a single channel only, but can contain multiple individual Messages or PresenceMessages. ProtocolMessages are serially numbered on a connection. See the Ably client library developer documentation for further details on the members of a ProtocolMessage

Constant Summary collapse

ACTION =

Actions which are sent by the Ably Realtime API

The values correspond to the ints which the API understands.

ruby_enum('ACTION',
  heartbeat:    0,
  ack:          1,
  nack:         2,
  connect:      3,
  connected:    4,
  disconnect:   5,
  disconnected: 6,
  close:        7,
  closed:       8,
  error:        9,
  attach:       10,
  attached:     11,
  detach:       12,
  detached:     13,
  presence:     14,
  message:      15,
  sync:         16,
  auth:         17
)

Instance Attribute Summary collapse

Attributes included from Ably::Modules::ModelCommon

#hash

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ably::Modules::SafeDeferrable

#callback, #errback, #fail, #succeed

Methods included from Ably::Modules::Encodeable

#decode, #encode, included, #original_encoding

Methods included from Ably::Modules::ModelCommon

#==, #[], included, #to_json

Methods included from Ably::Modules::MessagePack

#to_msgpack

Constructor Details

#initialize(hash_object, options = {}) ⇒ ProtocolMessage

Options Hash (options):

Raises:

  • (ArgumentError)

83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ably/models/protocol_message.rb', line 83

def initialize(hash_object, options = {})
  @logger = options[:logger] # Logger expected for SafeDeferrable

  @raw_hash_object = hash_object
  @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

  raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
  @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

  @hash_object.freeze
end

Instance Attribute Details

#actionACTION (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#attributesHash (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#authAbly::Models::AuthDetails (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#channelString (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#channel_serialString (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#connection_idString (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#connection_keyString (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#connection_serialBignum (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#countInteger (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#errorErrorInfo (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#flagsInteger (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#loggerObject (readonly)

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.


295
296
297
# File 'lib/ably/models/protocol_message.rb', line 295

def logger
  @logger
end

#message_serialBignum (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#messagesArray<Message> (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#presenceArray<PresenceMessage> (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

#timestampTime (readonly)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ably/models/protocol_message.rb', line 40

class ProtocolMessage
  include Ably::Modules::ModelCommon
  include Ably::Modules::Encodeable
  include Ably::Modules::SafeDeferrable if defined?(Ably::Realtime)
  extend Ably::Modules::Enum

  # Actions which are sent by the Ably Realtime API
  #
  # The values correspond to the ints which the API
  # understands.
  ACTION = ruby_enum('ACTION',
    heartbeat:    0,
    ack:          1,
    nack:         2,
    connect:      3,
    connected:    4,
    disconnect:   5,
    disconnected: 6,
    close:        7,
    closed:       8,
    error:        9,
    attach:       10,
    attached:     11,
    detach:       12,
    detached:     13,
    presence:     14,
    message:      15,
    sync:         16,
    auth:         17
  )

  # Indicates this protocol message action will generate an ACK response such as :message or :presence
  # @api private
  def self.ack_required?(for_action)
    ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
  end

  # {ProtocolMessage} initializer
  #
  # @param  hash_object [Hash]            object with the underlying protocol message data
  # @param  [Hash]      options           an options Hash for this initializer
  # @option options     [Logger]          :logger            An optional Logger to be used by {Ably::Modules::SafeDeferrable} if an exception is caught in a callback
  #
  def initialize(hash_object, options = {})
    @logger = options[:logger] # Logger expected for SafeDeferrable

    @raw_hash_object = hash_object
    @hash_object     = IdiomaticRubyWrapper(@raw_hash_object.clone)

    raise ArgumentError, 'Invalid ProtocolMessage, action cannot be nil' if @hash_object[:action].nil?
    @hash_object[:action] = ACTION(@hash_object[:action]).to_i unless @hash_object[:action].kind_of?(Integer)

    @hash_object.freeze
  end

  %w(id channel channel_serial connection_id).each do |attribute|
    define_method attribute do
      attributes[attribute.to_sym]
    end
  end

  def connection_key
    # connection_key in connection details takes precedence over connection_key on the ProtocolMessage
    # connection_key in the ProtocolMessage will be deprecated in future protocol versions > 0.8
    connection_details.connection_key || attributes[:connection_key]
  end

  def id!
    raise RuntimeError, 'ProtocolMessage #id is nil' unless id
    id
  end

  def action
    ACTION(attributes[:action])
  rescue KeyError
    raise KeyError, "Action '#{attributes[:action]}' is not supported by ProtocolMessage"
  end

  def error
    @error ||= ErrorInfo.new(attributes[:error]) if attributes[:error]
  end

  def timestamp
    as_time_from_epoch(attributes[:timestamp]) if attributes[:timestamp]
  end

  def message_serial
    Integer(attributes[:msg_serial])
  rescue TypeError
    raise TypeError, "msg_serial '#{attributes[:msg_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def connection_serial
    Integer(attributes[:connection_serial])
  rescue TypeError
    raise TypeError, "connection_serial '#{attributes[:connection_serial]}' is invalid, a positive Integer is expected for a ProtocolMessage"
  end

  def count
    [1, attributes[:count].to_i].max
  end

  # @api private
  def has_message_serial?
    message_serial && true
  rescue TypeError
    false
  end

  # @api private
  def has_connection_serial?
    connection_serial && true
  rescue TypeError
    false
  end

  def serial
    if has_connection_serial?
      connection_serial
    else
      message_serial
    end
  end

  # @api private
  def has_serial?
    has_connection_serial? || has_message_serial?
  end

  def messages
    @messages ||=
      Array(attributes[:messages]).map do |message|
        Ably::Models.Message(message, protocol_message: self)
      end
  end

  # @api private
  def add_message(message)
    messages << message
  end

  def presence
    @presence ||=
      Array(attributes[:presence]).map do |message|
        Ably::Models.PresenceMessage(message, protocol_message: self)
      end
  end

  def flags
    Integer(attributes[:flags])
  rescue TypeError
    0
  end

  # @api private
  def has_presence_flag?
    flags & 1 == 1
  end

  # @api private
  def has_backlog_flag?
    flags & 2 == 2 # 2^1
  end

  # @api private
  def has_channel_resumed_flag?
    flags & 4 == 4 # 2^2
  end

  # @api private
  def has_local_presence_flag?
    flags & 8 == 8 # 2^3
  end

  # @api private
  def has_transient_flag?
    flags & 16 == 16 # 2^4
  end

  # @api private
  def has_attach_presence_flag?
    flags & 65536 == 65536 # 2^16
  end

  # @api private
  def has_attach_publish_flag?
    flags & 131072 == 131072 # 2^17
  end

  # @api private
  def has_attach_subscribe_flag?
    flags & 262144 == 262144 # 2^18
  end

  # @api private
  def has_attach_presence_subscribe_flag?
    flags & 524288 == 524288 # 2^19
  end

  def connection_details
    @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
  end

  def auth
    @auth ||= Ably::Models::AuthDetails(attributes[:auth])
  end

  # Indicates this protocol message will generate an ACK response when sent
  # Examples of protocol messages required ACK include :message and :presence
  # @api private
  def ack_required?
    self.class.ack_required?(action)
  end

  def attributes
    @hash_object
  end

  # Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys
  def as_json(*args)
    raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
    raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

    attributes.dup.tap do |hash_object|
      hash_object['action']   = action.to_i
      hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
      hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
    end.as_json
  end

  def to_s
    json_hash = as_json

    # Decode any binary data to before converting to a JSON string representation
    %w(messages presence).each do |message_type|
      if json_hash[message_type] && !json_hash[message_type].empty?
        json_hash[message_type].each do |message|
          decode_binary_data_before_to_json message
        end
      end
    end

    json_hash.to_json
  end

  # True if the ProtocolMessage appears to be invalid, however this is not a guarantee
  # @return [Boolean]
  # @api private
  def invalid?
    action_enum = action rescue nil
    !action_enum || (ack_required? && !has_serial?)
  end

  # @!attribute [r] logger
  # @api private
  attr_reader :logger
end

Class Method Details

.ack_required?(for_action) ⇒ 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.

Indicates this protocol message action will generate an ACK response such as :message or :presence


73
74
75
# File 'lib/ably/models/protocol_message.rb', line 73

def self.ack_required?(for_action)
  ACTION(for_action).match_any?(ACTION.Presence, ACTION.Message)
end

Instance Method Details

#ack_required?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.

Indicates this protocol message will generate an ACK response when sent Examples of protocol messages required ACK include :message and :presence


250
251
252
# File 'lib/ably/models/protocol_message.rb', line 250

def ack_required?
  self.class.ack_required?(action)
end

#add_message(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.


177
178
179
# File 'lib/ably/models/protocol_message.rb', line 177

def add_message(message)
  messages << message
end

#as_json(*args) ⇒ Object

Return a JSON ready object from the underlying #attributes using Ably naming conventions for keys

Raises:

  • (TypeError)

259
260
261
262
263
264
265
266
267
268
# File 'lib/ably/models/protocol_message.rb', line 259

def as_json(*args)
  raise TypeError, ':action is missing, cannot generate a valid Hash for ProtocolMessage' unless action
  raise TypeError, ':msg_serial or :connection_serial is missing, cannot generate a valid Hash for ProtocolMessage' if ack_required? && !has_serial?

  attributes.dup.tap do |hash_object|
    hash_object['action']   = action.to_i
    hash_object['messages'] = messages.map(&:as_json) unless messages.empty?
    hash_object['presence'] = presence.map(&:as_json) unless presence.empty?
  end.as_json
end

#connection_detailsObject


239
240
241
# File 'lib/ably/models/protocol_message.rb', line 239

def connection_details
  @connection_details ||= Ably::Models::ConnectionDetails(attributes[:connection_details])
end

#has_attach_presence_flag?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.


220
221
222
# File 'lib/ably/models/protocol_message.rb', line 220

def has_attach_presence_flag?
  flags & 65536 == 65536 # 2^16
end

#has_attach_presence_subscribe_flag?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.


235
236
237
# File 'lib/ably/models/protocol_message.rb', line 235

def has_attach_presence_subscribe_flag?
  flags & 524288 == 524288 # 2^19
end

#has_attach_publish_flag?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.


225
226
227
# File 'lib/ably/models/protocol_message.rb', line 225

def has_attach_publish_flag?
  flags & 131072 == 131072 # 2^17
end

#has_attach_subscribe_flag?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.


230
231
232
# File 'lib/ably/models/protocol_message.rb', line 230

def has_attach_subscribe_flag?
  flags & 262144 == 262144 # 2^18
end

#has_backlog_flag?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.


200
201
202
# File 'lib/ably/models/protocol_message.rb', line 200

def has_backlog_flag?
  flags & 2 == 2 # 2^1
end

#has_channel_resumed_flag?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.


205
206
207
# File 'lib/ably/models/protocol_message.rb', line 205

def has_channel_resumed_flag?
  flags & 4 == 4 # 2^2
end

#has_connection_serial?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.


150
151
152
153
154
# File 'lib/ably/models/protocol_message.rb', line 150

def has_connection_serial?
  connection_serial && true
rescue TypeError
  false
end

#has_local_presence_flag?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.


210
211
212
# File 'lib/ably/models/protocol_message.rb', line 210

def has_local_presence_flag?
  flags & 8 == 8 # 2^3
end

#has_message_serial?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.


143
144
145
146
147
# File 'lib/ably/models/protocol_message.rb', line 143

def has_message_serial?
  message_serial && true
rescue TypeError
  false
end

#has_presence_flag?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.


195
196
197
# File 'lib/ably/models/protocol_message.rb', line 195

def has_presence_flag?
  flags & 1 == 1
end

#has_serial?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.


165
166
167
# File 'lib/ably/models/protocol_message.rb', line 165

def has_serial?
  has_connection_serial? || has_message_serial?
end

#has_transient_flag?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.


215
216
217
# File 'lib/ably/models/protocol_message.rb', line 215

def has_transient_flag?
  flags & 16 == 16 # 2^4
end

#id!Object

Raises:

  • (RuntimeError)

107
108
109
110
# File 'lib/ably/models/protocol_message.rb', line 107

def id!
  raise RuntimeError, 'ProtocolMessage #id is nil' unless id
  id
end

#invalid?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 the ProtocolMessage appears to be invalid, however this is not a guarantee


288
289
290
291
# File 'lib/ably/models/protocol_message.rb', line 288

def invalid?
  action_enum = action rescue nil
  !action_enum || (ack_required? && !has_serial?)
end

#serialObject


156
157
158
159
160
161
162
# File 'lib/ably/models/protocol_message.rb', line 156

def serial
  if has_connection_serial?
    connection_serial
  else
    message_serial
  end
end

#to_sObject


270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/ably/models/protocol_message.rb', line 270

def to_s
  json_hash = as_json

  # Decode any binary data to before converting to a JSON string representation
  %w(messages presence).each do |message_type|
    if json_hash[message_type] && !json_hash[message_type].empty?
      json_hash[message_type].each do |message|
        decode_binary_data_before_to_json message
      end
    end
  end

  json_hash.to_json
end