Module: Invoker::IPC::Message::Serialization

Included in:
Add, AddHttp, DnsCheck, DnsCheckResponse, List, ListResponse, Ping, Pong, Process, Reload, Remove, Tail, TailResponse
Defined in:
lib/invoker/ipc/message.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#as_jsonObject



9
10
11
# File 'lib/invoker/ipc/message.rb', line 9

def as_json
  attributes.merge(type: message_type)
end

#attributesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/invoker/ipc/message.rb', line 33

def attributes
  message_attribute_keys = message_attributes || []
  message_attribute_keys.reduce({}) do |mem, obj|
    value = send(obj)
    if value.is_a?(Array)
      mem[obj] = serialize_array(value)
    elsif value.is_a?(Hash)
      mem[obj] = serialize_hash(value)
    else
      mem[obj] = value.respond_to?(:as_json) ? value.as_json : encode_as_utf(value)
    end
    mem
  end
end

#encoded_messageObject



21
22
23
24
25
26
# File 'lib/invoker/ipc/message.rb', line 21

def encoded_message
  json_data = to_json
  json_size = json_data.length.to_s
  length_str = json_size.rjust(Invoker::IPC::INITIAL_PACKET_SIZE, '0')
  length_str + json_data
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/invoker/ipc/message.rb', line 28

def eql?(other)
  other.class == self.class &&
    compare_attributes(other)
end

#message_attributesObject



17
18
19
# File 'lib/invoker/ipc/message.rb', line 17

def message_attributes
  self.class.message_attributes
end

#to_jsonObject



13
14
15
# File 'lib/invoker/ipc/message.rb', line 13

def to_json
  JSON.generate(as_json)
end