lita-slack 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f2cd3d13734b1d4f33b836c9c72a01b7bc6f2b9
4
- data.tar.gz: b88e3e65f52bd8b0a8856d0bfa1af5f280a4a6ae
3
+ metadata.gz: c31d5c7f496a749e2903cabfb691bd7056e50a07
4
+ data.tar.gz: 5fa2a8da529285c189f7b2aa1c789d9252d05f36
5
5
  SHA512:
6
- metadata.gz: da158116c3b5e68c6f521d31220e9551ec09bc5e10026ab25eb24633039dd50ed71aa247237ead686c4cc0179ae53ffae86fa2441df0681e280631ffac2efcef
7
- data.tar.gz: f2315307596784cc435e4cbc908a72faad4ae383ee0be4e58df522a6a93cd4955511519110459b329fda4f8ad47332011b505884187388e94a08879440168e82
6
+ metadata.gz: 0f5c045ba0e230b6a869692221d67836af7dc92c568d31802bbf9d1e2b0351d9678f64f48762526cbac01a4bbb2e0a0f41b80db76f9fa3b550e446de05c83102
7
+ data.tar.gz: bce6039b1f1dc91d4e4930386b7e38e88cb964f346af89c7fe6ed5b2fa5ba717990a29fd9e0333f043b7d237b8acc613622535ea32a14df19ed4f27248d5f8f8
@@ -0,0 +1,23 @@
1
+ require 'eventmachine'
2
+
3
+ module Lita
4
+ module Adapters
5
+ class Slack < Adapter
6
+ class EventLoop
7
+ class << self
8
+ def defer
9
+ EM.defer { yield }
10
+ end
11
+
12
+ def run
13
+ EM.run { yield }
14
+ end
15
+
16
+ def safe_stop
17
+ EM.stop if EM.reactor_running?
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,8 +1,8 @@
1
- require 'eventmachine'
2
1
  require 'faye/websocket'
3
2
  require 'multi_json'
4
3
 
5
4
  require 'lita/adapters/slack/api'
5
+ require 'lita/adapters/slack/event_loop'
6
6
  require 'lita/adapters/slack/im_mapping'
7
7
  require 'lita/adapters/slack/message_handler'
8
8
  require 'lita/adapters/slack/user_creator'
@@ -34,7 +34,7 @@ module Lita
34
34
  end
35
35
 
36
36
  def run(queue = nil, options = {})
37
- EM.run do
37
+ EventLoop.run do
38
38
  log.debug("Connecting to the Slack Real Time Messaging API.")
39
39
  @websocket = Faye::WebSocket::Client.new(
40
40
  websocket_url,
@@ -56,7 +56,7 @@ module Lita
56
56
 
57
57
  def send_messages(channel, strings)
58
58
  strings.each do |string|
59
- websocket.send(safe_payload_for(channel, string))
59
+ EventLoop.defer { websocket.send(safe_payload_for(channel, string)) }
60
60
  end
61
61
  end
62
62
 
@@ -66,7 +66,7 @@ module Lita
66
66
  websocket.close
67
67
  end
68
68
 
69
- EM.stop if EM.reactor_running?
69
+ EventLoop.safe_stop
70
70
  end
71
71
 
72
72
  private
@@ -94,7 +94,7 @@ module Lita
94
94
  def receive_message(event)
95
95
  data = MultiJson.load(event.data)
96
96
 
97
- MessageHandler.new(robot, robot_id, data).handle
97
+ EventLoop.defer { MessageHandler.new(robot, robot_id, data).handle }
98
98
  end
99
99
 
100
100
  def safe_payload_for(channel, string)
data/lita-slack.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-slack"
3
- spec.version = "1.1.0"
3
+ spec.version = "1.1.1"
4
4
  spec.authors = ["Ken J.", "Jimmy Cuadra"]
5
5
  spec.email = ["kenjij@gmail.com", "jimmy@jimmycuadra.com"]
6
6
  spec.description = %q{Lita adapter for Slack.}
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lita::Adapters::Slack::EventLoop, lita: true do
4
+ describe '.defer' do
5
+ it 'defers the provided block with Eventmachine' do
6
+ allow(EM).to receive(:defer).and_yield
7
+ ran = false
8
+
9
+ described_class.defer { ran = true }
10
+
11
+ expect(ran).to be_truthy
12
+ end
13
+ end
14
+
15
+ describe '.run' do
16
+ it 'runs the provided block in Eventmachine' do
17
+ allow(EM).to receive(:run).and_yield
18
+ ran = false
19
+
20
+ described_class.run { ran = true }
21
+
22
+ expect(ran).to be_truthy
23
+ end
24
+ end
25
+
26
+ describe '.safe_stop' do
27
+ it 'stops Eventmachine when the reactor is running' do
28
+ described_class.run do
29
+ expect(EM.reactor_running?).to be_truthy
30
+
31
+ described_class.safe_stop
32
+ end
33
+
34
+ expect(EM.reactor_running?).to be_falsy
35
+ end
36
+
37
+ it 'does nothing when the reactor is not running' do
38
+ expect(EM).not_to receive(:stop)
39
+
40
+ described_class.safe_stop
41
+ end
42
+ end
43
+ end
@@ -67,11 +67,8 @@ describe Lita::Adapters::Slack::RTMConnection, lita: true do
67
67
  end
68
68
 
69
69
  describe "#run" do
70
- it "starts the reactor" do
71
- with_websocket(subject, queue) do |websocket|
72
- expect(EM.reactor_running?).to be_truthy
73
- end
74
- end
70
+ let(:event) { double('Event', data: '{}') }
71
+ let(:message_handler) { instance_double('Lita::Adapters::Slack::MessageHandler') }
75
72
 
76
73
  it "creates the WebSocket" do
77
74
  with_websocket(subject, queue) do |websocket|
@@ -91,14 +88,20 @@ describe Lita::Adapters::Slack::RTMConnection, lita: true do
91
88
  end
92
89
  end
93
90
 
94
- context "when the WebSocket is closed" do
95
- it "shuts down the reactor" do
96
- with_websocket(subject, queue) do |websocket|
97
- websocket.close
91
+ it "dispatches incoming data to MessageHandler" do
92
+ allow(Lita::Adapters::Slack::EventLoop).to receive(:defer).and_yield
93
+ allow(Lita::Adapters::Slack::MessageHandler).to receive(:new).with(
94
+ robot,
95
+ 'U12345678',
96
+ {}
97
+ ).and_return(message_handler)
98
98
 
99
- expect(EM.reactor_running?).to be_falsy
100
- end
101
- end
99
+ expect(message_handler).to receive(:handle)
100
+
101
+ # Testing private methods directly is bad, but it's difficult to get
102
+ # the timing right when testing it by emitting the "message" event on
103
+ # the WebSocket.
104
+ subject.send(:receive_message, event)
102
105
  end
103
106
  end
104
107
 
@@ -112,6 +115,7 @@ describe Lita::Adapters::Slack::RTMConnection, lita: true do
112
115
  allow(Faye::WebSocket::Client).to receive(:new).and_return(websocket)
113
116
  allow(websocket).to receive(:on)
114
117
  allow(websocket).to receive(:close)
118
+ allow(Lita::Adapters::Slack::EventLoop).to receive(:defer).and_yield
115
119
  end
116
120
 
117
121
  it "writes messages to the WebSocket" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken J.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-30 00:00:00.000000000 Z
12
+ date: 2015-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -182,6 +182,7 @@ files:
182
182
  - lib/lita-slack.rb
183
183
  - lib/lita/adapters/slack.rb
184
184
  - lib/lita/adapters/slack/api.rb
185
+ - lib/lita/adapters/slack/event_loop.rb
185
186
  - lib/lita/adapters/slack/im_mapping.rb
186
187
  - lib/lita/adapters/slack/message_handler.rb
187
188
  - lib/lita/adapters/slack/rtm_connection.rb
@@ -192,6 +193,7 @@ files:
192
193
  - lita-slack.gemspec
193
194
  - locales/en.yml
194
195
  - spec/lita/adapters/slack/api_spec.rb
196
+ - spec/lita/adapters/slack/event_loop_spec.rb
195
197
  - spec/lita/adapters/slack/im_mapping_spec.rb
196
198
  - spec/lita/adapters/slack/message_handler_spec.rb
197
199
  - spec/lita/adapters/slack/rtm_connection_spec.rb
@@ -227,6 +229,7 @@ specification_version: 4
227
229
  summary: Lita adapter for Slack.
228
230
  test_files:
229
231
  - spec/lita/adapters/slack/api_spec.rb
232
+ - spec/lita/adapters/slack/event_loop_spec.rb
230
233
  - spec/lita/adapters/slack/im_mapping_spec.rb
231
234
  - spec/lita/adapters/slack/message_handler_spec.rb
232
235
  - spec/lita/adapters/slack/rtm_connection_spec.rb