class Nagare::Listener

Listener is a base class for your own listeners.

It defines default behaviour for handle_event, invoking a method on the listener with the same name as the event if such method exists.

It also adds the `stream` class method, that when used causes the listener to register itself with the listener pool for receiveing messages.

Public Class Methods

inherited(subclass) click to toggle source

The ClassMethods module is automatically loaded into child classes effectively adding the `stream` class method to the child class.`

Calls superclass method
# File lib/nagare/listener.rb, line 45
def self.inherited(subclass)
  super
  subclass.extend(ClassMethods)
end

Public Instance Methods

handle_event(event) click to toggle source

This method gets called by the ListenerPool when messages are received from redis. You may override it in your own listener if you so wish.

The default implementation works based on the following convention: Listeners define methods with the name of the event they handle.

Events in nagare are always stored in redis as { event_name: data }

# File lib/nagare/listener.rb, line 58
def handle_event(event)
  event_name = event.keys.first
  Nagare.logger.debug("Received #{event}")
  return unless respond_to?(event_name)

  send(event_name, JSON.parse(event[event_name], symbolize_names: true))
end