Class: Karafka::BaseResponder
- Inherits:
-
Object
- Object
- Karafka::BaseResponder
- Defined in:
- lib/karafka/base_responder.rb
Overview
Base responder from which all Karafka responders should inherit Similar to Rails responders concept. It allows us to design flow from one app to another by isolating what responses should be sent (and where) based on a given action It differs from Rails responders in the way it works: in std http request we can have one response, here we can have unlimited number of them
It has a simple API for defining where should we respond (and if it is required)
Instance Attribute Summary collapse
-
#messages_buffer ⇒ Object
readonly
Returns the value of attribute messages_buffer.
Class Method Summary collapse
-
.call(*data) ⇒ Object
A simple alias for easier standalone responder usage.
-
.topic(topic_name, options = {}) ⇒ Object
Registers a topic as on to which we will be able to respond.
Instance Method Summary collapse
-
#call(*data) ⇒ Object
Performs respond and validates that all the response requirement were met.
-
#initialize(parser_class = Karafka::App.config.parser) ⇒ Karafka::BaseResponder
constructor
Creates a responder object.
Constructor Details
#initialize(parser_class = Karafka::App.config.parser) ⇒ Karafka::BaseResponder
Creates a responder object
100 101 102 103 |
# File 'lib/karafka/base_responder.rb', line 100 def initialize(parser_class = Karafka::App.config.parser) @parser_class = parser_class @messages_buffer = {} end |
Instance Attribute Details
#messages_buffer ⇒ Object (readonly)
Returns the value of attribute messages_buffer.
70 71 72 |
# File 'lib/karafka/base_responder.rb', line 70 def @messages_buffer end |
Class Method Details
.call(*data) ⇒ Object
A simple alias for easier standalone responder usage. Instead of building it with new.call it allows (in case of usin JSON parser) to just run it directly from the class level
88 89 90 91 92 93 |
# File 'lib/karafka/base_responder.rb', line 88 def call(*data) # Just in case there were no topics defined for a responder, we initialize with # empty hash not to handle a nil case self.topics ||= {} new.call(*data) end |
.topic(topic_name, options = {}) ⇒ Object
Registers a topic as on to which we will be able to respond
76 77 78 79 80 |
# File 'lib/karafka/base_responder.rb', line 76 def topic(topic_name, = {}) self.topics ||= {} topic_obj = Responders::Topic.new(topic_name, .merge(registered: true)) self.topics[topic_obj.name] = topic_obj end |
Instance Method Details
#call(*data) ⇒ Object
We know that validators should be executed also before sending data to topics, however the implementation gets way more complicated then, that’s why we check after everything was sent using responder
Performs respond and validates that all the response requirement were met
114 115 116 117 118 119 |
# File 'lib/karafka/base_responder.rb', line 114 def call(*data) respond(*data) validate_usage! deliver! end |