Sha256: 950aa3f1d1b88930d5460e62c2aa95e5f75911d32cb4e6eeaa0a27daafb46b52

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module SlackRubyBot
  module Hooks
    module Message
      extend Base

      def message(data)
        data = Hashie::Mash.new(data)
        bot_name, command, arguments = parse_command(data.text)
        return unless bot_name == SlackRubyBot.config.user
        klass = command_to_class(command || 'Default')
        klass.call data, command, arguments
      end

      private

      def parse_command(text)
        return unless text
        text = '= ' + text[1..text.length] if text[0] == '='
        parts = text.split.reject(&:blank?)
        if parts && parts[0] == '='
          parts[0] = SlackRubyBot.config.user
          parts.insert 1, 'calculate'
        end
        [parts.first.downcase, parts[1].try(:downcase), parts[2..parts.length]] if parts && parts.any?
      end

      def command_classes(include_slack_ruby_bot = false)
        klasses = SlackRubyBot::Commands::Base.descendants
        klasses.reject! { |k| k.name.starts_with? 'SlackRubyBot::Commands::' } unless include_slack_ruby_bot
        klasses
      end

      def command_to_class(command)
        # prioritize implementations to built-in classes
        klass = command_classes.detect { |d| d.name.ends_with? "::#{command.titleize}" }
        klass ||= command_classes(true).detect { |d| d.name.ends_with? "::#{command.titleize}" }
        klass || SlackRubyBot::Commands::Unknown
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slack-ruby-bot-0.1.0 lib/slack-ruby-bot/hooks/message.rb