Class: Grenache::GrapeMessage
- Inherits:
-
Object
- Object
- Grenache::GrapeMessage
- Defined in:
- lib/grenache/grape_message.rb
Overview
Store a single request information
Instance Attribute Summary collapse
-
#_ts ⇒ Object
Returns the value of attribute _ts.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #block_given? ⇒ Boolean
- #dump_payload ⇒ Object
-
#initialize(type, payload, opts = {}, &block) ⇒ GrapeMessage
constructor
A new instance of GrapeMessage.
- #qhash ⇒ Object
- #request? ⇒ Boolean
- #response? ⇒ Boolean
- #rid ⇒ Object
- #to_json ⇒ Object
- #yield(params = {}) ⇒ Object
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
#_ts ⇒ Object
Returns the value of attribute _ts.
5 6 7 |
# File 'lib/grenache/grape_message.rb', line 5 def _ts @_ts end |
#opts ⇒ Object
Returns the value of attribute opts.
5 6 7 |
# File 'lib/grenache/grape_message.rb', line 5 def opts @opts end |
#payload ⇒ Object
Returns the value of attribute payload.
5 6 7 |
# File 'lib/grenache/grape_message.rb', line 5 def payload @payload end |
#type ⇒ Object
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
41 42 43 |
# File 'lib/grenache/grape_message.rb', line 41 def block_given? !!@block end |
#dump_payload ⇒ Object
57 58 59 |
# File 'lib/grenache/grape_message.rb', line 57 def dump_payload @dump_payload ||= Oj.dump(payload) end |
#qhash ⇒ Object
53 54 55 |
# File 'lib/grenache/grape_message.rb', line 53 def qhash "#{type}#{dump_payload}" end |
#request? ⇒ Boolean
33 34 35 |
# File 'lib/grenache/grape_message.rb', line 33 def request? @type != 'res' end |
#response? ⇒ Boolean
37 38 39 |
# File 'lib/grenache/grape_message.rb', line 37 def response? @type == 'res' end |
#rid ⇒ Object
49 50 51 |
# File 'lib/grenache/grape_message.rb', line 49 def rid @rid ||= SecureRandom.uuid end |
#to_json ⇒ Object
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 |