Class: SlackRubyBot::Server

Inherits:
Object
  • Object
show all
Includes:
Hooks::Hello, Hooks::Message
Defined in:
lib/slack-ruby-bot/server.rb

Direct Known Subclasses

App

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hooks::Message

#message

Methods included from Hooks::Base

#included

Methods included from Hooks::Hello

#hello

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



11
12
13
14
15
# File 'lib/slack-ruby-bot/server.rb', line 11

def initialize(options = {})
  @token = options[:token]
  @aliases = options[:aliases]
  @send_gifs = options.key?(:send_gifs) ? !!options[:send_gifs] : true
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



5
6
7
# File 'lib/slack-ruby-bot/server.rb', line 5

def aliases
  @aliases
end

#send_gifsObject

Returns the value of attribute send_gifs.



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

def send_gifs
  @send_gifs
end

#tokenObject

Returns the value of attribute token.



4
5
6
# File 'lib/slack-ruby-bot/server.rb', line 4

def token
  @token
end

Instance Method Details

#auth!Object



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

def auth!
  client.auth = client.web_client.auth_test
  logger.info "Welcome '#{client.auth['user']}' to the '#{client.auth['team']}' team at #{client.auth['url']}."
end

#restart!(wait = 1) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/slack-ruby-bot/server.rb', line 64

def restart!(wait = 1)
  if @async
    start_async
  else
    start!
  end
rescue StandardError => e
  sleep wait
  logger.error "#{e.message}, reconnecting in #{wait} second(s)."
  logger.debug e
  restart! [wait * 2, 60].min
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/slack-ruby-bot/server.rb', line 17

def run
  auth!
  loop do
    begin
      start!
    rescue Slack::Web::Api::Error => e
      logger.error e
      case e.message
      when 'migration_in_progress'
        sleep 1 # ignore, try again
      else
        raise e
      end
    rescue Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed, Faraday::Error::SSLError => e
      logger.error e
      sleep 1 # ignore, try again
    rescue StandardError => e
      logger.error e
      raise e
    ensure
      @client = nil
    end
  end
end

#start!Object



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

def start!
  @stopping = false
  @async = false
  client.start!
end

#start_asyncObject



53
54
55
56
57
# File 'lib/slack-ruby-bot/server.rb', line 53

def start_async
  @stopping = false
  @async = true
  client.start_async
end

#stop!Object



59
60
61
62
# File 'lib/slack-ruby-bot/server.rb', line 59

def stop!
  @stopping = true
  client.stop! if @client
end