Sha256: 5e777fa8b00ab4f1b5ec760ebf321c92835de0676f9a44079b97a3970ae730f0

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module SlackRubyBotServer
  module Events
    module Config
      extend self

      ATTRIBUTES = %i[
        signing_secret
        signature_expires_in
        callbacks
      ].freeze

      attr_accessor(*Config::ATTRIBUTES)

      def reset!
        self.callbacks = Hash.new { |h, k| h[k] = [] }
        self.signing_secret = ENV['SLACK_SIGNING_SECRET']
        self.signature_expires_in = 5 * 60

        on :event, 'url_verification' do |event|
          { challenge: event[:challenge] }
        end
      end

      def on(types, value = nil, &block)
        Array(types).each do |type|
          value_key = Array(value).compact.join('/') if value
          key = [type.to_s, value_key].compact.join('/')
          callbacks[key] << block
        end
      end

      def run_callbacks(type, value, args)
        callbacks = []

        keys = ([type.to_s] + Array(value)).compact

        # more specific callbacks first
        while keys.any?
          callbacks += self.callbacks[keys.join('/')]
          keys.pop
        end

        return nil unless callbacks&.any?

        callbacks.each do |c|
          rc = c.call(args || value)
          return rc if rc
        end

        nil
      end
    end

    class << self
      def configure
        block_given? ? yield(Config) : Config
      end

      def config
        Config
      end
    end
  end
end

SlackRubyBotServer::Events::Config.reset!

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slack-ruby-bot-server-events-0.1.0 lib/slack-ruby-bot-server-events/config.rb