Class: SlackRubyBot::Commands::Base
- Inherits:
-
Object
- Object
- SlackRubyBot::Commands::Base
show all
- Includes:
- Loggable
- Defined in:
- lib/slack-ruby-bot/commands/base.rb
Class Method Summary
collapse
-
.command(*values, &block) ⇒ Object
-
.default_command_name ⇒ Object
-
.invoke(client, data) ⇒ Object
-
.match(match, &block) ⇒ Object
-
.operator(*values, &block) ⇒ Object
-
.scan(match, &block) ⇒ Object
-
.send_gif(client, channel, keywords, options = {}) ⇒ Object
-
.send_message(client, channel, text, options = {}) ⇒ Object
-
.send_message_with_gif(client, channel, text, keywords, options = {}) ⇒ Object
Class Method Details
.command(*values, &block) ⇒ Object
36
37
38
39
|
# File 'lib/slack-ruby-bot/commands/base.rb', line 36
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
end
|
.default_command_name ⇒ Object
27
28
29
|
# File 'lib/slack-ruby-bot/commands/base.rb', line 27
def default_command_name
name && name.split(':').last.downcase
end
|
.invoke(client, data) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/slack-ruby-bot/commands/base.rb', line 41
def invoke(client, data)
finalize_routes!
expression, text = parse(client, data)
called = false
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
call = options[:call]
if call
call.call(client, data, match)
elsif respond_to?(:call)
send(:call, client, data, match)
else
raise NotImplementedError, data.text
end
break
end if expression
called
end
|
.match(match, &block) ⇒ Object
71
72
73
74
|
# File 'lib/slack-ruby-bot/commands/base.rb', line 71
def match(match, &block)
self.routes ||= {}
self.routes[match] = { match_method: :match, call: block }
end
|
.operator(*values, &block) ⇒ Object
31
32
33
34
|
# File 'lib/slack-ruby-bot/commands/base.rb', line 31
def operator(*values, &block)
values = values.map { |value| Regexp.escape(value) }.join('|')
match Regexp.new("^(?<operator>#{values})(?<expression>.*)$", Regexp::IGNORECASE), &block
end
|
.scan(match, &block) ⇒ Object
76
77
78
79
|
# File 'lib/slack-ruby-bot/commands/base.rb', line 76
def scan(match, &block)
self.routes ||= {}
self.routes[match] = { match_method: :scan, call: block }
end
|
.send_gif(client, channel, keywords, options = {}) ⇒ Object
22
23
24
25
|
# File 'lib/slack-ruby-bot/commands/base.rb', line 22
def send_gif(client, channel, keywords, options = {})
logger.warn '[DEPRECATION] `send_gif` is deprecated. Please use `client.say` instead.'
client.say(options.merge(channel: channel, text: '', gif: keywords))
end
|
.send_message(client, channel, text, options = {}) ⇒ Object
8
9
10
11
12
13
14
15
|
# File 'lib/slack-ruby-bot/commands/base.rb', line 8
def send_message(client, channel, text, options = {})
logger.warn '[DEPRECATION] `send_message` is deprecated. Please use `client.say` instead.'
if text && !text.length.empty?
client.say(options.merge(channel: channel, text: text))
else
client.say(options.merge(channel: channel, text: 'Nothing to see here.', gif: 'nothing'))
end
end
|
.send_message_with_gif(client, channel, text, keywords, options = {}) ⇒ Object
17
18
19
20
|
# File 'lib/slack-ruby-bot/commands/base.rb', line 17
def send_message_with_gif(client, channel, text, keywords, options = {})
logger.warn '[DEPRECATION] `send_message_with_gif` is deprecated. Please use `client.say` instead.'
client.say(options.merge(channel: channel, text: text, gif: keywords))
end
|