Class: Mailgun::Tracking::Listener

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

Overview

Represents a mechanism for event listeners to subscribe to events and for event broadcasts.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMailgun::Tracking::Listener

Initializes a new Listener object.


13
14
15
# File 'lib/mailgun/tracking/listener.rb', line 13

def initialize
  @subscribers = []
end

Instance Attribute Details

#subscribersArray<Mailgun::Tracking::Subscriber::AllMessages, Mailgun::Tracking::Subscriber::Evented> (readonly)

List of subscribers.


8
9
10
# File 'lib/mailgun/tracking/listener.rb', line 8

def subscribers
  @subscribers
end

Instance Method Details

#add_subscriber(event, callable) ⇒ Array<Mailgun::Tracking::Subscriber::AllMessages, Mailgun::Tracking::Subscriber::Evented>

Adds a subscriber to the list of subscribers for the specified event.

Parameters:

  • event (Symbol, String)

    The name of event.

  • callable (Proc, Class)

    The listener of event. The callable objects should respond to call.

Returns:


25
26
27
# File 'lib/mailgun/tracking/listener.rb', line 25

def add_subscriber(event, callable)
  @subscribers << Subscriber.for(event, callable)
end

#broadcast(event, payload) ⇒ Array<Mailgun::Tracking::Subscriber::AllMessages, Mailgun::Tracking::Subscriber::Evented>

Broadcasts an event to the subscribers.

Parameters:

  • event (String)

    The name of event.

  • payload (Hash)

    The response parameters.

Returns:


36
37
38
# File 'lib/mailgun/tracking/listener.rb', line 36

def broadcast(event, payload)
  subscribers_for(event).each { |subscriber| subscriber.call(payload) }
end