Sha256: 73f35afa59bd8f52492e6a9b47903a94e8cc5a1a748c213b7ef97f176bd91f97

Contents?: true

Size: 986 Bytes

Versions: 1

Compression:

Stored size: 986 Bytes

Contents

module PipeRpc
  class Hub
    class Message
      def initialize(hub, body)
        @hub = hub
        @body = body
      end

      def request?
        @body.has_key? :method
      end

      def response?
        (@body.has_key? :error or @body.has_key? :result) and !!@body[:id]
      end

      def error?
        @body.has_key? :error and @body[:id].nil?
      end

      def handler_type
        if request?
          Hub::Request
        elsif response?
          Client::Request::Response
        elsif error?
          Message::ErrorResponse
        else
          raise 'no request, result or error'
        end
      end

      def handler
        handler_type.new(@hub, @body)
      rescue => e
        @hub.socket.write ErrorResponse.new(id: @body[:id], error: { code: -32600,
              data: { message: e.message } })
        nil
      end

      def handle
        handler.tap do |handler|
          handler.handle if handler
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pipe_rpc-1.0.0 lib/pipe_rpc/hub_message.rb