Class: Rubyists::Leopard::NatsRequestReplyCallbacks
- Inherits:
-
Object
- Object
- Rubyists::Leopard::NatsRequestReplyCallbacks
- Defined in:
- lib/leopard/nats_request_reply_callbacks.rb
Overview
Maps Leopard handler outcomes to request/reply response behavior.
Instance Method Summary collapse
-
#callbacks ⇒ Hash{Symbol => #call}
Returns transport callbacks for request/reply endpoints.
-
#initialize(logger:, failure_log_policy: nil) ⇒ void
constructor
Builds a callback set for request/reply endpoint outcomes.
-
#respond_with_error(wrapper, error) ⇒ void
private
Responds to a request with an exception payload after an unhandled error.
-
#respond_with_failure(wrapper, result) ⇒ void
private
Responds to a failed request with the failure payload.
-
#respond_with_success(wrapper, result) ⇒ void
private
Responds to a successful request with the handler payload.
Constructor Details
#initialize(logger:, failure_log_policy: nil) ⇒ void
Builds a callback set for request/reply endpoint outcomes.
14 15 16 17 18 |
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 14 def initialize(logger:, failure_log_policy: nil) @failure_log_policy = failure_log_policy || lambda do |failure| logger.error 'Error processing message: ', failure end end |
Instance Method Details
#callbacks ⇒ Hash{Symbol => #call}
Returns transport callbacks for request/reply endpoints.
23 24 25 26 27 28 29 |
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 23 def callbacks { on_success: method(:respond_with_success), on_failure: method(:respond_with_failure), on_error: method(:respond_with_error), } end |
#respond_with_error(wrapper, error) ⇒ void (private)
This method returns an undefined value.
Responds to a request with an exception payload after an unhandled error.
60 61 62 |
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 60 def respond_with_error(wrapper, error) wrapper.respond_with_error(error) end |
#respond_with_failure(wrapper, result) ⇒ void (private)
This method returns an undefined value.
Responds to a failed request with the failure payload.
49 50 51 52 |
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 49 def respond_with_failure(wrapper, result) @failure_log_policy.call(result.failure) wrapper.respond_with_error(result.failure) end |
#respond_with_success(wrapper, result) ⇒ void (private)
This method returns an undefined value.
Responds to a successful request with the handler payload.
39 40 41 |
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 39 def respond_with_success(wrapper, result) wrapper.respond(result.value!) end |