Class: Lita::Adapters::Slack::RTMConnection Private

Inherits:
Object
  • Object
show all
Defined in:
lib/lita/adapters/slack/rtm_connection.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

MAX_MESSAGE_BYTES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

16_000

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(robot, config, team_data) ⇒ RTMConnection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RTMConnection.



24
25
26
27
28
29
30
31
32
33
# File 'lib/lita/adapters/slack/rtm_connection.rb', line 24

def initialize(robot, config, team_data)
  @robot = robot
  @config = config
  @im_mapping = IMMapping.new(API.new(config), team_data.ims)
  @websocket_url = team_data.websocket_url
  @robot_id = team_data.self.id

  UserCreator.create_users(team_data.users, robot, robot_id)
  RoomCreator.create_rooms(team_data.channels, robot)
end

Class Method Details

.build(robot, config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/lita/adapters/slack/rtm_connection.rb', line 19

def build(robot, config)
  new(robot, config, API.new(config).rtm_start)
end

Instance Method Details

#im_for(user_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
# File 'lib/lita/adapters/slack/rtm_connection.rb', line 35

def im_for(user_id)
  im_mapping.im_for(user_id)
end

#run(queue = nil, options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lita/adapters/slack/rtm_connection.rb', line 39

def run(queue = nil, options = {})
  EventLoop.run do
    log.debug("Connecting to the Slack Real Time Messaging API.")
    @websocket = Faye::WebSocket::Client.new(
      websocket_url,
      nil,
      websocket_options.merge(options)
    )

    websocket.on(:open) { log.debug("Connected to the Slack Real Time Messaging API.") }
    websocket.on(:message) { |event| receive_message(event) }
    websocket.on(:close) do
      log.info("Disconnected from Slack.")
      shut_down
    end
    websocket.on(:error) { |event| log.debug("WebSocket error: #{event.message}") }

    queue << websocket if queue
  end
end

#send_messages(channel, strings) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



60
61
62
63
64
# File 'lib/lita/adapters/slack/rtm_connection.rb', line 60

def send_messages(channel, strings)
  strings.each do |string|
    EventLoop.defer { websocket.send(safe_payload_for(channel, string)) }
  end
end

#shut_downObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
69
70
71
72
73
# File 'lib/lita/adapters/slack/rtm_connection.rb', line 66

def shut_down
  if websocket
    log.debug("Closing connection to the Slack Real Time Messaging API.")
    websocket.close
  end

  EventLoop.safe_stop
end