Class: Maitredee::Publisher
- Inherits:
-
Object
- Object
- Maitredee::Publisher
- Defined in:
- lib/maitredee/publisher.rb
Overview
Inherit from this class to easily publish messages:
class RecipePublisher < Maitredee::Publisher
publish_defaults(
topic_name: :default_topic,
event_name: :optional_default_event_name,
schema_name: :default_schema
)
attr_reader :recipe
def initialize(recipe)
@recipe = recipe
end
def process
publish(
topic_name: :my_topic_override,
event_name: :event_name_is_optional,
schema_name: :schema_name,
primary_key: "optionalKey",
body: {
id: recipe.id,
name: recipe.name
}
)
end
end
Then in your Rails app, you can do this:
RecipePublisher.call(1, 2, 3)
Class Method Summary collapse
-
.call(*args) ⇒ Object
call #process and return publishes messages.
- .get_publish_defaults ⇒ Object
-
.publish_defaults(topic_name: nil, event_name: nil, schema_name: nil) ⇒ Object
set publish defaults.
Instance Method Summary collapse
-
#publish(topic_name: nil, event_name: nil, schema_name: nil, primary_key: nil, body:) ⇒ Object
publish a message with defaults.
-
#published_messages ⇒ Array<PublisherMessage>
array of messages published in this instance.
Class Method Details
.call(*args) ⇒ Object
call #process and return publishes messages
39 40 41 42 43 |
# File 'lib/maitredee/publisher.rb', line 39 def call(*args) publisher = new(*args) publisher.process publisher. end |
.get_publish_defaults ⇒ Object
57 58 59 |
# File 'lib/maitredee/publisher.rb', line 57 def get_publish_defaults @publish_defaults end |
.publish_defaults(topic_name: nil, event_name: nil, schema_name: nil) ⇒ Object
set publish defaults
49 50 51 52 53 54 55 |
# File 'lib/maitredee/publisher.rb', line 49 def publish_defaults(topic_name: nil, event_name: nil, schema_name: nil) @publish_defaults = { topic_name: topic_name, event_name: event_name, schema_name: schema_name } end |
Instance Method Details
#publish(topic_name: nil, event_name: nil, schema_name: nil, primary_key: nil, body:) ⇒ Object
publish a message with defaults
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/maitredee/publisher.rb', line 74 def publish(topic_name: nil, event_name: nil, schema_name: nil, primary_key: nil, body:) defaults = self.class.get_publish_defaults << Maitredee.publish( topic_name: topic_name || defaults[:topic_name], event_name: event_name || defaults[:event_name], schema_name: schema_name || defaults[:schema_name], primary_key: primary_key, body: body ) end |
#published_messages ⇒ Array<PublisherMessage>
array of messages published in this instance
64 65 66 |
# File 'lib/maitredee/publisher.rb', line 64 def ||= [] end |