class SlackRTMReceiver::Starter

HTTP client to call rtm.start

Attributes

logger[R]
options[RW]

Public Class Methods

new(opts) click to toggle source

@param opts [Hash] options for Slack web API rtm.start

# File lib/slack-rtm-receiver/starter.rb, line 29
def initialize(opts)
  @logger = SlackRTMReceiver.logger
  logger.debug 'Initializing Starter...'
  @options = opts
  @is_starting = false
  @session = nil
end
start(session, opts = {}) click to toggle source

Create and start a Starter @param session [SlackRTMReceiver::Session] @return [SlackRTMReceiver::Starter]

# File lib/slack-rtm-receiver/starter.rb, line 12
def self.start(session, opts = {})
  starter = self.new(opts)
  starter.start(session)
  return starter
end

Public Instance Methods

start(session) click to toggle source

@param session [SlackRTMReceiver::Session]

# File lib/slack-rtm-receiver/starter.rb, line 38
def start(session)
  if started?
    logger.debug 'Start requested but already running. Ignoring...'
    return nil
  end
  @is_starting = true
  @session = session
  logger.debug 'Starter is calling rtm.start...'
  baseurl = 'https://slack.com/api/rtm.start'
  http = EM::HttpRequest.new(baseurl).get(query: @options, redirects: 5)
  http.callback do
    callback(http, session)
    @is_starting = false
  end
  http.errback do
    errback(http)
    @is_starting = false
  end
end
started?() click to toggle source

@return [Boolean]

# File lib/slack-rtm-receiver/starter.rb, line 22
def started?
  return true if @is_starting
  return true if @session && @session.alive?
  return false
end

Private Instance Methods

callback(http, session) click to toggle source

EM::HttpRequest callback handler

# File lib/slack-rtm-receiver/starter.rb, line 61
def callback(http, session)
  status = http.response_header.status
  raise "Web API rtm.start failed: received HTTP status code #{status}" unless status == 200
  logger.info 'Recived rtm.start response'
  session.start(http.response)
end
errback(http) click to toggle source

EM::HttpRequest error callback handler

# File lib/slack-rtm-receiver/starter.rb, line 69
def errback(http)
  raise "Web API rtm.start failed: #{http.error}"
end