Class: Rubyists::Leopard::MessageProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/leopard/message_processor.rb

Overview

Composes middleware around Leopard handlers and routes their results to transport callbacks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrapper_factory:, middleware:, execute_handler:, logger:) ⇒ void

Builds a reusable processor for request/reply and JetStream transports.

Parameters:

  • wrapper_factory (#call)

    Callable that wraps a raw transport message in a Rubyists::Leopard::MessageWrapper-compatible object.

  • middleware (#call)

    Callable returning the current middleware stack.

  • execute_handler (#call)

    Callable that executes the endpoint handler with the wrapped message.

  • logger (#error)

    Logger used for processing failures.



17
18
19
20
21
22
# File 'lib/leopard/message_processor.rb', line 17

def initialize(wrapper_factory:, middleware:, execute_handler:, logger:)
  @wrapper_factory = wrapper_factory
  @middleware = middleware
  @execute_handler = execute_handler
  @logger = logger
end

Instance Attribute Details

#execute_handlerObject (readonly, private)

Returns the value of attribute execute_handler.



7
8
9
# File 'lib/leopard/message_processor.rb', line 7

def execute_handler
  @execute_handler
end

#loggerObject (readonly, private)

Returns the value of attribute logger.



7
8
9
# File 'lib/leopard/message_processor.rb', line 7

def logger
  @logger
end

#middlewareObject (readonly, private)

Returns the value of attribute middleware.



7
8
9
# File 'lib/leopard/message_processor.rb', line 7

def middleware
  @middleware
end

#wrapper_factoryObject (readonly, private)

Returns the value of attribute wrapper_factory.



7
8
9
# File 'lib/leopard/message_processor.rb', line 7

def wrapper_factory
  @wrapper_factory
end

Instance Method Details

#app(callbacks, handler) ⇒ #call (private)

Builds the middleware stack around the terminal application.

Parameters:

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

    Transport callbacks keyed by outcome.

  • handler (Proc)

    The endpoint handler to execute at the core of the stack.

Returns:

  • (#call)

    The composed middleware application.



43
44
45
46
47
# File 'lib/leopard/message_processor.rb', line 43

def app(callbacks, handler)
  middleware.call.reverse_each.reduce(base_app(handler, callbacks)) do |current, (klass, args, blk)|
    klass.new(current, *args, &blk)
  end
end

#base_app(handler, callbacks) ⇒ Proc (private)

Builds the terminal application that runs the handler and dispatches transport callbacks.

Parameters:

  • handler (Proc)

    The endpoint handler to execute.

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

    Transport callbacks keyed by outcome.

Returns:

  • (Proc)

    The terminal application for the middleware chain.



55
56
57
58
59
60
61
62
63
# File 'lib/leopard/message_processor.rb', line 55

def base_app(handler, callbacks)
  lambda do |wrapper|
    result = execute_handler.call(wrapper, handler)
    process_result(wrapper, result, callbacks)
  rescue StandardError => e
    logger.error 'Error processing message: ', e
    callbacks[:on_error].call(wrapper, e)
  end
end

#process(raw_msg, handler, callbacks) ⇒ Object

Processes a raw transport message through middleware and terminal callbacks.

Parameters:

  • raw_msg (Object)

    The raw transport message from NATS.

  • handler (Proc)

    The endpoint handler to execute.

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

    Success, failure, and error callbacks for the transport.

Returns:

  • (Object)

    The transport-specific callback result.



31
32
33
# File 'lib/leopard/message_processor.rb', line 31

def process(raw_msg, handler, callbacks)
  app(callbacks, handler).call(wrapper_factory.call(raw_msg))
end

#process_result(wrapper, result, callbacks) ⇒ Object (private)

Routes a Dry::Monads::Result to the appropriate transport callback.

Parameters:

  • wrapper (MessageWrapper)

    The wrapped transport message.

  • result (Dry::Monads::Result)

    The handler result to route.

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

    Transport callbacks keyed by outcome.

Returns:

  • (Object)

    The callback return value for the routed result.

Raises:

  • (ResultError)

    If the handler returned a non-result object.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/leopard/message_processor.rb', line 73

def process_result(wrapper, result, callbacks)
  case result
  in Dry::Monads::Success
    callbacks[:on_success].call(wrapper, result)
  in Dry::Monads::Failure
    callbacks[:on_failure].call(wrapper, result)
  else
    logger.error('Unexpected result: ', result:)
    raise ResultError, "Unexpected Response from Handler, must respond with a Success or Failure monad: #{result}"
  end
end