travis-ci
1 of 1 new or added line in 1 file covered. (100.0%)
481 of 762 relevant lines covered (63.12%)
4.56 hits per line
1 |
require 'faye/websocket'
|
2 all except jruby-9.1.2.0 ✔ |
2 |
require 'eventmachine'
|
2 all except jruby-9.1.2.0 ✔ |
3 |
|
|
4 |
module Slack |
2 all except jruby-9.1.2.0 ✔ |
5 |
module RealTime |
2 all except jruby-9.1.2.0 ✔ |
6 |
class Client |
2 all except jruby-9.1.2.0 ✔ |
7 |
def initialize(url) |
2 all except jruby-9.1.2.0 ✔ |
8 |
@url = url
|
× |
9 |
@callbacks ||= {}
|
× |
10 |
end
|
|
11 |
|
|
12 |
def on(type, &block) |
2 all except jruby-9.1.2.0 ✔ |
13 |
@callbacks[type] ||= []
|
× |
14 |
@callbacks[type] << block
|
× |
15 |
end
|
|
16 |
|
|
17 |
def start |
2 all except jruby-9.1.2.0 ✔ |
18 |
EM.run do |
× |
19 |
ws = Faye::WebSocket::Client.new(@url) |
× |
20 |
|
|
21 |
ws.on :open do |event| |
× |
22 |
end
|
|
23 |
|
|
24 |
ws.on :message do |event| |
× |
25 |
data = JSON.parse(event.data)
|
× |
26 |
if !data["type"].nil? && !@callbacks[data["type"].to_sym].nil? |
× |
27 |
@callbacks[data["type"].to_sym].each do |c| |
× |
28 |
c.call data |
× |
29 |
end
|
|
30 |
end
|
|
31 |
end
|
|
32 |
|
|
33 |
ws.on :close do |event| |
× |
|
@callbacks[:close].each { |c| c.call } unless @callbacks[:close].nil? |
× |
35 |
EM.stop
|
× |
36 |
end
|
|
37 |
end
|
|
38 |
end
|
|
39 |
end
|
|
40 |
end
|
|
41 |
end
|