slack-rtm-receiver 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/slack-rtm-receiver/session.rb +24 -12
- data/lib/slack-rtm-receiver/starter.rb +1 -2
- data/lib/slack-rtm-receiver/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17f671256581c72171b3a58c97fc77da2da06320
|
4
|
+
data.tar.gz: 0183ff7477014ba33ed88078c0f222af1a12a52c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4504c86920010d1e1f1d94c53ad7aad13df93d66d64db291b3bef15562e013a9283dd1610fc1754fb970d12c034869249a9ac4a5c2ef98ff237d9808c10966c
|
7
|
+
data.tar.gz: f530c30d2241548e823075f287ab60389c982738db526f0a8c05e95a6c4134b73ee3a301b8fb37727851645d2f43686781e60a8a3004f20d8cdda763b2f18d77
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'json'
|
1
2
|
require 'faye/websocket'
|
2
3
|
|
3
4
|
|
@@ -7,6 +8,7 @@ module SlackRTMReceiver
|
|
7
8
|
class Session
|
8
9
|
|
9
10
|
attr_reader :logger
|
11
|
+
attr_reader :self_uid
|
10
12
|
attr_reader :websocket
|
11
13
|
alias_method :ws, :websocket
|
12
14
|
attr_reader :last_timestamp
|
@@ -15,6 +17,11 @@ module SlackRTMReceiver
|
|
15
17
|
def initialize
|
16
18
|
@logger = SlackRTMReceiver.logger
|
17
19
|
logger.debug 'Initializing Session...'
|
20
|
+
reset_vars
|
21
|
+
end
|
22
|
+
|
23
|
+
def reset_vars
|
24
|
+
@self_uid = nil
|
18
25
|
@websocket = nil
|
19
26
|
@last_timestamp = nil
|
20
27
|
@ping = nil
|
@@ -22,13 +29,18 @@ module SlackRTMReceiver
|
|
22
29
|
end
|
23
30
|
|
24
31
|
# Start RTM websocket session
|
25
|
-
# @param
|
32
|
+
# @param json [String] rtm.start response from Starter
|
26
33
|
# @return [Boolean] false if a session is already up
|
27
|
-
def start(
|
28
|
-
|
29
|
-
|
34
|
+
def start(json)
|
35
|
+
if ws
|
36
|
+
logger.info 'RTM session start requested, but already running. Ignoring...'
|
37
|
+
return false
|
38
|
+
end
|
39
|
+
h = parse_rtm_start_res(json)
|
40
|
+
@self_uid = h[:self_id]
|
41
|
+
logger.debug "I am Slack ID: #{self_uid}. Connecting to websocket...\n URL: #{h[:url]}"
|
30
42
|
opts = {ping: 60}
|
31
|
-
@websocket = Faye::WebSocket::Client.new(url, nil, opts)
|
43
|
+
@websocket = Faye::WebSocket::Client.new(h[:url], nil, opts)
|
32
44
|
|
33
45
|
ws.on :open do
|
34
46
|
logger.debug 'Websocket opened'
|
@@ -62,7 +74,7 @@ module SlackRTMReceiver
|
|
62
74
|
# @param event [Hash] RTM event; 'id' will be added automatically
|
63
75
|
# @return [Hash] the event that was sent
|
64
76
|
def send_event(event)
|
65
|
-
return nil unless
|
77
|
+
return nil unless alive?
|
66
78
|
event[:id] = new_event_id
|
67
79
|
ws.send(JSON.fast_generate(event))
|
68
80
|
logger.info "Event sent with id: #{event[:id]} type: #{event[:type]}"
|
@@ -72,7 +84,6 @@ module SlackRTMReceiver
|
|
72
84
|
|
73
85
|
# True if RTM session is alive
|
74
86
|
def alive?
|
75
|
-
# return true if ws && ts && Time.now - ts < 30
|
76
87
|
return true if ws && ts
|
77
88
|
return false
|
78
89
|
end
|
@@ -141,6 +152,11 @@ module SlackRTMReceiver
|
|
141
152
|
|
142
153
|
private
|
143
154
|
|
155
|
+
def parse_rtm_start_res(json)
|
156
|
+
hash = JSON.parse(json, {symbolize_names: true})
|
157
|
+
return {url: hash[:url], self_id: hash[:self][:id]}
|
158
|
+
end
|
159
|
+
|
144
160
|
# Handle RTM hello
|
145
161
|
def hello_handler(event)
|
146
162
|
@stats[:hello_time] = Time.now
|
@@ -185,12 +201,8 @@ module SlackRTMReceiver
|
|
185
201
|
|
186
202
|
# Reset instance variables
|
187
203
|
def cleanup
|
188
|
-
@hellotime = 0
|
189
|
-
@websocket = nil
|
190
|
-
@last_timestamp = nil
|
191
|
-
@ping = nil
|
192
204
|
stats({log: true})
|
193
|
-
|
205
|
+
reset_vars
|
194
206
|
logger.warn 'RTM session closed'
|
195
207
|
end
|
196
208
|
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'em-http'
|
2
|
-
require 'json'
|
3
2
|
|
4
3
|
|
5
4
|
module SlackRTMReceiver
|
@@ -63,7 +62,7 @@ module SlackRTMReceiver
|
|
63
62
|
status = http.response_header.status
|
64
63
|
raise "Web API rtm.start failed: received HTTP status code #{status}" unless status == 200
|
65
64
|
logger.info 'Recived rtm.start response'
|
66
|
-
session.start(
|
65
|
+
session.start(http.response)
|
67
66
|
end
|
68
67
|
|
69
68
|
# EM::HttpRequest error callback handler
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack-rtm-receiver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ken J.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|