Class: SlackRubyBot::Client

Inherits:
Slack::RealTime::Client
  • Object
show all
Includes:
Loggable
Defined in:
lib/slack-ruby-bot/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

included

Constructor Details

#initialize(attrs = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(attrs = {})
  super(attrs)
  @aliases = attrs[:aliases]
  @send_gifs = attrs[:send_gifs]
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



6
7
8
# File 'lib/slack-ruby-bot/client.rb', line 6

def aliases
  @aliases
end

#send_gifsObject

Returns the value of attribute send_gifs.



7
8
9
# File 'lib/slack-ruby-bot/client.rb', line 7

def send_gifs
  @send_gifs
end

Instance Method Details

#nameObject



40
41
42
# File 'lib/slack-ruby-bot/client.rb', line 40

def name
  SlackRubyBot.config.user || self.self&.name
end

#name?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def name?(name)
  name && names.include?(name.downcase)
end

#namesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/slack-ruby-bot/client.rb', line 15

def names
  [
    SlackRubyBot::Config.user,
    self.self ? self.self.name : nil,
    aliases ? aliases.map(&:downcase) : nil,
    SlackRubyBot::Config.aliases ? SlackRubyBot::Config.aliases.map(&:downcase) : nil,
    self.self && self.self.id ? "<@#{self.self.id.downcase}>" : nil,
    SlackRubyBot::Config.user_id ? "<@#{SlackRubyBot::Config.user_id.downcase}>" : nil,
    self.self && self.self.id ? "<@#{self.self.id.downcase}>:" : nil,
    SlackRubyBot::Config.user_id ? "<@#{SlackRubyBot::Config.user_id.downcase}>:" : nil,
    self.self && self.self.name ? "#{self.self.name.downcase}:" : nil,
    SlackRubyBot::Config.user ? "#{SlackRubyBot::Config.user}:" : nil
  ].compact.flatten
end

#say(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/slack-ruby-bot/client.rb', line 48

def say(options = {})
  options = options.dup
  # get GIF
  keywords = options.delete(:gif)
  # text
  text = options.delete(:text)
  if keywords && send_gifs?
    gif = begin
      Giphy.random(keywords)
          rescue StandardError => e
            logger.warn "Giphy.random: #{e.message}"
            nil
    end
  end
  text = [text, gif && gif.image_url.to_s].compact.join("\n")
  message({ text: text }.merge(options))
end

#send_gifs?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/slack-ruby-bot/client.rb', line 34

def send_gifs?
  return false unless defined?(Giphy)

  send_gifs.nil? ? SlackRubyBot::Config.send_gifs? : send_gifs
end

#urlObject



44
45
46
# File 'lib/slack-ruby-bot/client.rb', line 44

def url
  SlackRubyBot.config.url || super
end