Sha256: 46444fb557d0550e878eb1f0b6398c0e2d9aea1aff5bde7fc509851d7113c93e

Contents?: true

Size: 901 Bytes

Versions: 6

Compression:

Stored size: 901 Bytes

Contents

module SlackRubyBot
  module Hooks
    class Set
      attr_accessor :handlers, :client

      def initialize(client = nil)
        self.handlers = {}
        self.client = client

        @pending_flush = client.blank?
      end

      def add(hook_name, handler)
        handlers[hook_name] ||= ::Set.new
        handlers[hook_name] << handler

        register_callback(hook_name)
      end

      def client=(client)
        @client = client

        flush_handlers if @pending_flush
      end

      protected

      def register_callback(hook_name)
        return unless client # We'll delay this until client is set

        client.on hook_name do |data|
          handlers[hook_name].each do |handler|
            handler.call(client, data)
          end
        end
      end

      def flush_handlers
        handlers.keys.each { |hook| register_callback(hook) }
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
slack-ruby-bot-0.12.0 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.11.2 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.11.1 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.11.0 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.10.5 lib/slack-ruby-bot/hooks/set.rb
slack-ruby-bot-0.10.4 lib/slack-ruby-bot/hooks/set.rb