class Slack::RealTime::Client

Attributes

events[RW]
callbacks[R]
store[RW]
url[RW]
web_client[RW]

Public Class Methods

config() click to toggle source
# File lib/slack/real_time/client.rb, line 76
def config
  Config
end
configure() { |config| ... } click to toggle source
# File lib/slack/real_time/client.rb, line 72
def configure
  block_given? ? yield(config) : config
end
new(options = {}) click to toggle source
# File lib/slack/real_time/client.rb, line 26
def initialize(options = {})
  @callbacks = Hash.new { |h, k| h[k] = [] }
  Slack::RealTime::Config::ATTRIBUTES.each do |key|
    send("#{key}=", options.key?(key) ? options[key] : Slack::RealTime.config.send(key))
  end
  @token ||= Slack.config.token
  @logger ||= (Slack::Config.logger || Slack::Logger.default)
  @web_client = Slack::Web::Client.new(token: token, logger: logger)
end

Public Instance Methods

on(type, &block) click to toggle source
# File lib/slack/real_time/client.rb, line 42
def on(type, &block)
  type = type.to_s
  callbacks[type] << block
end
run_loop() click to toggle source
# File lib/slack/real_time/client.rb, line 81
def run_loop
  @socket.connect! do |driver|
    @callback.call(driver) if @callback

    driver.on :open do |event|
      logger.debug("#{self.class}##{__method__}") { event.class.name }
      open(event)
      callback(event, :open)
    end

    driver.on :message do |event|
      logger.debug("#{self.class}##{__method__}") { "#{event.class}, #{event.data}" }
      dispatch(event)
    end

    driver.on :close do |event|
      logger.debug("#{self.class}##{__method__}") { event.class.name }
      callback(event, :close)
      close(event)
      callback(event, :closed)
    end
  end
end
start!(&block) click to toggle source

Start RealTime client and block until it disconnects.

# File lib/slack/real_time/client.rb, line 48
def start!(&block)
  @callback = block if block_given?
  @socket = build_socket
  @socket.start_sync(self)
end
start_async(&block) click to toggle source

Start RealTime client and return immediately. The RealTime::Client will run in the background.

# File lib/slack/real_time/client.rb, line 56
def start_async(&block)
  @callback = block if block_given?
  @socket = build_socket
  @socket.start_async(self)
end
started?() click to toggle source
# File lib/slack/real_time/client.rb, line 67
def started?
  @socket && @socket.connected?
end
stop!() click to toggle source
# File lib/slack/real_time/client.rb, line 62
def stop!
  raise ClientNotStartedError unless started?
  @socket.disconnect! if @socket
end

Protected Instance Methods

build_socket() click to toggle source

@return [Slack::RealTime::Socket]

# File lib/slack/real_time/client.rb, line 108
def build_socket
  raise ClientAlreadyStartedError if started?
  start = web_client.send(rtm_start_method, start_options)
  data = Slack::Messages::Message.new(start)
  @url = data.url
  @store = @store_class.new(data) if @store_class
  socket_class.new(@url, socket_options)
end
callback(event, type) click to toggle source
# File lib/slack/real_time/client.rb, line 157
def callback(event, type)
  callbacks = self.callbacks[type.to_s]
  return false unless callbacks
  callbacks.each do |c|
    c.call(event)
  end
  true
rescue StandardError => e
  logger.error e
  false
end
close(_event) click to toggle source
# File lib/slack/real_time/client.rb, line 148
def close(_event)
  socket = @socket
  @socket = nil

  [socket, socket_class].each do |s|
    s.close if s.respond_to?(:close)
  end
end
dispatch(event) click to toggle source
# File lib/slack/real_time/client.rb, line 169
def dispatch(event)
  return false unless event.data
  data = Slack::Messages::Message.new(JSON.parse(event.data))
  type = data.type
  return false unless type
  type = type.to_s
  logger.debug("#{self.class}##{__method__}") { data.to_s }
  run_handlers(type, data) if @store
  run_callbacks(type, data)
rescue StandardError => e
  logger.error e
  false
end
open(_event) click to toggle source
# File lib/slack/real_time/client.rb, line 146
def open(_event); end
rtm_start_method() click to toggle source
# File lib/slack/real_time/client.rb, line 117
def rtm_start_method
  if start_method
    start_method
  elsif @store_class && @store_class <= Slack::RealTime::Stores::Store
    :rtm_start
  else
    :rtm_connect
  end
end
run_callbacks(type, data) click to toggle source
# File lib/slack/real_time/client.rb, line 195
def run_callbacks(type, data)
  callbacks = self.callbacks[type]
  return false unless callbacks
  callbacks.each do |c|
    c.call(data)
  end
  true
rescue StandardError => e
  logger.error e
  false
end
run_handlers(type, data) click to toggle source
# File lib/slack/real_time/client.rb, line 183
def run_handlers(type, data)
  handlers = store.class.events[type.to_s]
  if handlers
    handlers.each do |handler|
      store.instance_exec(data, &handler)
    end
  end
rescue StandardError => e
  logger.error e
  false
end
send_json(data) click to toggle source
# File lib/slack/real_time/client.rb, line 140
def send_json(data)
  raise ClientNotStartedError unless started?
  logger.debug("#{self.class}##{__method__}") { data }
  @socket.send_data(data.to_json)
end
socket_class() click to toggle source
# File lib/slack/real_time/client.rb, line 136
def socket_class
  concurrency::Socket
end
socket_options() click to toggle source
# File lib/slack/real_time/client.rb, line 127
def socket_options
  socket_options = {}
  socket_options[:ping] = websocket_ping if websocket_ping
  socket_options[:proxy] = websocket_proxy if websocket_proxy
  socket_options[:logger] = logger
  socket_options
end