class Slack::RealTime::Client

Public Class Methods

new(rtm_start_response) click to toggle source
# File lib/slack/realtime/client.rb, line 7
def initialize(rtm_start_response)
  @response = rtm_start_response
  @url = rtm_start_response["url"]
  @callbacks ||= {}
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/slack/realtime/client.rb, line 41
def method_missing(method, *args, &block)
  return super if @response[method.to_s].nil?
  @response[method.to_s]
end
on(type, &block) click to toggle source
# File lib/slack/realtime/client.rb, line 13
def on(type, &block)
  @callbacks[type] ||= []
  @callbacks[type] << block
end
respond_to?(method, include_all=false) click to toggle source

Delegate to Slack::Client

Calls superclass method
# File lib/slack/realtime/client.rb, line 47
def respond_to?(method, include_all=false)
  return !@response[method.to_s].nil? || super
end
start() click to toggle source
# File lib/slack/realtime/client.rb, line 18
def start
  EM.run do
    ws = Faye::WebSocket::Client.new(@url, nil, ping: 30)

    ws.on :open do |event|
    end

    ws.on :message do |event|
      data = JSON.parse(event.data)
      if !data["type"].nil? && !@callbacks[data["type"].to_sym].nil?
        @callbacks[data["type"].to_sym].each do |c|
          c.call data
        end
      end
    end

    ws.on :close do |event|
      @callbacks[:close].each { |c| c.call } unless @callbacks[:close].nil?
      EM.stop
    end
  end
end