Module: Evesync::IPCData

Defined in:
lib/evesync/ipc/data.rb

Class Method Summary collapse

Class Method Details

.pack(message) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/evesync/ipc/data.rb', line 10

def self.pack(message)
  unless message.respond_to? :to_hash
    err_msg = "IPC ERROR Instance #{message} must implement `to_hash'"
    Log.fatal(err_msg)
    raise err_msg
  end

  hash = message.to_hash

  hash.to_json
end

.unpack(message) ⇒ Object



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
# File 'lib/evesync/ipc/data.rb', line 22

def self.unpack(message)
  unless message.is_a? String
    raise "IPC ERROR message #{message} must be of type String"
  end

  begin
    hash = JSON.parse(message)
  rescue JSON::ParseError => e
    Log.fatal("IPC ERROR Unable to parse message #{message}")
    raise e
  end

  begin
    Log.debug("IPC Accepted basic hash #{hash}")
    cl = Object.const_get hash['type']
  rescue NameError => e
    # FIXME: just sent JSON, this event will be delegated
    # to another daemon (maybe) with fields:
    # redirect_to_port: <port number>
    Log.fatal("Unsupported basic type #{hash['type']}")
    raise e
  end

  unless cl.respond_to? :from_hash
    err_msg = "IPC ERROR Class #{cl} must implement `self.from_hash'"
    Log.fatal(err_msg)
    raise err_msg
  end

  cl.from_hash hash
end