Class: Ably::Realtime::Models::Message

Inherits:
Object
  • Object
show all
Includes:
Modules::Conversions, Shared
Defined in:
lib/ably/realtime/models/message.rb

Overview

A class representing an individual message to be sent or received via the Ably Realtime service.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shared

#==, #[]

Constructor Details

#initialize(json_object, protocol_message) ⇒ Message

Returns a new instance of Message.



24
25
26
27
28
# File 'lib/ably/realtime/models/message.rb', line 24

def initialize(json_object, protocol_message)
  @protocol_message = protocol_message
  @raw_json_object  = json_object
  @json_object      = IdiomaticRubyWrapper(@raw_json_object.clone.freeze, stop_at: [:data])
end

Instance Attribute Details

#ably_timestampTime (readonly)

Returns Timestamp when the message was received by the Ably the service for publishing.

Returns:

  • (Time)

    Timestamp when the message was received by the Ably the service for publishing



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/ably/realtime/models/message.rb', line 20

class Message
  include Shared
  include Ably::Modules::Conversions

  def initialize(json_object, protocol_message)
    @protocol_message = protocol_message
    @raw_json_object  = json_object
    @json_object      = IdiomaticRubyWrapper(@raw_json_object.clone.freeze, stop_at: [:data])
  end

  %w( name client_id ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def data
    @data ||= json[:data].freeze
  end

  def message_id
    "#{connection_id}:#{message_serial}:#{protocol_message_index}"
  end

  def sender_timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def ably_timestamp
    protocol_message.timestamp
  end

  def json
    @json_object
  end

  def to_json_object
    raise RuntimeError, ":name is missing, cannot generate valid JSON for Message" unless name

    json.dup.tap do |json_object|
      json_object[:timestamp] = as_since_epoch(Time.now) unless sender_timestamp
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end

  private
  attr_reader :protocol_message

  def protocol_message_index
    protocol_message.messages.index(self)
  end

  def connection_id
    protocol_message.connection_id
  end

  def message_serial
    protocol_message.message_serial
  end
end

#client_idString (readonly)

Returns The id of the publisher of this message.

Returns:

  • (String)

    The id of the publisher of this message



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/ably/realtime/models/message.rb', line 20

class Message
  include Shared
  include Ably::Modules::Conversions

  def initialize(json_object, protocol_message)
    @protocol_message = protocol_message
    @raw_json_object  = json_object
    @json_object      = IdiomaticRubyWrapper(@raw_json_object.clone.freeze, stop_at: [:data])
  end

  %w( name client_id ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def data
    @data ||= json[:data].freeze
  end

  def message_id
    "#{connection_id}:#{message_serial}:#{protocol_message_index}"
  end

  def sender_timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def ably_timestamp
    protocol_message.timestamp
  end

  def json
    @json_object
  end

  def to_json_object
    raise RuntimeError, ":name is missing, cannot generate valid JSON for Message" unless name

    json.dup.tap do |json_object|
      json_object[:timestamp] = as_since_epoch(Time.now) unless sender_timestamp
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end

  private
  attr_reader :protocol_message

  def protocol_message_index
    protocol_message.messages.index(self)
  end

  def connection_id
    protocol_message.connection_id
  end

  def message_serial
    protocol_message.message_serial
  end
end

#dataObject (readonly)

Returns The message payload. See the documentation for supported datatypes.

Returns:

  • (Object)

    The message payload. See the documentation for supported datatypes.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/ably/realtime/models/message.rb', line 20

class Message
  include Shared
  include Ably::Modules::Conversions

  def initialize(json_object, protocol_message)
    @protocol_message = protocol_message
    @raw_json_object  = json_object
    @json_object      = IdiomaticRubyWrapper(@raw_json_object.clone.freeze, stop_at: [:data])
  end

  %w( name client_id ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def data
    @data ||= json[:data].freeze
  end

  def message_id
    "#{connection_id}:#{message_serial}:#{protocol_message_index}"
  end

  def sender_timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def ably_timestamp
    protocol_message.timestamp
  end

  def json
    @json_object
  end

  def to_json_object
    raise RuntimeError, ":name is missing, cannot generate valid JSON for Message" unless name

    json.dup.tap do |json_object|
      json_object[:timestamp] = as_since_epoch(Time.now) unless sender_timestamp
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end

  private
  attr_reader :protocol_message

  def protocol_message_index
    protocol_message.messages.index(self)
  end

  def connection_id
    protocol_message.connection_id
  end

  def message_serial
    protocol_message.message_serial
  end
end

#jsonHash (readonly)

Returns Access the protocol message Hash object ruby’fied to use symbolized keys.

Returns:

  • (Hash)

    Access the protocol message Hash object ruby’fied to use symbolized keys



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/ably/realtime/models/message.rb', line 20

class Message
  include Shared
  include Ably::Modules::Conversions

  def initialize(json_object, protocol_message)
    @protocol_message = protocol_message
    @raw_json_object  = json_object
    @json_object      = IdiomaticRubyWrapper(@raw_json_object.clone.freeze, stop_at: [:data])
  end

  %w( name client_id ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def data
    @data ||= json[:data].freeze
  end

  def message_id
    "#{connection_id}:#{message_serial}:#{protocol_message_index}"
  end

  def sender_timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def ably_timestamp
    protocol_message.timestamp
  end

  def json
    @json_object
  end

  def to_json_object
    raise RuntimeError, ":name is missing, cannot generate valid JSON for Message" unless name

    json.dup.tap do |json_object|
      json_object[:timestamp] = as_since_epoch(Time.now) unless sender_timestamp
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end

  private
  attr_reader :protocol_message

  def protocol_message_index
    protocol_message.messages.index(self)
  end

  def connection_id
    protocol_message.connection_id
  end

  def message_serial
    protocol_message.message_serial
  end
end

#message_idString (readonly)

Returns A globally unique message ID.

Returns:

  • (String)

    A globally unique message ID



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/ably/realtime/models/message.rb', line 20

class Message
  include Shared
  include Ably::Modules::Conversions

  def initialize(json_object, protocol_message)
    @protocol_message = protocol_message
    @raw_json_object  = json_object
    @json_object      = IdiomaticRubyWrapper(@raw_json_object.clone.freeze, stop_at: [:data])
  end

  %w( name client_id ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def data
    @data ||= json[:data].freeze
  end

  def message_id
    "#{connection_id}:#{message_serial}:#{protocol_message_index}"
  end

  def sender_timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def ably_timestamp
    protocol_message.timestamp
  end

  def json
    @json_object
  end

  def to_json_object
    raise RuntimeError, ":name is missing, cannot generate valid JSON for Message" unless name

    json.dup.tap do |json_object|
      json_object[:timestamp] = as_since_epoch(Time.now) unless sender_timestamp
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end

  private
  attr_reader :protocol_message

  def protocol_message_index
    protocol_message.messages.index(self)
  end

  def connection_id
    protocol_message.connection_id
  end

  def message_serial
    protocol_message.message_serial
  end
end

#nameString (readonly)

Returns The event name, if available.

Returns:

  • (String)

    The event name, if available



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/ably/realtime/models/message.rb', line 20

class Message
  include Shared
  include Ably::Modules::Conversions

  def initialize(json_object, protocol_message)
    @protocol_message = protocol_message
    @raw_json_object  = json_object
    @json_object      = IdiomaticRubyWrapper(@raw_json_object.clone.freeze, stop_at: [:data])
  end

  %w( name client_id ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def data
    @data ||= json[:data].freeze
  end

  def message_id
    "#{connection_id}:#{message_serial}:#{protocol_message_index}"
  end

  def sender_timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def ably_timestamp
    protocol_message.timestamp
  end

  def json
    @json_object
  end

  def to_json_object
    raise RuntimeError, ":name is missing, cannot generate valid JSON for Message" unless name

    json.dup.tap do |json_object|
      json_object[:timestamp] = as_since_epoch(Time.now) unless sender_timestamp
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end

  private
  attr_reader :protocol_message

  def protocol_message_index
    protocol_message.messages.index(self)
  end

  def connection_id
    protocol_message.connection_id
  end

  def message_serial
    protocol_message.message_serial
  end
end

#sender_timestampTime (readonly)

Returns Timestamp when the message was sent according to the publisher client.

Returns:

  • (Time)

    Timestamp when the message was sent according to the publisher client



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/ably/realtime/models/message.rb', line 20

class Message
  include Shared
  include Ably::Modules::Conversions

  def initialize(json_object, protocol_message)
    @protocol_message = protocol_message
    @raw_json_object  = json_object
    @json_object      = IdiomaticRubyWrapper(@raw_json_object.clone.freeze, stop_at: [:data])
  end

  %w( name client_id ).each do |attribute|
    define_method attribute do
      json[attribute.to_sym]
    end
  end

  def data
    @data ||= json[:data].freeze
  end

  def message_id
    "#{connection_id}:#{message_serial}:#{protocol_message_index}"
  end

  def sender_timestamp
    as_time_from_epoch(json[:timestamp]) if json[:timestamp]
  end

  def ably_timestamp
    protocol_message.timestamp
  end

  def json
    @json_object
  end

  def to_json_object
    raise RuntimeError, ":name is missing, cannot generate valid JSON for Message" unless name

    json.dup.tap do |json_object|
      json_object[:timestamp] = as_since_epoch(Time.now) unless sender_timestamp
    end
  end

  def to_json(*args)
    to_json_object.to_json
  end

  private
  attr_reader :protocol_message

  def protocol_message_index
    protocol_message.messages.index(self)
  end

  def connection_id
    protocol_message.connection_id
  end

  def message_serial
    protocol_message.message_serial
  end
end

Instance Method Details

#to_json(*args) ⇒ Object



64
65
66
# File 'lib/ably/realtime/models/message.rb', line 64

def to_json(*args)
  to_json_object.to_json
end

#to_json_objectObject

Raises:

  • (RuntimeError)


56
57
58
59
60
61
62
# File 'lib/ably/realtime/models/message.rb', line 56

def to_json_object
  raise RuntimeError, ":name is missing, cannot generate valid JSON for Message" unless name

  json.dup.tap do |json_object|
    json_object[:timestamp] = as_since_epoch(Time.now) unless sender_timestamp
  end
end