Class: SlackRubyBot::Commands::Base
- Inherits:
-
Object
- Object
- SlackRubyBot::Commands::Base
- Includes:
- Loggable
- Defined in:
- lib/slack-ruby-bot/commands/base.rb
Class Attribute Summary collapse
-
.command_classes ⇒ Object
Returns the value of attribute command_classes.
Class Method Summary collapse
- .attachment(match, fields_to_scan = nil, &block) ⇒ Object
- .bot_matcher ⇒ Object
- .command(*values, &block) ⇒ Object
- .command_name_from_class ⇒ Object
- .help(&block) ⇒ Object
- .inherited(subclass) ⇒ Object
- .invoke(client, data) ⇒ Object
- .match(match, &block) ⇒ Object
- .operator(*values, &block) ⇒ Object
- .routes ⇒ 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
Methods included from Loggable
Class Attribute Details
.command_classes ⇒ Object
Returns the value of attribute command_classes.
10 11 12 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 10 def command_classes @command_classes end |
Class Method Details
.attachment(match, fields_to_scan = nil, &block) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 92 def (match, fields_to_scan = nil, &block) fields_to_scan = [fields_to_scan] unless fields_to_scan.nil? || fields_to_scan.is_a?(Array) routes[match] = { match_method: :attachment, block: block, fields_to_scan: fields_to_scan } end |
.bot_matcher ⇒ Object
101 102 103 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 101 def bot_matcher '(?<bot>\S*)' end |
.command(*values, &block) ⇒ Object
49 50 51 52 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 49 def command(*values, &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 | Regexp::MULTILINE), &block end |
.command_name_from_class ⇒ Object
40 41 42 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 40 def command_name_from_class name ? name.split(':').last.downcase : object_id.to_s end |
.help(&block) ⇒ Object
36 37 38 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 36 def help(&block) Support::Help.instance.capture_help(self, &block) end |
.inherited(subclass) ⇒ Object
12 13 14 15 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 12 def inherited(subclass) SlackRubyBot::Commands::Base.command_classes ||= [] SlackRubyBot::Commands::Base.command_classes << subclass end |
.invoke(client, data) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 54 def invoke(client, data) finalize_routes! expression, text = parse(client, data) return false unless expression || data. routes.each_pair do |route, | match_method = [:match_method] case match_method when :match next unless expression match = route.match(expression) match ||= route.match(text) if text next unless match next if match.names.include?('bot') && !client.name?(match['bot']) match = Support::Match.new(match) when :scan next unless expression match = expression.scan(route) next unless match.any? when :attachment next unless data. && !data..empty? match, , field = (data, route, [:fields_to_scan]) next unless match match = Support::Match.new(match, , field) end call_command(client, data, match, [:block]) return true end false end |
.match(match, &block) ⇒ Object
84 85 86 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 84 def match(match, &block) routes[match] = { match_method: :match, block: block } end |
.operator(*values, &block) ⇒ Object
44 45 46 47 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 44 def operator(*values, &block) values = values.map { |value| Regexp.escape(value) }.join('|') match Regexp.new("^(?<operator>#{values})(?<expression>.*)$", Regexp::IGNORECASE), &block end |
.routes ⇒ Object
105 106 107 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 105 def routes @routes ||= ActiveSupport::OrderedHash.new end |
.scan(match, &block) ⇒ Object
88 89 90 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 88 def scan(match, &block) routes[match] = { match_method: :scan, block: block } end |
.send_gif(client, channel, keywords, options = {}) ⇒ Object
31 32 33 34 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 31 def send_gif(client, channel, keywords, = {}) logger.warn '[DEPRECATION] `send_gif` is deprecated. Please use `client.say` instead.' client.say(.merge(channel: channel, text: '', gif: keywords)) end |
.send_message(client, channel, text, options = {}) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 17 def (client, channel, text, = {}) logger.warn '[DEPRECATION] `send_message` is deprecated. Please use `client.say` instead.' if text && !text.length.empty? client.say(.merge(channel: channel, text: text)) else client.say(.merge(channel: channel, text: 'Nothing to see here.', gif: 'nothing')) end end |
.send_message_with_gif(client, channel, text, keywords, options = {}) ⇒ Object
26 27 28 29 |
# File 'lib/slack-ruby-bot/commands/base.rb', line 26 def (client, channel, text, keywords, = {}) logger.warn '[DEPRECATION] `send_message_with_gif` is deprecated. Please use `client.say` instead.' client.say(.merge(channel: channel, text: text, gif: keywords)) end |