Class: SlackRubyBot::Commands::Base

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/slack-ruby-bot/commands/base.rb

Direct Known Subclasses

Bot, Default, Help, Hi, Unknown, Testing::HelloCommand

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Loggable

included

Class Attribute Details

.command_classesObject

Returns the value of attribute command_classes.



12
13
14
# File 'lib/slack-ruby-bot/commands/base.rb', line 12

def command_classes
  @command_classes
end

Class Method Details

.attachment(match, fields_to_scan = nil, &block) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/slack-ruby-bot/commands/base.rb', line 100

def attachment(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_matcherObject



109
110
111
# File 'lib/slack-ruby-bot/commands/base.rb', line 109

def bot_matcher
  '(?<bot>\S*)'
end

.command(*values, &block) ⇒ Object



51
52
53
54
# File 'lib/slack-ruby-bot/commands/base.rb', line 51

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_classObject



42
43
44
# File 'lib/slack-ruby-bot/commands/base.rb', line 42

def command_name_from_class
  name ? name.split(':').last.downcase : object_id.to_s
end

.help(&block) ⇒ Object



38
39
40
# File 'lib/slack-ruby-bot/commands/base.rb', line 38

def help(&block)
  Support::Help.instance.capture_help(self, &block)
end

.inherited(subclass) ⇒ Object



14
15
16
17
# File 'lib/slack-ruby-bot/commands/base.rb', line 14

def inherited(subclass)
  SlackRubyBot::Commands::Base.command_classes ||= []
  SlackRubyBot::Commands::Base.command_classes << subclass
end

.invoke(client, data) ⇒ Object



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
83
84
85
86
87
88
89
90
# File 'lib/slack-ruby-bot/commands/base.rb', line 56

def invoke(client, data)
  finalize_routes!
  expression, text = parse(client, data)
  return false unless expression || data.attachments

  routes.each_pair do |route, options|
    match_method = options[: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.attachments && !data.attachments.empty?

      match, attachment, field = match_attachments(data, route, options[:fields_to_scan])
      next unless match

      match = Support::Match.new(match, attachment, field)
    end
    call_command(client, data, match, options[:block])
    return true
  end
  false
end

.match(match, &block) ⇒ Object



92
93
94
# File 'lib/slack-ruby-bot/commands/base.rb', line 92

def match(match, &block)
  routes[match] = { match_method: :match, block: block }
end

.operator(*values, &block) ⇒ Object



46
47
48
49
# File 'lib/slack-ruby-bot/commands/base.rb', line 46

def operator(*values, &block)
  values = values.map { |value| Regexp.escape(value) }.join('|')
  match Regexp.new("^(?<operator>#{values})(?<expression>.*)$", Regexp::IGNORECASE), &block
end

.routesObject



113
114
115
# File 'lib/slack-ruby-bot/commands/base.rb', line 113

def routes
  @routes ||= ActiveSupport::OrderedHash.new
end

.scan(match, &block) ⇒ Object



96
97
98
# File 'lib/slack-ruby-bot/commands/base.rb', line 96

def scan(match, &block)
  routes[match] = { match_method: :scan, block: block }
end

.send_gif(client, channel, keywords, options = {}) ⇒ Object



33
34
35
36
# File 'lib/slack-ruby-bot/commands/base.rb', line 33

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



19
20
21
22
23
24
25
26
# File 'lib/slack-ruby-bot/commands/base.rb', line 19

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



28
29
30
31
# File 'lib/slack-ruby-bot/commands/base.rb', line 28

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