Class: MessageBus::Message

Inherits:
Struct
  • 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

Instance Attribute Details

#channelObject

Returns the value of attribute channel

Returns:

  • (Object)

    the current value of channel



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

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

Returns:

  • (Object)

    the current value of data



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

def data
  @data
end

#global_idObject

Returns the value of attribute global_id

Returns:

  • (Object)

    the current value of global_id



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

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

Returns:

  • (Object)

    the current value of message_id



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

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



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/message_bus/message.rb', line 7

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]

  MessageBus::Message.new(global_id, message_id, channel, data)
end

Instance Method Details

#encodeObject

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



22
23
24
# File 'lib/message_bus/message.rb', line 22

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

#encode_without_idsObject



26
27
28
# File 'lib/message_bus/message.rb', line 26

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