Class: ZSS::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/zss/message.rb,
lib/zss/message/smi.rb,
lib/zss/message/message_type.rb,
lib/zss/message/message_address.rb

Defined Under Namespace

Classes: Address, SMI, Type

Constant Summary collapse

PROTOCOL_VERSION =
"ZSS:0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Message

Returns a new instance of Message.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zss/message.rb', line 21

def initialize(args = {})

  @identity     = args[:identity]
  @protocol     = args[:protocol] || PROTOCOL_VERSION
  @type         = args[:type] || Type::REQ
  @rid          = args[:rid] || SecureRandom.uuid
  @address      = args[:address]
  @headers      = args[:headers] || {}
  @status       = args[:status]
  @payload      = args[:payload]

end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



12
13
14
# File 'lib/zss/message.rb', line 12

def address
  @address
end

#headersObject

Returns the value of attribute headers.



12
13
14
# File 'lib/zss/message.rb', line 12

def headers
  @headers
end

#identityObject

Returns the value of attribute identity.



12
13
14
# File 'lib/zss/message.rb', line 12

def identity
  @identity
end

#payloadObject

Returns the value of attribute payload.



12
13
14
# File 'lib/zss/message.rb', line 12

def payload
  @payload
end

#protocolObject

Returns the value of attribute protocol.



12
13
14
# File 'lib/zss/message.rb', line 12

def protocol
  @protocol
end

#ridObject

Returns the value of attribute rid.



12
13
14
# File 'lib/zss/message.rb', line 12

def rid
  @rid
end

#statusObject

Returns the value of attribute status.



12
13
14
# File 'lib/zss/message.rb', line 12

def status
  @status
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/zss/message.rb', line 12

def type
  @type
end

Class Method Details

.parse(frames) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zss/message.rb', line 38

def self.parse(frames)

  frames.unshift(nil) if frames.length == 7

  msg = Message.new(
    identity: frames.shift,
    protocol: frames.shift,
    type:     frames.shift,
    rid:      frames.shift,
    address:  Address.new(
      MessagePack.unpack(frames.shift).with_indifferent_access
    ),
    headers:  Hashie::Mash.new(MessagePack.unpack(frames.shift)),
    status:   frames.shift.to_i,
    payload:  MessagePack.unpack(frames.shift),
  )

  if msg.payload.kind_of? Hash
    msg.payload = Hashie::Mash.new(msg.payload)
  end

  msg
end

Instance Method Details

#is_error?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/zss/message.rb', line 98

def is_error?
  status != 200
end

#req?Boolean

Returns:

  • (Boolean)


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

def req?
  type == Type::REQ
end

#to_framesObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zss/message.rb', line 85

def to_frames
  [
    identity,
    protocol,
    type,
    rid,
    address.instance_values.to_msgpack,
    headers.to_h.to_msgpack,
    status.to_s,
    payload.to_msgpack
  ]
end

#to_sObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zss/message.rb', line 62

def to_s
  "    FRAME 0:\n      IDENTITY      : \#{identity}\n    FRAME 1:\n      PROTOCOL      : \#{protocol}\n    FRAME 2:\n      TYPE          : \#{type}\n    FRAME 3:\n      RID           : \#{rid}\n    FRAME 4:\n      SID           : \#{address.sid}\n      VERB          : \#{address.verb}\n      SVERSION      : \#{address.sversion}\n    FRAME 5:\n      HEADERS       : \#{headers.to_h}\n    FRAME 6:\n      STATUS        : \#{status}\n    FRAME 7:\n      PAYLOAD       : \#{payload}\n  out\nend\n"