Class: RailwayIpc::Rabbitmq::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/railway_ipc/rabbitmq/payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, message) ⇒ Payload

Returns a new instance of Payload.



26
27
28
29
# File 'lib/railway_ipc/rabbitmq/payload.rb', line 26

def initialize(type, message)
  @type = type
  @message = message
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/railway_ipc/rabbitmq/payload.rb', line 6

def message
  @message
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/railway_ipc/rabbitmq/payload.rb', line 6

def type
  @type
end

Class Method Details

.decode(message) ⇒ Object



19
20
21
22
23
24
# File 'lib/railway_ipc/rabbitmq/payload.rb', line 19

def self.decode(message)
  message = JSON.parse(message)
  type = message['type']
  message = Base64.decode64(message['encoded_message'])
  new(type, message)
end

.encode(message) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/railway_ipc/rabbitmq/payload.rb', line 8

def self.encode(message)
  type = message.class.to_s
  begin
    message = Base64.encode64(message.class.encode(message))
  # TODO: also need to rescue Google::Protobuf::TypeError
  rescue NoMethodError
    raise RailwayIpc::InvalidProtobuf.new("Message #{message} is not a valid protobuf")
  end
  new(type, message).to_json
end

Instance Method Details

#to_jsonObject

rubocop:disable Lint/ToJSON



32
33
34
35
36
37
# File 'lib/railway_ipc/rabbitmq/payload.rb', line 32

def to_json
  {
    type: type,
    encoded_message: message
  }.to_json
end