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, HelpCommand, 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.



8
9
10
# File 'lib/slack-ruby-bot/commands/base.rb', line 8

def command_classes
  @command_classes
end

Class Method Details

.command(*values, &block) ⇒ Object



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

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_nameObject



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

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

.help(&block) ⇒ Object



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

def help(&block)
  CommandsHelper.instance.capture_help(self, &block)
end

.inherited(subclass) ⇒ Object



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

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

.invoke(client, data) ⇒ Object



52
53
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
# File 'lib/slack-ruby-bot/commands/base.rb', line 52

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



82
83
84
85
# File 'lib/slack-ruby-bot/commands/base.rb', line 82

def match(match, &block)
  self.routes ||= ActiveSupport::OrderedHash.new
  self.routes[match] = { match_method: :match, call: block }
end

.operator(*values, &block) ⇒ Object



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

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



87
88
89
90
# File 'lib/slack-ruby-bot/commands/base.rb', line 87

def scan(match, &block)
  self.routes ||= ActiveSupport::OrderedHash.new
  self.routes[match] = { match_method: :scan, call: block }
end

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



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

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



15
16
17
18
19
20
21
22
# File 'lib/slack-ruby-bot/commands/base.rb', line 15

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



24
25
26
27
# File 'lib/slack-ruby-bot/commands/base.rb', line 24

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