lita-slack 1.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +7 -1
- data/README.md +7 -0
- data/lib/lita/adapters/slack/api.rb +9 -5
- data/lib/lita/adapters/slack/rtm_connection.rb +22 -7
- data/lib/lita/adapters/slack.rb +2 -1
- data/lita-slack.gemspec +1 -1
- data/spec/lita/adapters/slack/api_spec.rb +6 -1
- data/spec/lita/adapters/slack/rtm_connection_spec.rb +34 -6
- data/spec/lita/adapters/slack_spec.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: 5f2cd3d13734b1d4f33b836c9c72a01b7bc6f2b9
|
4
|
+
data.tar.gz: b88e3e65f52bd8b0a8856d0bfa1af5f280a4a6ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da158116c3b5e68c6f521d31220e9551ec09bc5e10026ab25eb24633039dd50ed71aa247237ead686c4cc0179ae53ffae86fa2441df0681e280631ffac2efcef
|
7
|
+
data.tar.gz: f2315307596784cc435e4cbc908a72faad4ae383ee0be4e58df522a6a93cd4955511519110459b329fda4f8ad47332011b505884187388e94a08879440168e82
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# lita-slack
|
2
2
|
|
3
|
+
[](http://badge.fury.io/rb/lita-slack)
|
4
|
+
[](https://travis-ci.org/kenjij/lita-slack)
|
5
|
+
|
3
6
|
**lita-slack** is an adapter for [Lita](https://www.lita.io/) that allows you to use the robot with [Slack](https://slack.com/). The current adapter is not compatible with pre-1.0.0 versions, as it now uses Slack's [Real Time Messaging API](https://api.slack.com/rtm).
|
4
7
|
|
5
8
|
## Installation
|
@@ -16,6 +19,10 @@ gem "lita-slack"
|
|
16
19
|
|
17
20
|
* `token` (String) – The bot's Slack API token. Create a bot and get its token at https://my.slack.com/services/new/lita.
|
18
21
|
|
22
|
+
### Optional attributes
|
23
|
+
|
24
|
+
* `proxy` (String) – Specify a HTTP proxy URL. (e.g. "http://squid.example.com:3128")
|
25
|
+
|
19
26
|
**Note**: When using lita-slack, the adapter will overwrite the bot's name and mention name with the values set on the server, so `config.robot.name` and `config.robot.mention_name` will have no effect.
|
20
27
|
|
21
28
|
### config.robot.admins
|
@@ -8,8 +8,8 @@ module Lita
|
|
8
8
|
module Adapters
|
9
9
|
class Slack < Adapter
|
10
10
|
class API
|
11
|
-
def initialize(
|
12
|
-
@
|
11
|
+
def initialize(config, stubs = nil)
|
12
|
+
@config = config
|
13
13
|
@stubs = stubs
|
14
14
|
end
|
15
15
|
|
@@ -33,12 +33,12 @@ module Lita
|
|
33
33
|
private
|
34
34
|
|
35
35
|
attr_reader :stubs
|
36
|
-
attr_reader :
|
36
|
+
attr_reader :config
|
37
37
|
|
38
38
|
def call_api(method, post_data = {})
|
39
39
|
response = connection.post(
|
40
40
|
"https://slack.com/api/#{method}",
|
41
|
-
{ token: token }.merge(post_data)
|
41
|
+
{ token: config.token }.merge(post_data)
|
42
42
|
)
|
43
43
|
|
44
44
|
data = parse_response(response, method)
|
@@ -52,7 +52,11 @@ module Lita
|
|
52
52
|
if stubs
|
53
53
|
Faraday.new { |faraday| faraday.adapter(:test, stubs) }
|
54
54
|
else
|
55
|
-
|
55
|
+
options = {}
|
56
|
+
unless config.proxy.nil?
|
57
|
+
options = { proxy: config.proxy }
|
58
|
+
end
|
59
|
+
Faraday.new(options)
|
56
60
|
end
|
57
61
|
end
|
58
62
|
|
@@ -14,14 +14,15 @@ module Lita
|
|
14
14
|
MAX_MESSAGE_BYTES = 16_000
|
15
15
|
|
16
16
|
class << self
|
17
|
-
def build(robot,
|
18
|
-
new(robot,
|
17
|
+
def build(robot, config)
|
18
|
+
new(robot, config, API.new(config).rtm_start)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
def initialize(robot,
|
22
|
+
def initialize(robot, config, team_data)
|
23
23
|
@robot = robot
|
24
|
-
@
|
24
|
+
@config = config
|
25
|
+
@im_mapping = IMMapping.new(API.new(config), team_data.ims)
|
25
26
|
@websocket_url = team_data.websocket_url
|
26
27
|
@robot_id = team_data.self.id
|
27
28
|
|
@@ -32,14 +33,21 @@ module Lita
|
|
32
33
|
im_mapping.im_for(user_id)
|
33
34
|
end
|
34
35
|
|
35
|
-
def run(queue = nil)
|
36
|
+
def run(queue = nil, options = {})
|
36
37
|
EM.run do
|
37
38
|
log.debug("Connecting to the Slack Real Time Messaging API.")
|
38
|
-
@websocket = Faye::WebSocket::Client.new(
|
39
|
+
@websocket = Faye::WebSocket::Client.new(
|
40
|
+
websocket_url,
|
41
|
+
nil,
|
42
|
+
websocket_options.merge(options)
|
43
|
+
)
|
39
44
|
|
40
45
|
websocket.on(:open) { log.debug("Connected to the Slack Real Time Messaging API.") }
|
41
46
|
websocket.on(:message) { |event| receive_message(event) }
|
42
|
-
websocket.on(:close)
|
47
|
+
websocket.on(:close) do
|
48
|
+
log.info("Disconnected from Slack.")
|
49
|
+
shut_down
|
50
|
+
end
|
43
51
|
websocket.on(:error) { |event| log.debug("WebSocket error: #{event.message}") }
|
44
52
|
|
45
53
|
queue << websocket if queue
|
@@ -63,6 +71,7 @@ module Lita
|
|
63
71
|
|
64
72
|
private
|
65
73
|
|
74
|
+
attr_reader :config
|
66
75
|
attr_reader :im_mapping
|
67
76
|
attr_reader :robot
|
68
77
|
attr_reader :robot_id
|
@@ -97,6 +106,12 @@ module Lita
|
|
97
106
|
|
98
107
|
payload
|
99
108
|
end
|
109
|
+
|
110
|
+
def websocket_options
|
111
|
+
options = { ping: 10 }
|
112
|
+
options[:proxy] = { :origin => config.proxy } if config.proxy
|
113
|
+
options
|
114
|
+
end
|
100
115
|
end
|
101
116
|
end
|
102
117
|
end
|
data/lib/lita/adapters/slack.rb
CHANGED
@@ -5,12 +5,13 @@ module Lita
|
|
5
5
|
class Slack < Adapter
|
6
6
|
# Required configuration attributes.
|
7
7
|
config :token, type: String, required: true
|
8
|
+
config :proxy, type: String
|
8
9
|
|
9
10
|
# Starts the connection.
|
10
11
|
def run
|
11
12
|
return if rtm_connection
|
12
13
|
|
13
|
-
@rtm_connection = RTMConnection.build(robot, config
|
14
|
+
@rtm_connection = RTMConnection.build(robot, config)
|
14
15
|
rtm_connection.run
|
15
16
|
end
|
16
17
|
|
data/lita-slack.gemspec
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Lita::Adapters::Slack::API do
|
4
|
-
subject { described_class.new(
|
4
|
+
subject { described_class.new(config, stubs) }
|
5
5
|
|
6
6
|
let(:http_status) { 200 }
|
7
7
|
let(:token) { 'abcd-1234567890-hWYd21AmMH2UHAkx29vb5c1Y' }
|
8
|
+
let(:config) { Lita::Adapters::Slack.configuration_builder.build }
|
9
|
+
|
10
|
+
before do
|
11
|
+
config.token = token
|
12
|
+
end
|
8
13
|
|
9
14
|
describe "#im_open" do
|
10
15
|
let(:channel_id) { 'D024BFF1M' }
|
@@ -2,14 +2,14 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe Lita::Adapters::Slack::RTMConnection, lita: true do
|
4
4
|
def with_websocket(subject, queue)
|
5
|
-
thread = Thread.new { subject.run(queue) }
|
5
|
+
thread = Thread.new { subject.run(queue, ping: nil) }
|
6
6
|
thread.abort_on_exception = true
|
7
7
|
yield queue.pop
|
8
8
|
subject.shut_down
|
9
9
|
thread.join
|
10
10
|
end
|
11
11
|
|
12
|
-
subject { described_class.new(robot,
|
12
|
+
subject { described_class.new(robot, config, rtm_start_response) }
|
13
13
|
|
14
14
|
let(:api) { instance_double("Lita::Adapters::Slack::API") }
|
15
15
|
let(:registry) { Lita::Registry.new }
|
@@ -24,27 +24,33 @@ describe Lita::Adapters::Slack::RTMConnection, lita: true do
|
|
24
24
|
end
|
25
25
|
let(:token) { 'abcd-1234567890-hWYd21AmMH2UHAkx29vb5c1Y' }
|
26
26
|
let(:queue) { Queue.new }
|
27
|
+
let(:proxy_url) { "http://foo:3128" }
|
28
|
+
let(:config) { Lita::Adapters::Slack.configuration_builder.build }
|
29
|
+
|
30
|
+
before do
|
31
|
+
config.token = token
|
32
|
+
end
|
27
33
|
|
28
34
|
describe ".build" do
|
29
35
|
before do
|
30
|
-
allow(Lita::Adapters::Slack::API).to receive(:new).with(
|
36
|
+
allow(Lita::Adapters::Slack::API).to receive(:new).with(config).and_return(api)
|
31
37
|
allow(api).to receive(:rtm_start).and_return(rtm_start_response)
|
32
38
|
end
|
33
39
|
|
34
40
|
it "constructs a new RTMConnection with the results of rtm.start data" do
|
35
|
-
expect(described_class.build(robot,
|
41
|
+
expect(described_class.build(robot, config)).to be_an_instance_of(described_class)
|
36
42
|
end
|
37
43
|
|
38
44
|
it "creates users with the results of rtm.start data" do
|
39
45
|
expect(Lita::Adapters::Slack::UserCreator).to receive(:create_users)
|
40
46
|
|
41
|
-
described_class.build(robot,
|
47
|
+
described_class.build(robot, config)
|
42
48
|
end
|
43
49
|
end
|
44
50
|
|
45
51
|
describe "#im_for" do
|
46
52
|
before do
|
47
|
-
allow(Lita::Adapters::Slack::API).to receive(:new).with(
|
53
|
+
allow(Lita::Adapters::Slack::API).to receive(:new).with(config).and_return(api)
|
48
54
|
allow(
|
49
55
|
Lita::Adapters::Slack::IMMapping
|
50
56
|
).to receive(:new).with(api, []).and_return(im_mapping)
|
@@ -72,6 +78,28 @@ describe Lita::Adapters::Slack::RTMConnection, lita: true do
|
|
72
78
|
expect(websocket).to be_an_instance_of(Faye::WebSocket::Client)
|
73
79
|
end
|
74
80
|
end
|
81
|
+
|
82
|
+
context "with a proxy server specified" do
|
83
|
+
before do
|
84
|
+
config.proxy = proxy_url
|
85
|
+
end
|
86
|
+
|
87
|
+
it "creates the WebSocket" do
|
88
|
+
with_websocket(subject, queue) do |websocket|
|
89
|
+
expect(websocket).to be_an_instance_of(Faye::WebSocket::Client)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
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
|
98
|
+
|
99
|
+
expect(EM.reactor_running?).to be_falsy
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
75
103
|
end
|
76
104
|
|
77
105
|
describe "#send_messages" do
|
@@ -13,7 +13,7 @@ describe Lita::Adapters::Slack, lita: true do
|
|
13
13
|
|
14
14
|
allow(
|
15
15
|
described_class::RTMConnection
|
16
|
-
).to receive(:build).with(robot,
|
16
|
+
).to receive(:build).with(robot, subject.config).and_return(rtm_connection)
|
17
17
|
allow(rtm_connection).to receive(:run)
|
18
18
|
end
|
19
19
|
|
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.0
|
4
|
+
version: 1.1.0
|
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-
|
12
|
+
date: 2015-01-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: eventmachine
|