Class: MessageBus::Message

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

Overview

Represents a published message and its encoding for persistence.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_id, message_id, channel, data) ⇒ Message

Returns a new instance of Message.



8
9
10
11
12
13
# File 'lib/message_bus/message.rb', line 8

def initialize(global_id, message_id, channel, data)
  @global_id = global_id
  @message_id = message_id
  @channel = channel
  @data = data
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



5
6
7
# File 'lib/message_bus/message.rb', line 5

def channel
  @channel
end

#client_idsObject

Returns the value of attribute client_ids.



5
6
7
# File 'lib/message_bus/message.rb', line 5

def client_ids
  @client_ids
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/message_bus/message.rb', line 5

def data
  @data
end

#global_idObject

Returns the value of attribute global_id.



5
6
7
# File 'lib/message_bus/message.rb', line 5

def global_id
  @global_id
end

#group_idsObject

Returns the value of attribute group_ids.



5
6
7
# File 'lib/message_bus/message.rb', line 5

def group_ids
  @group_ids
end

#message_idObject

Returns the value of attribute message_id.



5
6
7
# File 'lib/message_bus/message.rb', line 5

def message_id
  @message_id
end

#site_idObject

Returns the value of attribute site_id.



5
6
7
# File 'lib/message_bus/message.rb', line 5

def site_id
  @site_id
end

#user_idsObject

Returns the value of attribute user_ids.



5
6
7
# File 'lib/message_bus/message.rb', line 5

def user_ids
  @user_ids
end

Class Method Details

.decode(encoded) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/message_bus/message.rb', line 15

def self.decode(encoded)
  s1 = encoded.index("|")
  s2 = encoded.index("|", s1 + 1)
  s3 = encoded.index("|", s2 + 1)

  global_id  = encoded[0, s1 + 1].to_i
  message_id = encoded[(s1 + 1), (s2 - s1 - 1)].to_i
  channel    = encoded[(s2 + 1), (s3 - s2 - 1)]
  channel.gsub!("$$123$$", "|")
  data = encoded[(s3 + 1), encoded.size]

  new(global_id, message_id, channel, data)
end

Instance Method Details

#==(other) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/message_bus/message.rb', line 38

def ==(other)
  other.is_a?(self.class) &&
    self.global_id == other.global_id &&
    self.message_id == other.message_id &&
    self.channel == other.channel &&
    self.data == other.data
end

#encodeObject

only tricky thing to encode is pipes in a channel name ... do a straight replace



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

def encode
  "#{global_id}|#{message_id}|#{channel.gsub("|", "$$123$$")}|#{data}"
end

#encode_without_idsObject



34
35
36
# File 'lib/message_bus/message.rb', line 34

def encode_without_ids
  "#{channel.gsub("|", "$$123$$")}|#{data}"
end