Class: SlackRubyBot::Commands::Base

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

Direct Known Subclasses

Default, Help, Hi, Unknown

Class Method Summary collapse

Class Method Details

.command(*values, &block) ⇒ Object



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

def self.command(*values, &block)
  values.each do |value|
    match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{value})$", Regexp::IGNORECASE), &block
    match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{value})[\\s]+(?<expression>.*)$", Regexp::IGNORECASE), &block
  end
end

.default_command_nameObject



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

def self.default_command_name
  name && name.split(':').last.downcase
end

.finalize_routes!Object



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

def self.finalize_routes!
  return if self.routes && self.routes.any?
  command default_command_name
end

.get_gif_and_send(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/slack-ruby-bot/commands/base.rb', line 99

def self.get_gif_and_send(options = {})
  options = options.dup
  gif = begin
    keywords = options.delete(:keywords)
    Giphy.random(keywords)
  rescue StandardError => e
    logger.warn "Giphy.random: #{e.message}"
    nil
  end
  client = options.delete(:client)
  text = options.delete(:text)
  text = [text, gif && gif.image_url.to_s].compact.join("\n")
  send_client_message(client, { text: text }.merge(options))
end

.invoke(client, data) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/slack-ruby-bot/commands/base.rb', line 55

def self.invoke(client, data)
  self.finalize_routes!
  expression = parse(data)
  called = false
  routes.each_pair do |route, method|
    match = route.match(expression)
    next unless match
    next if match.names.include?('bot') && !SlackRubyBot.config.name?(match['bot'])
    called = true
    if method
      method.call(client, data, match)
    elsif self.respond_to?(:call)
      send(:call, client, data, match)
    else
      fail NotImplementedError, data.text
    end
    break
  end
  called
end

.loggerObject



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

def self.logger
  @logger ||= begin
    $stdout.sync = true
    Logger.new(STDOUT)
  end
end

.match(match, &block) ⇒ Object



76
77
78
79
# File 'lib/slack-ruby-bot/commands/base.rb', line 76

def self.match(match, &block)
  self.routes ||= {}
  self.routes[match] = block
end

.operator(*values, &block) ⇒ Object



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

def self.operator(*values, &block)
  values.each do |value|
    match Regexp.new("^(?<operator>\\#{value})(?<expression>.*)$", Regexp::IGNORECASE), &block
  end
end

.parse(data) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/slack-ruby-bot/commands/base.rb', line 83

def self.parse(data)
  text = data.text
  return text unless data.channel && data.channel[0] == 'D' && data.user && data.user != SlackRubyBot.config.user_id
  SlackRubyBot.config.names.each do |name|
    text.downcase.tap do |td|
      return text if td == name || td.starts_with?("#{name} ")
    end
  end
  "#{SlackRubyBot.config.user} #{text}"
end

.send_client_message(client, data) ⇒ Object



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

def self.send_client_message(client, data)
  client.message(data)
end

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



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

def self.send_gif(client, channel, keywords, options = {})
  get_gif_and_send({
    client: client,
    channel: channel,
    keywords: keywords
  }.merge(options))
end

.send_message(client, channel, text, options = {}) ⇒ Object



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

def self.send_message(client, channel, text, options = {})
  if text && text.length > 0
    send_client_message(client, { channel: channel, text: text }.merge(options))
  else
    send_message_with_gif client, channel, 'Nothing to see here.', 'nothing', options
  end
end

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



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

def self.send_message_with_gif(client, channel, text, keywords, options = {})
  get_gif_and_send({
    client: client,
    channel: channel,
    text: text,
    keywords: keywords
  }.merge(options))
end