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(value) ⇒ Object



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

def self.command(value)
  self.commands ||= []
  self.commands << value.to_s
end

.default_command_nameObject



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

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

.loggerObject



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

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

.operator(value) ⇒ Object



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

def self.operator(value)
  self.operators ||= []
  self.operators << value.to_s
end

.responds_to_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.responds_to_command?(command)
  commands ? commands.include?(command) : command == default_command_name
end

.responds_to_operator?(operator) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.responds_to_operator?(operator)
  operators && operators.include?(operator)
end

.send_message(channel, text) ⇒ Object



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

def self.send_message(channel, text)
  if text && text.length > 0
    Slack.chat_postMessage(channel: channel, text: text)
  else
    send_message_with_gif channel, 'Nothing to see here.', 'nothing'
  end
end

.send_message_with_gif(channel, text, keywords) ⇒ Object



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

def self.send_message_with_gif(channel, text, keywords)
  gif = begin
    Giphy.random(keywords)
  rescue StandardError => e
    logger.warn "Giphy.random: #{e.message}"
    nil
  end
  text = text + "\n" + gif.image_url.to_s if gif
  send_message channel, text
end