Module: Nagare::Publisher

Defined in:
lib/nagare/publisher.rb

Overview

Publisher is a mixin that allows classes to easily publish events to a redis stream.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



61
62
63
# File 'lib/nagare/publisher.rb', line 61

def included(base)
  base.extend ClassMethods
end

Instance Method Details

#publish(event_name, data, stream = nil) ⇒ Object

Publishes a message to the configured stream for this class.

The message is always in the format { event_name: data } hence the 2 separate parameters for this method.

Event name will be used on the listener side to determine which method of the listener to invoke.

matches a method on a listener on this stream, that method will be invoked upon receiving the message

Parameters:

  • event_name (String)

    event_name name of the event. If it

  • data (Object)

    an object representing the data

  • stream (String) (defaults to: nil)

    name of the stream to publish to



43
44
45
46
47
48
# File 'lib/nagare/publisher.rb', line 43

def publish(event_name, data, stream = nil)
  stream ||= stream_name
  Nagare.logger.info "Publishing to stream #{stream}: "\
    "#{event_name}: #{data}"
  Nagare::RedisStreams.publish(stream, event_name, data.to_json)
end

#stream_nameString

Returns the name of the configured or default stream for this publisher class.

Returns:

  • (String)

    stream name



55
56
57
58
# File 'lib/nagare/publisher.rb', line 55

def stream_name
  own_class = self.class
  own_class.redis_publisher_stream || own_class.name.downcase
end