Sha256: 519cd62fb7d9f0432d02a4dfdbfd41ed6dd76650f1782913b8b12067a000f9ed

Contents?: true

Size: 1.13 KB

Versions: 9

Compression:

Stored size: 1.13 KB

Contents

module PipeRpc
  PipeRpcErrorResponse = ErrorResponse
  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 handle
        if response?
          guarded{ handler.handle if handler }
        else
          handler.handle if handler
        end
      end

      private

      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 ||= guarded{ handler_type.new(@hub, @body) }
      end

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pipe_rpc-2.5.0 lib/pipe_rpc/hub_message.rb
pipe_rpc-2.4.0 lib/pipe_rpc/hub_message.rb
pipe_rpc-2.3.0 lib/pipe_rpc/hub_message.rb
pipe_rpc-2.2.3 lib/pipe_rpc/hub_message.rb
pipe_rpc-2.2.2 lib/pipe_rpc/hub_message.rb
pipe_rpc-2.2.1 lib/pipe_rpc/hub_message.rb
pipe_rpc-2.2.0 lib/pipe_rpc/hub_message.rb
pipe_rpc-2.1.0 lib/pipe_rpc/hub_message.rb
pipe_rpc-2.0.0 lib/pipe_rpc/hub_message.rb