Module: Rubyists::Leopard::NatsApiServer::MessageHandling

Defined in:
lib/leopard/nats_api_server.rb

Overview

Message execution helpers shared by request/reply and JetStream transports.

Instance Method Summary collapse

Instance Method Details

#build_endpoint(parent, ept) ⇒ void (private)

This method returns an undefined value.

Builds an endpoint in the NATS service.

Parameters:

  • parent (NATS::Group)

    The parent group or service to add the endpoint to.

  • ept (Endpoint)

    The endpoint definition containing name, subject, queue, and handler. NOTE: Named ept because ‘endpoint` is a DSL method we expose, to avoid confusion.



454
455
456
457
458
# File 'lib/leopard/nats_api_server.rb', line 454

def build_endpoint(parent, ept)
  parent.endpoints.add(ept.name, subject: ept.subject, queue: ept.queue) do |raw_msg|
    process_transport_message(raw_msg, ept.handler, request_reply_callbacks.callbacks)
  end
end

#execute_handler(wrapper, handler) ⇒ Dry::Monads::Result (private)

Executes an endpoint handler within the worker instance context.

Parameters:

  • wrapper (MessageWrapper)

    The wrapped transport message.

  • handler (Proc)

    The endpoint handler block.

Returns:

  • (Dry::Monads::Result)

    The handler result.



496
497
498
# File 'lib/leopard/nats_api_server.rb', line 496

def execute_handler(wrapper, handler)
  instance_exec(wrapper, &handler)
end

#loggerObject

Returns the logger configured for the NATS API server.

Returns:

  • (Object)

    The configured logger.



443
# File 'lib/leopard/nats_api_server.rb', line 443

def logger = self.class.logger

#message_processorMessageProcessor (private)

Returns the memoized message processor for this worker instance.

Returns:



481
482
483
484
485
486
487
488
# File 'lib/leopard/nats_api_server.rb', line 481

def message_processor
  @message_processor ||= MessageProcessor.new(
    wrapper_factory: MessageWrapper.method(:new),
    middleware: -> { self.class.middleware },
    execute_handler: method(:execute_handler),
    logger:,
  )
end

#process_transport_message(raw_msg, handler, callbacks) ⇒ Object (private)

Processes a raw transport message through Leopard’s middleware and callback pipeline.

Parameters:

  • raw_msg (Object)

    The raw NATS transport message.

  • handler (Proc)

    The endpoint handler block.

  • callbacks (Hash{Symbol => #call})

    Transport callbacks keyed by outcome.

Returns:

  • (Object)

    The transport-specific callback result.



467
468
469
# File 'lib/leopard/nats_api_server.rb', line 467

def process_transport_message(raw_msg, handler, callbacks)
  message_processor.process(raw_msg, handler, callbacks)
end

#request_reply_callbacksNatsRequestReplyCallbacks (private)

Returns the callback helper for request/reply endpoints.

Returns:



474
475
476
# File 'lib/leopard/nats_api_server.rb', line 474

def request_reply_callbacks
  @request_reply_callbacks ||= NatsRequestReplyCallbacks.new(logger:)
end