Class: Grenache::GrapeMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/grenache/grape_message.rb

Overview

Store a single request information

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GrapeMessage.



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

def initialize(type, payload, opts={}, &block)
  @payload = payload
  @type = type
  @opts = opts
  @rid = opts[:rid]  if opts[:rid]
  @_ts = Time.now
  @block = block
end

Instance Attribute Details

#_tsObject

Returns the value of attribute _ts.



5
6
7
# File 'lib/grenache/grape_message.rb', line 5

def _ts
  @_ts
end

#optsObject

Returns the value of attribute opts.



5
6
7
# File 'lib/grenache/grape_message.rb', line 5

def opts
  @opts
end

#payloadObject

Returns the value of attribute payload.



5
6
7
# File 'lib/grenache/grape_message.rb', line 5

def payload
  @payload
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/grenache/grape_message.rb', line 5

def type
  @type
end

Class Method Details

.parse(json) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/grenache/grape_message.rb', line 16

def self.parse(json)
  rid,type,payload = Oj.load(json)
  if payload.nil?
    payload = type
    type = 'res'
  end
  new(type,payload, {rid:rid})
end

.req(type, payload) ⇒ Object



25
26
27
# File 'lib/grenache/grape_message.rb', line 25

def self.req(type,payload)
  new(type,payload)
end

.response_to(req, payload) ⇒ Object



29
30
31
# File 'lib/grenache/grape_message.rb', line 29

def self.response_to(req,payload)
  new('res',payload,{rid: req.rid})
end

Instance Method Details

#block_given?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/grenache/grape_message.rb', line 41

def block_given?
  !!@block
end

#dump_payloadObject



57
58
59
# File 'lib/grenache/grape_message.rb', line 57

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

#qhashObject



53
54
55
# File 'lib/grenache/grape_message.rb', line 53

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

#request?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/grenache/grape_message.rb', line 33

def request?
  @type != 'res'
end

#response?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/grenache/grape_message.rb', line 37

def response?
  @type == 'res'
end

#ridObject



49
50
51
# File 'lib/grenache/grape_message.rb', line 49

def rid
  @rid ||= SecureRandom.uuid
end

#to_jsonObject



61
62
63
64
65
66
67
# File 'lib/grenache/grape_message.rb', line 61

def to_json
  if response?
    Oj.dump([rid,payload])
  else
    Oj.dump([rid,type,payload])
  end
end

#yield(params = {}) ⇒ Object



45
46
47
# File 'lib/grenache/grape_message.rb', line 45

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