Class: SlackRTMReceiver::Starter

Inherits:
Object
  • Object
show all
Defined in:
lib/slack-rtm-receiver/starter.rb

Overview

HTTP client to call rtm.start

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Starter

Returns a new instance of Starter.

Parameters:

  • opts (Hash)

    options for Slack web API rtm.start



29
30
31
32
33
34
35
# 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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/slack-rtm-receiver/starter.rb', line 18

def logger
  @logger
end

#optionsObject

Returns the value of attribute options.



19
20
21
# File 'lib/slack-rtm-receiver/starter.rb', line 19

def options
  @options
end

Class Method Details

.start(session, opts = {}) ⇒ SlackRTMReceiver::Starter

Create and start a Starter

Parameters:

Returns:



12
13
14
15
16
# File 'lib/slack-rtm-receiver/starter.rb', line 12

def self.start(session, opts = {})
  starter = self.new(opts)
  starter.start(session)
  return starter
end

Instance Method Details

#start(session) ⇒ Object

Parameters:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# 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