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

Class Attribute Summary collapse

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


25
26
27
28
29
30
31
32
33
# File 'lib/slack/real_time/client.rb', line 25

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
  @store_class = options.key?(:store_class) ? options[:store_class] : Slack::RealTime::Store
  @token ||= Slack.config.token
  @web_client = Slack::Web::Client.new(token: token)
end

Class Attribute Details

.eventsObject

Returns the value of attribute events


15
16
17
# File 'lib/slack/real_time/client.rb', line 15

def events
  @events
end

Instance Attribute Details

#storeObject

Returns the value of attribute store


19
20
21
# File 'lib/slack/real_time/client.rb', line 19

def store
  @store
end

#urlObject

Returns the value of attribute url


20
21
22
# File 'lib/slack/real_time/client.rb', line 20

def url
  @url
end

#web_clientObject

Returns the value of attribute web_client


18
19
20
# File 'lib/slack/real_time/client.rb', line 18

def web_client
  @web_client
end

Class Method Details

.configObject


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

def config
  Config
end

.configureObject


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

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

Instance Method Details

#on(type, &block) ⇒ Object


41
42
43
44
# File 'lib/slack/real_time/client.rb', line 41

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)

48
49
50
51
# File 'lib/slack/real_time/client.rb', line 48

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)

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

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

#started?Boolean


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

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

#stop!Object


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

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