Sha256: e4634eb21b05123da0c00fce5bdc981a0964754b71e3229db2256552cc61ed01

Contents?: true

Size: 727 Bytes

Versions: 3

Compression:

Stored size: 727 Bytes

Contents

# frozen_string_literal: true

module RailwayIpc
  class Responder
    def self.respond(&block)
      @block = block
    end

    class << self
      attr_reader :block
    end

    def respond(request)
      RailwayIpc.logger.info(request, 'Responding to request')
      response = self.class.block.call(request)
      raise ResponseTypeError.new(response.class) unless response.is_a?(Google::Protobuf::MessageExts)

      response
    end

    class ResponseTypeError < StandardError
      def initialize(response_class)
        message = "`respond` block should return a Google Protobuf message that the corresponding client can handle, instead returned #{response_class}"
        super(message)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
railway-ipc-1.1.0 lib/railway_ipc/responder.rb
railway-ipc-1.0.1 lib/railway_ipc/responder.rb
railway-ipc-1.0.0 lib/railway_ipc/responder.rb