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

CLIENT_ID_REGEX =
/^(.+?)#/
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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zss/message.rb', line 25

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]
  @client       = nil
  @payload_size  = args[:payload_size]

  match = identity.try(:match, CLIENT_ID_REGEX)
  @client = match.captures.first if match
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



14
15
16
# File 'lib/zss/message.rb', line 14

def address
  @address
end

#clientObject

Returns the value of attribute client.



14
15
16
# File 'lib/zss/message.rb', line 14

def client
  @client
end

#headersObject

Returns the value of attribute headers.



14
15
16
# File 'lib/zss/message.rb', line 14

def headers
  @headers
end

#identityObject

Returns the value of attribute identity.



14
15
16
# File 'lib/zss/message.rb', line 14

def identity
  @identity
end

#payloadObject

Returns the value of attribute payload.



14
15
16
# File 'lib/zss/message.rb', line 14

def payload
  @payload
end

#payload_sizeObject

Returns the value of attribute payload_size.



14
15
16
# File 'lib/zss/message.rb', line 14

def payload_size
  @payload_size
end

#protocolObject

Returns the value of attribute protocol.



14
15
16
# File 'lib/zss/message.rb', line 14

def protocol
  @protocol
end

#ridObject

Returns the value of attribute rid.



14
15
16
# File 'lib/zss/message.rb', line 14

def rid
  @rid
end

#statusObject

Returns the value of attribute status.



14
15
16
# File 'lib/zss/message.rb', line 14

def status
  @status
end

#typeObject

Returns the value of attribute type.



14
15
16
# File 'lib/zss/message.rb', line 14

def type
  @type
end

Class Method Details

.parse(frames) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zss/message.rb', line 52

def self.parse(frames)

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

  payload_data = frames[7]
  payload_size = payload_data.length
  payload = MessagePack.unpack(payload_data)
  payload = Hashie::Mash.new(payload) if payload.kind_of? Hash

  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:  payload,
    payload_size: payload_size
  )

  msg
end

Instance Method Details

#big?Boolean

Returns:

  • (Boolean)


133
134
135
136
# File 'lib/zss/message.rb', line 133

def big?
  payload_size = payload_msgpack.length unless payload_size
  payload_size > 1024
end

#is_error?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/zss/message.rb', line 129

def is_error?
  status != 200
end

#payload_msgpackObject



138
139
140
141
# File 'lib/zss/message.rb', line 138

def payload_msgpack
  # this will avoid executing multiple serializations
  @payload_msgpack_data ||= payload.to_msgpack
end

#req?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/zss/message.rb', line 48

def req?
  type == Type::REQ
end

#to_framesObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/zss/message.rb', line 101

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

#to_logObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/zss/message.rb', line 114

def to_log
  {
    client: client,
    identity: identity,
    protocol: protocol,
    type: type,
    rid: rid,
    address: address,
    headers: headers,
    status: status,
    payload: big? ? "<<Message to big to log>>" : payload,
    "payload-size" => payload_size
  }
end

#to_sObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/zss/message.rb', line 78

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