Class: Grenache::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/grenache/message.rb,
lib/grenache/request.rb

Overview

Store a single request information

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, payload, opts = {}, &block) ⇒ Message

Returns a new instance of Message.



8
9
10
11
12
13
14
# File 'lib/grenache/message.rb', line 8

def initialize(type, payload, opts={}, &block)
  @payload = payload
  @type = type
  @opts = opts
  @_ts = Time.now
  @block = block
end

Instance Attribute Details

#_tsObject

Returns the value of attribute _ts.



6
7
8
# File 'lib/grenache/message.rb', line 6

def _ts
  @_ts
end

#optsObject

Returns the value of attribute opts.



6
7
8
# File 'lib/grenache/message.rb', line 6

def opts
  @opts
end

#payloadObject

Returns the value of attribute payload.



6
7
8
# File 'lib/grenache/message.rb', line 6

def payload
  @payload
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/grenache/message.rb', line 6

def type
  @type
end

Instance Method Details

#block_given?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/grenache/message.rb', line 16

def block_given?
  !!@block
end

#dump_payloadObject



32
33
34
# File 'lib/grenache/message.rb', line 32

def dump_payload
  @dump_payload ||= Oj.dump(payload)
end

#qhashObject



28
29
30
# File 'lib/grenache/message.rb', line 28

def qhash
  "#{type}#{dump_payload}"
end

#request?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/grenache/request.rb', line 15

def request?
  @type == 'req'
end

#response?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/grenache/request.rb', line 19

def response?
  @type == 'res'
end

#ridObject



24
25
26
# File 'lib/grenache/message.rb', line 24

def rid
  @rid ||= SecureRandom.uuid
end

#to_jsonObject



36
37
38
# File 'lib/grenache/message.rb', line 36

def to_json
  Oj.dump([rid,type,payload])
end

#yield(params = {}) ⇒ Object



20
21
22
# File 'lib/grenache/message.rb', line 20

def yield(params={})
  @block.call(params)
end