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 = Hash.new { |h, k| h[k] = [] }
  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



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

def config
  Config
end

.configureObject



60
61
62
# File 'lib/slack/real_time/client.rb', line 60

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

Instance Method Details

#on(type, &block) ⇒ Object



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

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

#start! {|driver| ... } ⇒ Object

Start RealTime client and block until it disconnects.

Yield Parameters:

  • driver (Websocket::Driver)


37
38
39
40
# File 'lib/slack/real_time/client.rb', line 37

def start!(&block)
  socket = build_socket
  socket.start_sync { run_loop(socket, &block) }
end

#start_async {|driver| ... } ⇒ Object

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

Yield Parameters:

  • driver (Websocket::Driver)


45
46
47
48
# File 'lib/slack/real_time/client.rb', line 45

def start_async(&block)
  socket = build_socket
  socket.start_async { run_loop(socket, &block) }
end

#started?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/slack/real_time/client.rb', line 55

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

#stop!Object



50
51
52
53
# File 'lib/slack/real_time/client.rb', line 50

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