Class: Slack::RealTime::Client

Inherits:
Object
  • Object
show all
Includes:
Api::Message, Api::MessageId, Api::Ping, Api::Typing
Defined in:
lib/slack/real_time/client.rb

Defined Under Namespace

Classes: ClientAlreadyStartedError, ClientNotStartedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Api::Typing

#typing

Methods included from Api::Message

#message

Methods included from Api::Ping

#ping

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
# File 'lib/slack/real_time/client.rb', line 15

def initialize(options = {})
  @callbacks = {}
  Slack::RealTime::Config::ATTRIBUTES.each do |key|
    send("#{key}=", options[key] || Slack::RealTime.config.send(key))
  end
  @token ||= Slack.config.token
  @web_client = Slack::Web::Client.new(token: token)
end

Instance Attribute Details

#web_clientObject

Returns the value of attribute web_client.



12
13
14
# File 'lib/slack/real_time/client.rb', line 12

def web_client
  @web_client
end

Class Method Details

.configObject



76
77
78
# File 'lib/slack/real_time/client.rb', line 76

def config
  Config
end

.configureObject



72
73
74
# File 'lib/slack/real_time/client.rb', line 72

def configure
  block_given? ? yield(Config) : Config
end

Instance Method Details

#on(type, &block) ⇒ Object



30
31
32
33
34
# File 'lib/slack/real_time/client.rb', line 30

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

#start!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/slack/real_time/client.rb', line 36

def start!
  fail ClientAlreadyStartedError if started?
  EM.run do
    @options = web_client.rtm_start

    socket_options = {}
    socket_options[:ping] = websocket_ping if websocket_ping
    socket_options[:proxy] = websocket_proxy if websocket_proxy
    @socket = Slack::RealTime::Socket.new(@options['url'], socket_options)

    @socket.connect! do |ws|
      ws.on :open do |event|
        open(event)
      end

      ws.on :message do |event|
        dispatch(event)
      end

      ws.on :close do |event|
        close(event)
      end
    end
  end
end

#started?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/slack/real_time/client.rb', line 67

def started?
  @socket && @socket.connected?
end

#stop!Object



62
63
64
65
# File 'lib/slack/real_time/client.rb', line 62

def stop!
  fail ClientNotStartedError unless started?
  @socket.disconnect! if @socket
end