class DarkPrism::Dispatcher

Attributes

enable_sentry[RW]
listeners[R]
logger[RW]

Public Class Methods

new() click to toggle source
# File lib/dark_prism/dispatcher.rb, line 7
def initialize
  @listeners = {}.with_indifferent_access
end

Public Instance Methods

add_listener(name, listener) click to toggle source
# File lib/dark_prism/dispatcher.rb, line 55
def add_listener(name, listener)
  unless listener.respond_to?(name)
    message = "#{listener.class.name} cannot respond to #{name}"
    raise ArgumentError, message
  end

  listeners[name] = [] unless listeners.dig(name)
  listeners[name] << listener unless include_listener?(name, listener)
end
add_listeners(event_name, listeners) click to toggle source
# File lib/dark_prism/dispatcher.rb, line 65
def add_listeners(event_name, listeners)
  listeners.each do |l|
    add_listener(event_name, l)
  end
end
clear_all_listeners() click to toggle source
# File lib/dark_prism/dispatcher.rb, line 82
def clear_all_listeners
  @listeners = {}
end
dispatch(event_name, obj) click to toggle source
# File lib/dark_prism/dispatcher.rb, line 11
def dispatch(event_name, obj)
  return unless @listeners.include?(event_name)

  @listeners.dig(event_name).each do |listener|
    listener.send(event_name, obj)
  end

  true
end
dispatch_pubsub(topic_name, obj, attributes = nil) click to toggle source
# File lib/dark_prism/dispatcher.rb, line 21
def dispatch_pubsub(topic_name, obj, attributes = nil)
  return unless obj.respond_to? :to_pubsub

  message = obj.to_pubsub
  topic = pubsub.topic topic_name
  unless topic.present?
    raise ArgumentError.new('Topic not found. Please create the pubsub topic and try again')
  end

  topic.publish message, attributes
end
dispatch_pubsub_async(topic_name, obj, attributes = nil) click to toggle source
# File lib/dark_prism/dispatcher.rb, line 33
def dispatch_pubsub_async(topic_name, obj, attributes = nil)
  return unless obj.respond_to? :to_pubsub

  message = obj.to_pubsub
  topic = pubsub.topic topic_name
  unless topic.present?
    raise ArgumentError.new('Topic not found. Please create the pubsub topic and try again')
  end

  topic.publish_async message, attributes do |result|
    if result.succeeded?
      logger.info result.data
    else
      if enable_sentry
        Raven.capture_exception(result.error)
      else
        logger.error result.data, result.error
      end
    end
  end
end
remove_listener(event_name, listener) click to toggle source
# File lib/dark_prism/dispatcher.rb, line 71
def remove_listener(event_name, listener)
  return unless listeners[event_name].present?

  listeners[event_name].each_with_index do |l, idx|
    if l.equal?(listener)
      listeners[event_name].delete_at(idx)
      break
    end
  end
end

Private Instance Methods

include_listener?(event_name, listener) click to toggle source
# File lib/dark_prism/dispatcher.rb, line 88
def include_listener?(event_name, listener)
  listeners[event_name].select do |l|
    l.class == listener.class
  end.any?
end
pubsub() click to toggle source
# File lib/dark_prism/dispatcher.rb, line 94
def pubsub
  DarkPrism::Config::GcloudConfig.instance.pubsub
end