class Lita::Adapters::Slack::RTMConnection

@api private

Constants

MAX_MESSAGE_BYTES

Attributes

config[R]
im_mapping[R]
robot[R]
robot_id[R]
websocket[R]
websocket_url[R]

Public Class Methods

build(robot, config) click to toggle source
# File lib/lita/adapters/slack/rtm_connection.rb, line 19
def build(robot, config)
  new(robot, config, API.new(config).rtm_start)
end
new(robot, config, team_data) click to toggle source
# 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

Public Instance Methods

im_for(user_id) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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_down() click to toggle source
# 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

Private Instance Methods

log() click to toggle source
# File lib/lita/adapters/slack/rtm_connection.rb, line 84
def log
  Lita.logger
end
payload_for(channel, string) click to toggle source
# File lib/lita/adapters/slack/rtm_connection.rb, line 88
def payload_for(channel, string)
  MultiJson.dump({
    id: 1,
    type: 'message',
    text: string,
    channel: channel
  })
end
receive_message(event) click to toggle source
# File lib/lita/adapters/slack/rtm_connection.rb, line 97
def receive_message(event)
  data = MultiJson.load(event.data)

  EventLoop.defer { MessageHandler.new(robot, robot_id, data).handle }
end
safe_payload_for(channel, string) click to toggle source
# File lib/lita/adapters/slack/rtm_connection.rb, line 103
def safe_payload_for(channel, string)
  payload = payload_for(channel, string)

  if payload.size > MAX_MESSAGE_BYTES
    raise ArgumentError, "Cannot send payload greater than #{MAX_MESSAGE_BYTES} bytes."
  end

  payload
end
websocket_options() click to toggle source
# File lib/lita/adapters/slack/rtm_connection.rb, line 113
def websocket_options
  options = { ping: 10 }
  options[:proxy] = { :origin => config.proxy } if config.proxy
  options
end