lita-slack 1.0.5 → 1.1.0

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: f17abc31f51924e4c36f96a2e4bfbdbb4ffd2d4f
4
- data.tar.gz: 1df989c692141fcc9e052ffcd701fbefda6dcfb0
3
+ metadata.gz: 5f2cd3d13734b1d4f33b836c9c72a01b7bc6f2b9
4
+ data.tar.gz: b88e3e65f52bd8b0a8856d0bfa1af5f280a4a6ae
5
5
  SHA512:
6
- metadata.gz: 5018da29c4758866976afc93ea3f649a27877b54ae1eb531e8f4af941b13e68e0f6793f3c157d6ff4b7885eddade956df031783d487c12604e8b76b08e00a2d2
7
- data.tar.gz: 5c44dace1fa28b9af419a67d1718d104173d3ba7a3f397cf060467b5cbd1a174dbbd96d9e2c6da1b7881ebb69ed597fae3e331c723b5135ff1f96261ea70effd
6
+ metadata.gz: da158116c3b5e68c6f521d31220e9551ec09bc5e10026ab25eb24633039dd50ed71aa247237ead686c4cc0179ae53ffae86fa2441df0681e280631ffac2efcef
7
+ data.tar.gz: f2315307596784cc435e4cbc908a72faad4ae383ee0be4e58df522a6a93cd4955511519110459b329fda4f8ad47332011b505884187388e94a08879440168e82
data/.travis.yml CHANGED
@@ -6,4 +6,10 @@ before_install:
6
6
  - gem update --system
7
7
  services:
8
8
  - redis-server
9
-
9
+ branches:
10
+ except:
11
+ - /^v[0-9]/
12
+ notifications:
13
+ webhooks:
14
+ urls:
15
+ - https://lita-freenode.herokuapp.com/travis
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # lita-slack
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/lita-slack.png)](http://badge.fury.io/rb/lita-slack)
4
+ [![Build Status](https://travis-ci.org/kenjij/lita-slack.png?branch=master)](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(token, stubs = nil)
12
- @token = token
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 :token
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
- Faraday.new
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, token)
18
- new(robot, token, API.new(token).rtm_start)
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, token, team_data)
22
+ def initialize(robot, config, team_data)
23
23
  @robot = robot
24
- @im_mapping = IMMapping.new(API.new(token), team_data.ims)
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(websocket_url, nil, ping: 10)
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) { log.info("Disconnected from Slack.") }
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
@@ -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.token)
14
+ @rtm_connection = RTMConnection.build(robot, config)
14
15
  rtm_connection.run
15
16
  end
16
17
 
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.0.5"
3
+ spec.version = "1.1.0"
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.}
@@ -1,10 +1,15 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Lita::Adapters::Slack::API do
4
- subject { described_class.new(token, stubs) }
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, token, rtm_start_response) }
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(token).and_return(api)
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, token)).to be_an_instance_of(described_class)
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, token)
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(token).and_return(api)
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, token).and_return(rtm_connection)
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.5
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-26 00:00:00.000000000 Z
12
+ date: 2015-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine