class SlackRubyBot::Server

Constants

TRAPPED_SIGNALS

Attributes

aliases[RW]
token[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/slack-ruby-bot/server.rb, line 13
def initialize(options = {})
  @token = options[:token]
  @aliases = options[:aliases]

  # Hook Handling
  flush_hook_blocks

  add_hook_handlers options[:hook_handlers] || {
    hello: SlackRubyBot::Hooks::Hello.new(logger),
    message: SlackRubyBot::Hooks::Message.new
  }
end

Public Instance Methods

run() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 26
def run
  loop do
    handle_exceptions do
      handle_signals
      start!
    end
  end
end
start!() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 35
def start!
  client.start!
end
start_async() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 39
def start_async
  client.start_async
end
stop!() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 43
def stop!
  client.stop! if @client
end

Private Instance Methods

client() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 79
def client
  @client ||= begin
    client = SlackRubyBot::Client.new(aliases: aliases, token: token)
    _hooks.client = client

    client
  end
end
handle_exceptions() { || ... } click to toggle source
# File lib/slack-ruby-bot/server.rb, line 49
def handle_exceptions
  yield
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::TimeoutError, Faraday::ConnectionFailed, Faraday::SSLError => e
  logger.error e
  sleep 1 # ignore, try again
rescue StandardError => e
  logger.error e.message
  logger.error e.backtrace.join("\n")
  raise e
ensure
  @client = nil
end
handle_signals() click to toggle source
# File lib/slack-ruby-bot/server.rb, line 70
def handle_signals
  TRAPPED_SIGNALS.each do |signal|
    Signal.trap(signal) do
      stop!
      exit
    end
  end
end