Class: SardonyxRing::Services::SlackSocketClient

Inherits:
Object
  • Object
show all
Defined in:
lib/sardonyx_ring/services/slack_socket_client.rb

Constant Summary collapse

SLACK_API_ORIGIN =
'https://slack.com'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SlackSocketClient

Returns a new instance of SlackSocketClient.



8
9
10
11
12
13
# File 'lib/sardonyx_ring/services/slack_socket_client.rb', line 8

def initialize(options = {})
  @app_client = SlackAppClient.new(token: options[:token])
  @logger = options[:logger]
  @handler = nil
  @ws = nil
end

Instance Method Details

#connect!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sardonyx_ring/services/slack_socket_client.rb', line 19

def connect!
  require 'faye/websocket'

  open_resp = @app_client.request('apps.connections.open')
  raise open_resp unless open_resp.ok

  url = "#{open_resp.url}#{debug? ? '&debug_reconnects=true' : ''}"

  @ws = Faye::WebSocket::Client.new(url)
  @ws.on(:open, &method(:on_ws_open))
  @ws.on(:message, &method(:on_ws_message))
  @ws.on(:close, &method(:on_ws_close))
end

#on_message(&block) ⇒ Object



15
16
17
# File 'lib/sardonyx_ring/services/slack_socket_client.rb', line 15

def on_message(&block)
  @handler = block
end