Sha256: 21e61ecd365eb701644fb6d5c9c0ac99db8f4de9ab9d082e9a20ff5343b2206a

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

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 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 PipeRpc::ErrorResponse.new(id: @body[:id], error: { code: -32600,
              data: { message: e.message } })
        nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pipe_rpc-1.1.2 lib/pipe_rpc/hub_message.rb
pipe_rpc-1.1.1 lib/pipe_rpc/hub_message.rb
pipe_rpc-1.1.0 lib/pipe_rpc/hub_message.rb