Sha256: b008a3463fcab05ead1a6f0d1e3c0bb05d21acc5b39c3b392d217e9cce6dd79c

Contents?: true

Size: 1.16 KB

Versions: 13

Compression:

Stored size: 1.16 KB

Contents

module Grenache

  # Store a single request information
  class GrapeMessage
    attr_accessor :payload, :type, :opts, :_ts

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

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

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

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

    def request?
      @type != 'res'
    end

    def response?
      @type == 'res'
    end

    def block_given?
      !!@block
    end

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

    def rid
      @rid ||= SecureRandom.uuid
    end

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

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

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
grenache-ruby-base-0.2.18 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.17 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.16 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.15 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.14 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.13 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.12 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.11 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.9 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.7 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.3 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.1 lib/grenache/grape_message.rb
grenache-ruby-base-0.2.0 lib/grenache/grape_message.rb