Class: Slack::RealTime::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/slack/realtime/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(rtm_start_response) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/slack/realtime/client.rb', line 7

def initialize(rtm_start_response)
  @response = rtm_start_response
  @url = rtm_start_response["url"]
  @callbacks ||= {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



41
42
43
44
# 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

Instance Method Details

#on(type, &block) ⇒ Object



13
14
15
16
# File 'lib/slack/realtime/client.rb', line 13

def on(type, &block)
  @callbacks[type] ||= []
  @callbacks[type] << block
end

#respond_to?(method, include_all = false) ⇒ Boolean

Delegate to Slack::Client

Returns:

  • (Boolean)


47
48
49
# File 'lib/slack/realtime/client.rb', line 47

def respond_to?(method, include_all=false)
  return !@response[method.to_s].nil? || super
end

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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