class SlackRubyBot::Server

Attributes

aliases[RW]
send_gifs[RW]
token[RW]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

auth!() click to toggle source
# 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) click to toggle source
# 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
run() click to toggle source
# 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!() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 47
def start!
  @stopping = false
  @async = false
  client.start!
end
start_async() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 53
def start_async
  @stopping = false
  @async = true
  client.start_async
end
stop!() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 59
def stop!
  @stopping = true
  client.stop! if @client
end

Private Instance Methods

client() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 86
def client
  @client ||= begin
    client = SlackRubyBot::Client.new(aliases: aliases, send_gifs: send_gifs, token: token)
    client.on :close do |_data|
      @client = nil
      restart! unless @stopping
    end
    hooks.each do |hook|
      client.on hook do |data|
        begin
          send hook, client, data
        rescue StandardError => e
          logger.error e
          begin
            client.message(channel: data['channel'], text: e.message) if data.key?('channel')
          rescue
            # ignore
          end
        end
      end
    end
    client
  end
end
logger() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 79
def logger
  @logger ||= begin
    $stdout.sync = true
    Logger.new(STDOUT)
  end
end