lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.10.4 vs lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.10.5
- old
+ new
@@ -33,28 +33,28 @@
def help(&block)
CommandsHelper.instance.capture_help(self, &block)
end
- def default_command_name
+ def command_name_from_class
name ? name.split(':').last.downcase : object_id.to_s
end
def operator(*values, &block)
values = values.map { |value| Regexp.escape(value) }.join('|')
match Regexp.new("^(?<operator>#{values})(?<expression>.*)$", Regexp::IGNORECASE), &block
end
def command(*values, &block)
- values = values.map { |value| Regexp.escape(value) }.join('|')
- match Regexp.new("^(?<bot>[[:alnum:][:punct:]@<>]*)[\\s]+(?<command>#{values})([\\s]+(?<expression>.*)|)$", Regexp::IGNORECASE), &block
+ values = values.map { |value| value.is_a?(Regexp) ? value.source : Regexp.escape(value) }.join('|')
+ match Regexp.new("^#{bot_matcher}[\\s]+(?<command>#{values})([\\s]+(?<expression>.*)|)$", Regexp::IGNORECASE), &block
end
def invoke(client, data)
finalize_routes!
expression, text = parse(client, data)
- called = false
+ return false unless expression
routes.each_pair do |route, options|
match_method = options[:match_method]
case match_method
when :match
match = route.match(expression)
@@ -63,40 +63,46 @@
next if match.names.include?('bot') && !client.name?(match['bot'])
when :scan
match = expression.scan(route)
next unless match.any?
end
- called = true
- call = options[:call]
- if call
- call.call(client, data, match) if permitted?(client, data, match)
- elsif respond_to?(:call)
- send(:call, client, data, match) if permitted?(client, data, match)
- else
- raise NotImplementedError, data.text
- end
- break
- end if expression
- called
+ call_command(client, data, match, options[:block])
+ return true
+ end
+ false
end
def match(match, &block)
self.routes ||= ActiveSupport::OrderedHash.new
- self.routes[match] = { match_method: :match, call: block }
+ self.routes[match] = { match_method: :match, block: block }
end
def scan(match, &block)
self.routes ||= ActiveSupport::OrderedHash.new
- self.routes[match] = { match_method: :scan, call: block }
+ self.routes[match] = { match_method: :scan, block: block }
end
+ def bot_matcher
+ '(?<bot>\S*)'
+ end
+
private
+ def call_command(client, data, match, block)
+ if block
+ block.call(client, data, match) if permitted?(client, data, match)
+ elsif respond_to?(:call)
+ send(:call, client, data, match) if permitted?(client, data, match)
+ else
+ raise NotImplementedError, data.text
+ end
+ end
+
def parse(client, data)
text = data.text
return text unless direct_message?(data) && message_from_another_user?(data)
- return text if bot_mentioned_in_message?(text, client.names)
+ return text if message_begins_with_bot_mention?(text, client.names)
["#{client.name} #{text}", text]
end
def direct_message?(data)
data.channel && data.channel[0] == 'D'
@@ -104,17 +110,17 @@
def message_from_another_user?(data)
data.user && data.user != SlackRubyBot.config.user_id
end
- def bot_mentioned_in_message?(text, bot_names)
+ def message_begins_with_bot_mention?(text, bot_names)
bot_names = bot_names.join('|')
!!text.downcase.match(/\A(#{bot_names})\s|\A(#{bot_names})\z/)
end
def finalize_routes!
return if routes && routes.any?
- command default_command_name
+ command command_name_from_class
end
# Intended to be overridden by subclasses to hook in an
# authorization mechanism.
def permitted?(_client, _data, _match)