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.



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

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.



500
501
502
# File 'lib/leopard/nats_api_server.rb', line 500

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.



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

def logger = self.class.logger

#message_processorMessageProcessor (private)

Returns the memoized message processor for this worker instance.

Returns:



485
486
487
488
489
490
491
492
# File 'lib/leopard/nats_api_server.rb', line 485

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.



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

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:



475
476
477
478
479
480
# File 'lib/leopard/nats_api_server.rb', line 475

def request_reply_callbacks
  @request_reply_callbacks ||= NatsRequestReplyCallbacks.new(
    logger:,
    failure_log_policy: self.class.request_reply_failure_log_policy,
  )
end