lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.2.0 vs lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.3.0

- old
+ new

@@ -1,56 +1,87 @@ module SlackRubyBot module Commands class Base - class_attribute :operators - class_attribute :commands + class_attribute :routes - def self.send_message(channel, text) + def self.send_message(channel, text, options = { as_user: true }) if text && text.length > 0 - Slack.chat_postMessage(channel: channel, text: text) + chat_postMessage({ channel: channel, text: text }.merge(options)) else - send_message_with_gif channel, 'Nothing to see here.', 'nothing' + send_message_with_gif channel, 'Nothing to see here.', 'nothing', options end end - def self.send_message_with_gif(channel, text, keywords) + def self.send_message_with_gif(channel, text, keywords, options = { as_user: true }) gif = begin Giphy.random(keywords) rescue StandardError => e logger.warn "Giphy.random: #{e.message}" nil end text = text + "\n" + gif.image_url.to_s if gif - send_message channel, text + send_message channel, text, options end def self.logger @logger ||= begin $stdout.sync = true Logger.new(STDOUT) end end - def self.responds_to_command?(command) - commands ? commands.include?(command) : command == default_command_name - end - def self.default_command_name name && name.split(':').last.downcase end - def self.responds_to_operator?(operator) - operators && operators.include?(operator) + def self.operator(*values, &block) + values.each do |value| + match Regexp.new("^(?<operator>\\#{value})(?<expression>.*)$", Regexp::IGNORECASE), &block + end end - def self.operator(value) - self.operators ||= [] - self.operators << value.to_s + def self.command(*values, &block) + values.each do |value| + match Regexp.new("^(?<bot>\\w*)[\\s]+(?<command>#{value})$", Regexp::IGNORECASE), &block + match Regexp.new("^(?<bot>\\w*)[\\s]+(?<command>#{value})[\\s]+(?<expression>.*)$", Regexp::IGNORECASE), &block + end end - def self.command(value) - self.commands ||= [] - self.commands << value.to_s + def self.invoke(data) + self.finalize_routes! + expression = data.text + called = false + routes.each_pair do |route, method| + match = route.match(expression) + next unless match + next if match.names.include?('bot') && match['bot'].downcase != SlackRubyBot.config.user + called = true + if method + method.call(data, match) + elsif self.respond_to?(:call) + send(:call, data, match) + else + fail NotImplementedError, data.text + end + break + end + called + end + + def self.match(match, &block) + self.routes ||= {} + self.routes[match] = block + end + + private + + def self.chat_postMessage(message) + Slack.chat_postMessage(message) + end + + def self.finalize_routes! + return if self.routes && self.routes.any? + command default_command_name end end end end