Sha256: 6da4ebfc8fcfae7eebbe78a66c9965dad54227404eb2fa02dfb10dee8e385343

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 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
        @hash = { :type => @type, :id => @id }

        headers ||= {}
        @headers = @hash[: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
        @body
      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

4 entries across 4 versions & 1 rubygems

Version Path
bixby-common-0.4.4 lib/bixby-common/websocket/message.rb
bixby-common-0.4.3 lib/bixby-common/websocket/message.rb
bixby-common-0.4.2 lib/bixby-common/websocket/message.rb
bixby-common-0.4.1 lib/bixby-common/websocket/message.rb