lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.6.2 vs lib/slack-ruby-bot/commands/base.rb in slack-ruby-bot-0.7.0
- old
+ new
@@ -27,47 +27,56 @@
def default_command_name
name && name.split(':').last.downcase
end
def operator(*values, &block)
- values.each do |value|
- match Regexp.new("^(?<operator>\\#{value})(?<expression>.*)$", Regexp::IGNORECASE), &block
- end
+ values = values.map { |value| Regexp.escape(value) }.join('|')
+ match Regexp.new("^(?<operator>#{values})(?<expression>.*)$", Regexp::IGNORECASE), &block
end
def command(*values, &block)
- values.each do |value|
- escaped = Regexp.escape(value)
- match Regexp.new("^(?<bot>[[:alnum:][:punct:]@<>]*)[\\s]+(?<command>#{escaped})$", Regexp::IGNORECASE), &block
- match Regexp.new("^(?<bot>[[:alnum:][:punct:]@<>]*)[\\s]+(?<command>#{escaped})[\\s]+(?<expression>.*)$", Regexp::IGNORECASE), &block
- end
+ values = values.map { |value| Regexp.escape(value) }.join('|')
+ match Regexp.new("^(?<bot>[[:alnum:][:punct:]@<>]*)[\\s]+(?<command>#{values})([\\s]+(?<expression>.*)|)$", Regexp::IGNORECASE), &block
end
def invoke(client, data)
finalize_routes!
expression, text = parse(client, data)
called = false
- routes.each_pair do |route, method|
- match = route.match(expression)
- match ||= route.match(text) if text
- next unless match
- next if match.names.include?('bot') && !client.name?(match['bot'])
+ routes.each_pair do |route, options|
+ match_method = options[:match_method]
+ case match_method
+ when :match
+ match = route.match(expression)
+ match ||= route.match(text) if text
+ next unless match
+ next if match.names.include?('bot') && !client.name?(match['bot'])
+ when :scan
+ match = expression.scan(route)
+ next unless match.any?
+ end
called = true
- if method
- method.call(client, data, match)
+ call = options[:call]
+ if call
+ call.call(client, data, match)
elsif respond_to?(:call)
send(:call, client, data, match)
else
fail NotImplementedError, data.text
end
break
- end
+ end if expression
called
end
def match(match, &block)
self.routes ||= {}
- self.routes[match] = block
+ self.routes[match] = { match_method: :match, call: block }
+ end
+
+ def scan(match, &block)
+ self.routes ||= {}
+ self.routes[match] = { match_method: :scan, call: block }
end
private
def parse(client, data)