Class: Nagare::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/nagare/listener.rb

Overview

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.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(subclass) ⇒ Object

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



45
46
47
48
# File 'lib/nagare/listener.rb', line 45

def self.inherited(subclass)
  super
  subclass.extend(ClassMethods)
end

Instance Method Details

#handle_event(event) ⇒ Object

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 }



58
59
60
61
62
63
64
# 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