Class: BotMob::Slack::Connection

Inherits:
Connection show all
Defined in:
lib/bot_mob/slack/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, options = {}) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
# File 'lib/bot_mob/slack/connection.rb', line 6

def initialize(bot, options = {})
  @bot = bot
  @token = options['token']
  token.nil? ? set_state(:inactive) : setup
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



4
5
6
# File 'lib/bot_mob/slack/connection.rb', line 4

def bot
  @bot
end

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/bot_mob/slack/connection.rb', line 4

def token
  @token
end

Class Method Details

.setup(bot, options = {}) ⇒ Object



12
13
14
# File 'lib/bot_mob/slack/connection.rb', line 12

def self.setup(bot, options = {})
  new(bot, options)
end

Instance Method Details

#clientObject



53
54
55
# File 'lib/bot_mob/slack/connection.rb', line 53

def client
  @client ||= renew_client
end

#connectObject



22
23
24
25
26
# File 'lib/bot_mob/slack/connection.rb', line 22

def connect
  return unless active?
  set_state(:connecting)
  establish
end

#deliver(outbound_message, options = {}) ⇒ Object



16
17
18
19
20
# File 'lib/bot_mob/slack/connection.rb', line 16

def deliver(outbound_message, options = {})
  BotMob.logger.info("  Sending to Slack: \"#{options.inspect}\"\n")
  BotMob.logger.info("  Text: \"#{outbound_message.text.inspect}\"\n")
  client.web_client.chat_postMessage(outbound_message.to_h)
end

#reconnectObject



28
29
30
31
32
33
34
# File 'lib/bot_mob/slack/connection.rb', line 28

def reconnect
  return unless active?
  set_state(:reconnecting)
  renew_client
  setup
  establish
end

#refreshObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bot_mob/slack/connection.rb', line 36

def refresh
  return unless active?

  case true
  when waiting?
    connect
  when disconnected?
    reconnect
  else
    BotMob.logger.info "State[#{bot.class.to_s}##{bot.key}] - No Change"
  end
end

#welcome_messageObject



49
50
51
# File 'lib/bot_mob/slack/connection.rb', line 49

def welcome_message
  "Successfully connected, '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
end