Sha256: 13fd33453a69e91aa5a8977c853c342cf02d3b760c250ace1f128019749497a0

Contents?: true

Size: 1.37 KB

Versions: 19

Compression:

Stored size: 1.37 KB

Contents

module Bixby
  module WebSocket

    class Message

      attr_reader :id, :type, :headers, :body

      def initialize(id=nil, type="rpc", headers=nil)
        @id = id || SecureRandom.uuid
        @type = type
        @headers = headers || {}
      end

      def self.from_wire(body)
        obj = MultiJson.load(body)

        clazz = case obj["type"]
        when "rpc", "connect"
          Request
        when "rpc_result"
          Response
        end

        req = clazz.allocate
        req.instance_eval do
          @id = obj["id"]
          @type = obj["type"]
          @body = obj["data"]
          if obj.include? "headers" then
            @headers = obj["headers"]
          end
        end

        return req
      end

      def to_wire
        hash = { :type    => @type,
                 :id      => @id,
                 :headers => @headers,
                 :data    => @body }

        MultiJson.dump(hash)
      end

      # Convert object to String, useful for debugging
      #
      # @return [String]
      def to_s # :nocov:
        s = []
        s << "#{self.class}:#{self.object_id}"
        s << "  id:       #{self.id}"
        s << "  type:     #{self.type}"
        s << "  headers:  " + Debug.pretty_hash(self.headers)
        s << "  body:     " + Debug.pretty_hash(MultiJson.load(self.body))
        s.join("\n")
      end # :nocov:

    end

  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
bixby-common-0.7.1 lib/bixby-common/websocket/message.rb
bixby-common-0.7.0 lib/bixby-common/websocket/message.rb
bixby-common-0.6.6 lib/bixby-common/websocket/message.rb
bixby-common-0.6.5 lib/bixby-common/websocket/message.rb
bixby-common-0.6.4 lib/bixby-common/websocket/message.rb
bixby-common-0.6.3 lib/bixby-common/websocket/message.rb
bixby-common-0.6.2 lib/bixby-common/websocket/message.rb
bixby-common-0.6.1 lib/bixby-common/websocket/message.rb
bixby-common-0.6.0 lib/bixby-common/websocket/message.rb
bixby-common-0.5.0 lib/bixby-common/websocket/message.rb
bixby-common-0.4.13 lib/bixby-common/websocket/message.rb
bixby-common-0.4.12 lib/bixby-common/websocket/message.rb
bixby-common-0.4.11 lib/bixby-common/websocket/message.rb
bixby-common-0.4.10 lib/bixby-common/websocket/message.rb
bixby-common-0.4.9 lib/bixby-common/websocket/message.rb
bixby-common-0.4.8 lib/bixby-common/websocket/message.rb
bixby-common-0.4.7 lib/bixby-common/websocket/message.rb
bixby-common-0.4.6 lib/bixby-common/websocket/message.rb
bixby-common-0.4.5 lib/bixby-common/websocket/message.rb