slack-ruby-client 0.1.0 → 0.2.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: bfc38c6f34626b363ad4323f0fab5a791e77ca3b
4
- data.tar.gz: dc37a02ec0492a1191f7fe1f495b704be45e344d
3
+ metadata.gz: 59058e21912e23e38cc9fe877b7edbb463b07c5b
4
+ data.tar.gz: beb3d5ee788708f684f10430764a7c9c8a70263e
5
5
  SHA512:
6
- metadata.gz: 0b37c6b2289f5337239d52b199cbf0b224f1a7d8f4c510de22295ca2686f04f15aab992eead738cc69799e63230accdea1636ca9e3e2d9e9723f9ee3985f7e2c
7
- data.tar.gz: 5441b5ebb561df58e6233b7444f154f76bd6227b7657c4d402eb444ca709954315e771dbfc99683e481a606b3897f4a14f41bc3e91f79d81380dcee61b03086d
6
+ metadata.gz: 7df8114e487363c6e5cd5b4ea6a650247ffe4c0062c58b472f2a715888246c1d906658531db405a544ac5952923dade5bb1241a71b4b64a200310e5ac5d0e2a7
7
+ data.tar.gz: 1d180b4b25cf3c8cd514e40ebe2bb98bab04d0e0f2588f1ac733f61f97de0f1a3e1115266cec3d1b5bbf64e6ee22ce24b6630c1207956042e6eb72f494360ca0
data/.rubocop_todo.yml CHANGED
@@ -1,11 +1,15 @@
1
1
  # This configuration was generated by `rubocop --auto-gen-config`
2
- # on 2015-07-25 14:51:14 -0400 using RuboCop version 0.32.1.
2
+ # on 2015-07-30 11:21:36 +0200 using RuboCop version 0.32.1.
3
3
  # The point is for the user to remove these configuration records
4
4
  # one by one as the offenses are removed from the code base.
5
5
  # Note that changes in the inspected code, or installation of new
6
6
  # versions of RuboCop, may require this file to be generated again.
7
7
 
8
- # Offense count: 77
8
+ # Offense count: 2
9
+ Metrics/AbcSize:
10
+ Max: 21
11
+
12
+ # Offense count: 102
9
13
  # Configuration parameters: AllowURI, URISchemes.
10
14
  Metrics/LineLength:
11
15
  Max: 153
@@ -13,9 +17,9 @@ Metrics/LineLength:
13
17
  # Offense count: 3
14
18
  # Configuration parameters: CountComments.
15
19
  Metrics/MethodLength:
16
- Max: 16
20
+ Max: 19
17
21
 
18
- # Offense count: 26
22
+ # Offense count: 32
19
23
  Style/Documentation:
20
24
  Enabled: false
21
25
 
@@ -29,6 +33,6 @@ Style/FileName:
29
33
  Style/MethodName:
30
34
  Enabled: false
31
35
 
32
- # Offense count: 2
36
+ # Offense count: 3
33
37
  Style/ModuleFunction:
34
38
  Enabled: false
data/.travis.yml CHANGED
@@ -1,5 +1,16 @@
1
1
  rvm:
2
2
  - 2.2
3
+ - 2.1
4
+ - 2.0.0
5
+ - rbx-2.2.10
6
+ - jruby-19mode
7
+ - ruby-head
8
+ - jruby-head
9
+
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: ruby-head
13
+ - rvm: jruby-head
3
14
 
4
15
  git:
5
16
  submodules: false
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ### 0.2.0 (7/31/2015)
2
+
3
+ * [#2](https://github.com/dblock/slack-ruby-client/pull/2): `Slack::RealTime::Socket` now pings frames every 30s, as recommended by Slack - [@samdoiron](https://github.com/samdoiron).
4
+ * [#3](https://github.com/dblock/slack-ruby-client/issues/3): RealTime client WebSocket frame ping frequency is now configurable with `Slack::RealTime::Client.websocket_ping` - [@dblock](https://github.com/dblock).
5
+ * [#3](https://github.com/dblock/slack-ruby-client/issues/3): RealTime client WebSocket proxy is now configurable with `Slack::RealTime::Client.websocket_proxy` - [@dblock](https://github.com/dblock).
6
+ * [#3](https://github.com/dblock/slack-ruby-client/issues/3): Added global `Slack::Web::Client` and `Slack::RealTime::Client` configuration options via `Slack::Web::Client.configure` and `Slack::RealTime::Client.configure` - [@dblock](https://github.com/dblock).
7
+ * Exposed `Slack::RealTime::Client.url`, `team`, `self`, `users`, `channels`, `groups`, `ims` and `bots` - [@dblock](https://github.com/dblock).
8
+ * Default user-agent for `Slack::Web::Client` now includes a slash, eg. _Slack Ruby Client/0.1.1_ - [@dblock](https://github.com/dblock).
9
+ * Fix: set `Slack::Web::Client` connection options for `ca_path`, `ca_file`, and `proxy` - [@dblock](https://github.com/dblock).
10
+
1
11
  ### 0.1.0 (7/25/2015)
2
12
 
3
13
  * Initial public release with Web and RealTime Messaging API support - [@dblock](https://github.com/dblock).
data/CONTRIBUTING.md CHANGED
@@ -1,4 +1,4 @@
1
- # Contributing to Slack
1
+ # Contributing to Slack-Ruby-Client
2
2
 
3
3
  This project is work of [many contributors](https://github.com/dblock/slack-ruby-client/graphs/contributors).
4
4
 
@@ -6,7 +6,7 @@ You're encouraged to submit [pull requests](https://github.com/dblock/slack-ruby
6
6
 
7
7
  In the examples below, substitute your Github username for `contributor` in URLs.
8
8
 
9
- ## Fork the Project
9
+ ### Fork the Project
10
10
 
11
11
  Fork the [project on Github](https://github.com/dblock/slack-ruby-client) and check out your copy.
12
12
 
@@ -16,7 +16,7 @@ cd slack-ruby-client
16
16
  git remote add upstream https://github.com/dblock/slack-ruby-client.git
17
17
  ```
18
18
 
19
- ## Bundle Install and Test
19
+ ### Bundle Install and Test
20
20
 
21
21
  Ensure that you can build the project and run tests.
22
22
 
@@ -25,9 +25,9 @@ bundle install
25
25
  bundle exec rake
26
26
  ```
27
27
 
28
- ## Run Slack in Development
28
+ ### Run Examples in Development
29
29
 
30
- Create a private slack group for yourself.
30
+ Sign up for Slack, create a private slack group for yourself.
31
31
 
32
32
  Create a new Bot Integration under [services/new/bot](http://slack.com/services/new/bot).
33
33
 
@@ -35,11 +35,11 @@ Create a new Bot Integration under [services/new/bot](http://slack.com/services/
35
35
 
36
36
  On the next screen, note the API token.
37
37
 
38
- Run `SLACK_API_TOKEN=<your API token> foreman start`.
38
+ To run [the example in examples/hi_real_time](examples/hi_real_time/hi.rb), open a terminal, cd into _examples/hi_real_time_, run `bundle install`, followed by `SLACK_API_TOKEN=<your API token> bundle exec ruby hi.rb`.
39
39
 
40
- You can also create a `.env` file with `SLACK_API_TOKEN=<your API token>` and just run `foreman start`.
40
+ ## Contribute Code
41
41
 
42
- ## Create a Topic Branch
42
+ ### Create a Topic Branch
43
43
 
44
44
  Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
45
45
 
@@ -49,32 +49,31 @@ git pull upstream master
49
49
  git checkout -b my-feature-branch
50
50
  ```
51
51
 
52
- ## Write Tests
52
+ ### Write Tests
53
53
 
54
- Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build.
55
- Add to [spec](spec).
54
+ Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add tests to [spec](spec).
56
55
 
57
56
  We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
58
57
 
59
- ## Write Code
58
+ ### Write Code
60
59
 
61
60
  Implement your feature or bug fix.
62
61
 
63
- Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop).
64
- Run `bundle exec rubocop` and fix any style issues highlighted.
62
+ Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop). Run `bundle exec rubocop` and fix any style issues highlighted, auto-correct issues when possible with `bundle exec rubocop -a`. To silence generally ingored issues, including line lengths or code complexity metrics, run `bundle exec rubocop --auto-gen-config`.
65
63
 
66
64
  Make sure that `bundle exec rake` completes without errors.
67
65
 
68
- ## Write Documentation
66
+ ### Write Documentation
69
67
 
70
68
  Document any external behavior in the [README](README.md).
71
69
 
72
- ## Update Changelog
70
+ ### Update Changelog
73
71
 
74
- Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*.
75
- Make it look like every other line, including your name and link to your Github account.
72
+ Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*. Don't remove *Your contribution here*.
76
73
 
77
- ## Commit Changes
74
+ Make it look like every other line, including a link to the issue being fixed, your name and link to your Github account.
75
+
76
+ ### Commit Changes
78
77
 
79
78
  Make sure git knows your name and email address:
80
79
 
@@ -90,28 +89,17 @@ git add ...
90
89
  git commit
91
90
  ```
92
91
 
93
- ## Push
92
+ ### Push
94
93
 
95
94
  ```
96
95
  git push origin my-feature-branch
97
96
  ```
98
97
 
99
- ## Make a Pull Request
100
-
101
- Go to https://github.com/contributor/slack-ruby-client and select your feature branch.
102
- Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
103
-
104
- ## Rebase
105
-
106
- If you've been working on a change for a while, rebase with upstream/master.
98
+ ### Make a Pull Request
107
99
 
108
- ```
109
- git fetch upstream
110
- git rebase upstream/master
111
- git push origin my-feature-branch -f
112
- ```
100
+ Go to https://github.com/contributor/slack-ruby-client and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
113
101
 
114
- ## Update CHANGELOG Again
102
+ ### Update CHANGELOG Again
115
103
 
116
104
  Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
117
105
 
@@ -126,11 +114,21 @@ git commit --amend
126
114
  git push origin my-feature-branch -f
127
115
  ```
128
116
 
129
- ## Check on Your Pull Request
117
+ ### Rebase
118
+
119
+ If you've been working on a change for a while, rebase with upstream/master.
120
+
121
+ ```
122
+ git fetch upstream
123
+ git rebase upstream/master
124
+ git push origin my-feature-branch -f
125
+ ```
126
+
127
+ ### Check on Your Pull Request
130
128
 
131
129
  Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
132
130
 
133
- ## Be Patient
131
+ ### Be Patient
134
132
 
135
133
  It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
136
134
 
data/README.md CHANGED
@@ -52,6 +52,28 @@ See a fully working example in [examples/hi_web](examples/hi_web/hi.rb).
52
52
 
53
53
  Refer to the [Slack Web API Method Reference](https://api.slack.com/methods) for the list of all available functions.
54
54
 
55
+ You can configure the Web client either globally or via the initializer.
56
+
57
+ ```ruby
58
+ Slack::Web::Client.config do |config|
59
+ config.user_agent = 'Slack Ruby Client/1.0'
60
+ end
61
+ ```
62
+
63
+ ```ruby
64
+ client = Slack::Web::Client.new(user_agent: 'Slack Ruby Client/1.0')
65
+ ```
66
+
67
+ The following settings are supported.
68
+
69
+ setting | description
70
+ -------------|-------------------------------------------------------------------------------------------------
71
+ user_agent | User-agent, defaults to _Slack Ruby Client/version_.
72
+ proxy | Optional HTTP proxy.
73
+ ca_path | Optional SSL certificates path.
74
+ ca_file | Optional SSL certificates file.
75
+ endpoint | Slack endpoint, default is _https://slack.com/api_.
76
+
55
77
  ### RealTime Client
56
78
 
57
79
  The Real Time Messaging API is a WebSocket-based API that allows you to receive events from Slack in real time and send messages as user.
@@ -60,7 +82,7 @@ The Real Time Messaging API is a WebSocket-based API that allows you to receive
60
82
  client = Slack::RealTime::Client.new
61
83
 
62
84
  client.on :hello do
63
- puts 'Successfully connected.'
85
+ puts "Successfully connected, welcome '#{client.self['name']}' to the '#{client.team['name']}' team at https://#{client.team['domain']}.slack.com."
64
86
  end
65
87
 
66
88
  client.on :message do |data|
@@ -75,19 +97,59 @@ end
75
97
  client.start!
76
98
  ```
77
99
 
78
- You can also send typing indicators.
100
+ You can send typing indicators with `typing`.
79
101
 
80
102
  ```ruby
81
103
  client.typing channel: data['channel']
82
104
  ```
83
105
 
106
+ You can send a ping with `ping`.
107
+
108
+ ```ruby
109
+ client.ping
110
+ ```
111
+
112
+ The client exposes the properties of [rtm.start](https://api.slack.com/methods/rtm.start) upon a successful connection.
113
+
114
+ property | description
115
+ ---------|-------------------------------------------------------------------------------------------------
116
+ url | A WebSocket Message Server URL.
117
+ self | Details on the authenticated user.
118
+ team | Details on the authenticated user's team.
119
+ users | A list of user objects, one for every member of the team.
120
+ channels | A list of channel objects, one for every channel visible to the authenticated user.
121
+ groups | A list of group objects, one for every group the authenticated user is in.
122
+ ims | A list of IM objects, one for every direct message channel visible to the authenticated user.
123
+ bots | Details of the integrations set up on this team.
124
+
125
+ You can configure the RealTime client either globally or via the initializer.
126
+
127
+ ```ruby
128
+ Slack::RealTime::Client.config do |config|
129
+ config.websocket_ping = 42
130
+ end
131
+ ```
132
+
133
+ ```ruby
134
+ client = Slack::RealTime::Client.new(websocket_ping: 42)
135
+ ```
136
+
137
+ The following settings are supported.
138
+
139
+ setting | description
140
+ ----------------|-----------------------------------------------------------------------------------------------------
141
+ websocket_ping | The number of seconds that indicates how often the WebSocket should send ping frames, default is 30.
142
+ websocket_proxy | Connect via proxy, include `:origin` and `:headers`.
143
+
144
+ Note that the RealTime client uses a Web client to obtain the WebSocket URL via [rtm.start](https://api.slack.com/methods/rtm.start), configure Web client options via `Slack::Web::Client.configure` as described above.
145
+
84
146
  See a fullly working example in [examples/hi_real_time](examples/hi_real_time/hi.rb).
85
147
 
86
148
  ![](examples/hi_real_time/hi.gif)
87
149
 
88
150
  ### Combinging RealTime and Web Clients
89
151
 
90
- Since the web client is used to obtain the RealTime client's WebSocket URL, you can continue using the web client in combination with the RealTime client.
152
+ Since the Web client is used to obtain the RealTime client's WebSocket URL, you can continue using the Web client in combination with the RealTime client.
91
153
 
92
154
  ```ruby
93
155
  client = Slack::RealTime::Client.new
data/RELEASING.md CHANGED
@@ -1,6 +1,6 @@
1
- # Releasing SlackRubyClient
1
+ # Releasing Slack-Ruby-Client
2
2
 
3
- There're no particular rules about when to release slack-ruby-client. Release bug fixes frequenty, features not so frequently and breaking API changes rarely.
3
+ There're no hard rules about when to release slack-ruby-client. Release bug fixes frequenty, features not so frequently and breaking API changes rarely.
4
4
 
5
5
  ### Release
6
6
 
@@ -8,7 +8,7 @@ end
8
8
  client = Slack::RealTime::Client.new
9
9
 
10
10
  client.on :hello do
11
- puts 'Successfully connected.'
11
+ puts "Successfully connected, welcome '#{client.self['name']}' to the '#{client.team['name']}' team at https://#{client.team['domain']}.slack.com."
12
12
  end
13
13
 
14
14
  client.on :message do |data|
@@ -8,7 +8,7 @@ end
8
8
  client = Slack::RealTime::Client.new
9
9
 
10
10
  client.on :hello do
11
- puts 'Successfully connected.'
11
+ puts "Successfully connected, welcome '#{client.self['name']}' to the '#{client.team['name']}' team at https://#{client.team['domain']}.slack.com."
12
12
  end
13
13
 
14
14
  client.on :message do |data|
@@ -20,4 +20,5 @@ require 'slack/real_time/api/message_id'
20
20
  require 'slack/real_time/api/ping'
21
21
  require 'slack/real_time/api/message'
22
22
  require 'slack/real_time/api/typing'
23
+ require 'slack/real_time/config'
23
24
  require 'slack/real_time/client'
@@ -10,13 +10,17 @@ module Slack
10
10
  include Api::Typing
11
11
 
12
12
  attr_accessor :web_client
13
+ attr_accessor(*Config::ATTRIBUTES)
13
14
 
14
- def initialize
15
+ def initialize(options = {})
15
16
  @callbacks = {}
16
17
  @web_client = Slack::Web::Client.new
18
+ Slack::RealTime::Config::ATTRIBUTES.each do |key|
19
+ send("#{key}=", options[key] || Slack::RealTime.config.send(key))
20
+ end
17
21
  end
18
22
 
19
- [:url, :team, :self, :users, :channels, :ims, :bots].each do |attr|
23
+ [:url, :team, :self, :users, :channels, :groups, :ims, :bots].each do |attr|
20
24
  define_method attr do
21
25
  @options[attr.to_s] if @options
22
26
  end
@@ -32,7 +36,11 @@ module Slack
32
36
  fail ClientAlreadyStartedError if started?
33
37
  EM.run do
34
38
  @options = web_client.rtm_start
35
- @socket = Slack::RealTime::Socket.new(@options['url'])
39
+
40
+ socket_options = {}
41
+ socket_options[:ping] = websocket_ping if websocket_ping
42
+ socket_options[:proxy] = websocket_proxy if websocket_proxy
43
+ @socket = Slack::RealTime::Socket.new(@options['url'], socket_options)
36
44
 
37
45
  @socket.connect! do |ws|
38
46
  ws.on :open do |event|
@@ -59,6 +67,16 @@ module Slack
59
67
  @socket && @socket.connected?
60
68
  end
61
69
 
70
+ class << self
71
+ def configure
72
+ block_given? ? yield(Config) : Config
73
+ end
74
+
75
+ def config
76
+ Config
77
+ end
78
+ end
79
+
62
80
  protected
63
81
 
64
82
  def send_json(data)
@@ -0,0 +1,30 @@
1
+ module Slack
2
+ module RealTime
3
+ module Config
4
+ extend self
5
+
6
+ ATTRIBUTES = [
7
+ :websocket_ping,
8
+ :websocket_proxy
9
+ ]
10
+
11
+ attr_accessor(*Config::ATTRIBUTES)
12
+
13
+ def reset
14
+ self.websocket_ping = 30
15
+ end
16
+ end
17
+
18
+ class << self
19
+ def configure
20
+ block_given? ? yield(Config) : Config
21
+ end
22
+
23
+ def config
24
+ Config
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Slack::RealTime::Config.reset
@@ -2,9 +2,11 @@ module Slack
2
2
  module RealTime
3
3
  class Socket
4
4
  attr_accessor :url
5
+ attr_accessor :options
5
6
 
6
- def initialize(url)
7
+ def initialize(url, options = {})
7
8
  @url = url
9
+ @options = options
8
10
  end
9
11
 
10
12
  def send_data(data)
@@ -14,7 +16,7 @@ module Slack
14
16
  def connect!(&_block)
15
17
  return if connected?
16
18
 
17
- @ws = Faye::WebSocket::Client.new(url)
19
+ @ws = Faye::WebSocket::Client.new(url, nil, options)
18
20
 
19
21
  @ws.on :close do |event|
20
22
  close(event)
data/lib/slack/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Slack
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -12,6 +12,16 @@ module Slack
12
12
  send("#{key}=", options[key] || Slack::Web.config.send(key))
13
13
  end
14
14
  end
15
+
16
+ class << self
17
+ def configure
18
+ block_given? ? yield(Config) : Config
19
+ end
20
+
21
+ def config
22
+ Config
23
+ end
24
+ end
15
25
  end
16
26
  end
17
27
  end
@@ -15,7 +15,7 @@ module Slack
15
15
 
16
16
  def reset
17
17
  self.endpoint = 'https://slack.com/api/'
18
- self.user_agent = "Slack Ruby Client #{Slack::VERSION}"
18
+ self.user_agent = "Slack Ruby Client/#{Slack::VERSION}"
19
19
  self.ca_path = `openssl version -a | grep OPENSSLDIR | awk '{print $2}'|sed -e 's/\"//g'`
20
20
  self.ca_file = "#{ca_path}/ca-certificates.crt"
21
21
  end
@@ -6,11 +6,14 @@ module Slack
6
6
 
7
7
  def connection
8
8
  options = {
9
- headers: { 'Accept' => 'application/json; charset=utf-8', 'User-Agent' => user_agent },
10
- url: endpoint
9
+ headers: { 'Accept' => 'application/json; charset=utf-8' }
11
10
  }
12
11
 
13
- ::Faraday::Connection.new(options) do |connection|
12
+ options[:headers]['User-Agent'] = user_agent if user_agent
13
+ options[:proxy] = proxy if proxy
14
+ options[:ssl] = { ca_path: ca_path, ca_file: ca_file }
15
+
16
+ ::Faraday::Connection.new(endpoint, options) do |connection|
14
17
  connection.use ::Faraday::Request::Multipart
15
18
  connection.use ::Faraday::Request::UrlEncoded
16
19
  connection.use ::Faraday::Response::RaiseError
@@ -2,20 +2,48 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Slack::RealTime::Client, vcr: { cassette_name: 'web/rtm_start' } do
4
4
  let(:client) { Slack::RealTime::Client.new }
5
- let(:ws) { double(Faye::WebSocket::Client) }
5
+ let(:ws) { double(Faye::WebSocket::Client, on: true) }
6
6
  let(:url) { 'wss://ms173.slack-msgs.com/websocket/lqcUiAvrKTP-uuid=' }
7
- let(:socket) { double(Slack::RealTime::Socket, connected?: true) }
7
+ before do
8
+ allow(EM).to receive(:run).and_yield
9
+ end
8
10
  context 'started' do
9
11
  describe '#start!' do
12
+ let(:socket) { double(Slack::RealTime::Socket, connected?: true) }
10
13
  before do
11
- allow(EM).to receive(:run).and_yield
12
- allow(Slack::RealTime::Socket).to receive(:new).with(url).and_return(socket)
14
+ allow(Slack::RealTime::Socket).to receive(:new).with(url, ping: 30).and_return(socket)
13
15
  allow(socket).to receive(:connect!).and_yield(ws)
14
- allow(ws).to receive(:on)
15
16
  client.start!
16
17
  end
17
- it 'sets url' do
18
- expect(client.url).to eq url
18
+ context 'properties provided upon connection' do
19
+ it 'sets url' do
20
+ expect(client.url).to eq url
21
+ end
22
+ it 'sets team' do
23
+ expect(client.team['domain']).to eq 'dblockdotorg'
24
+ end
25
+ it 'sets self' do
26
+ expect(client.self['id']).to eq 'U07518DTL'
27
+ end
28
+ it 'sets users' do
29
+ expect(client.users.count).to eq 7
30
+ expect(client.users.first['id']).to eq 'U07KECJ77'
31
+ end
32
+ it 'sets channels' do
33
+ expect(client.channels.count).to eq 8
34
+ expect(client.channels.first['name']).to eq 'demo'
35
+ end
36
+ it 'sets ims' do
37
+ expect(client.ims.count).to eq 2
38
+ expect(client.ims.first['user']).to eq 'USLACKBOT'
39
+ end
40
+ it 'sets bots' do
41
+ expect(client.bots.count).to eq 5
42
+ expect(client.bots.first['name']).to eq 'bot'
43
+ end
44
+ it 'sets groups' do
45
+ expect(client.groups.count).to eq 0
46
+ end
19
47
  end
20
48
  it 'uses web client to fetch url' do
21
49
  expect(client.web_client).to be_a Slack::Web::Client
@@ -48,4 +76,83 @@ RSpec.describe Slack::RealTime::Client, vcr: { cassette_name: 'web/rtm_start' }
48
76
  end
49
77
  end
50
78
  end
79
+ context 'with defaults' do
80
+ describe '#initialize' do
81
+ it 'sets ping' do
82
+ expect(client.websocket_ping).to eq 30
83
+ end
84
+ it "doesn't set proxy" do
85
+ expect(client.websocket_proxy).to be nil
86
+ end
87
+ Slack::RealTime::Config::ATTRIBUTES.each do |key|
88
+ it "sets #{key}" do
89
+ expect(client.send(key)).to eq Slack::RealTime::Config.send(key)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ context 'with custom settings' do
95
+ describe '#initialize' do
96
+ Slack::RealTime::Config::ATTRIBUTES.each do |key|
97
+ context key do
98
+ let(:client) { Slack::RealTime::Client.new(key => 'custom') }
99
+ it "sets #{key}" do
100
+ expect(client.send(key)).to_not eq Slack::RealTime::Config.send(key)
101
+ expect(client.send(key)).to eq 'custom'
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ context 'global config' do
108
+ after do
109
+ Slack::RealTime::Client.config.reset
110
+ end
111
+ let(:client) { Slack::RealTime::Client.new }
112
+ context 'ping' do
113
+ before do
114
+ Slack::RealTime::Client.configure do |config|
115
+ config.websocket_ping = 15
116
+ end
117
+ end
118
+ describe '#initialize' do
119
+ it 'sets ping' do
120
+ expect(client.websocket_ping).to eq 15
121
+ end
122
+ it 'creates a connection with custom ping' do
123
+ expect(Faye::WebSocket::Client).to receive(:new).with(url, nil, ping: 15).and_return(ws)
124
+ client.start!
125
+ end
126
+ end
127
+ end
128
+ context 'proxy' do
129
+ before do
130
+ Slack::RealTime::Client.configure do |config|
131
+ config.websocket_proxy = {
132
+ origin: 'http://username:password@proxy.example.com',
133
+ headers: { 'User-Agent' => 'ruby' }
134
+ }
135
+ end
136
+ end
137
+ describe '#initialize' do
138
+ it 'sets proxy' do
139
+ expect(client.websocket_proxy).to eq(
140
+ origin: 'http://username:password@proxy.example.com',
141
+ headers: { 'User-Agent' => 'ruby' }
142
+ )
143
+ end
144
+ it 'creates a connection with custom proxy' do
145
+ expect(Faye::WebSocket::Client).to receive(:new).with(
146
+ url,
147
+ nil,
148
+ ping: 30,
149
+ proxy: {
150
+ origin: 'http://username:password@proxy.example.com',
151
+ headers: { 'User-Agent' => 'ruby' }
152
+ }).and_return(ws)
153
+ client.start!
154
+ end
155
+ end
156
+ end
157
+ end
51
158
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  RSpec.describe Slack::RealTime::Socket do
4
4
  context 'with url' do
5
5
  let(:url) { 'wss://ms174.slack-msgs.com/websocket/xyz' }
6
- let(:socket) { Slack::RealTime::Socket.new(url) }
6
+ let(:socket) { Slack::RealTime::Socket.new(url, ping: 42) }
7
7
  let(:ws) { double(Faye::WebSocket::Client) }
8
8
  describe '#initialize' do
9
9
  it 'sets url' do
@@ -12,13 +12,17 @@ RSpec.describe Slack::RealTime::Socket do
12
12
  end
13
13
  describe '#connect!' do
14
14
  before do
15
- allow(Faye::WebSocket::Client).to receive(:new).and_return(ws)
15
+ allow(ws).to receive(:on).with(:close)
16
16
  end
17
17
  it 'connects' do
18
- expect(ws).to receive(:on).with(:close)
18
+ allow(Faye::WebSocket::Client).to receive(:new).and_return(ws)
19
19
  socket.connect!
20
20
  expect(socket.instance_variable_get('@ws')).to eq ws
21
21
  end
22
+ it 'pings every 30s' do
23
+ expect(Faye::WebSocket::Client).to receive(:new).with(url, nil, ping: 42).and_return(ws)
24
+ socket.connect!
25
+ end
22
26
  end
23
27
  describe '#disconnect!' do
24
28
  it 'closes and nils the websocket' do
@@ -28,4 +28,62 @@ RSpec.describe Slack::Web::Client do
28
28
  end
29
29
  end
30
30
  end
31
+ context 'global config' do
32
+ after do
33
+ Slack::Web::Client.config.reset
34
+ end
35
+ let(:client) { Slack::Web::Client.new }
36
+ context 'user-agent' do
37
+ before do
38
+ Slack::Web::Client.configure do |config|
39
+ config.user_agent = 'custom/user-agent'
40
+ end
41
+ end
42
+ describe '#initialize' do
43
+ it 'sets user-agent' do
44
+ expect(client.user_agent).to eq 'custom/user-agent'
45
+ end
46
+ it 'creates a connection with the custom user-agent' do
47
+ expect(client.send(:connection).headers).to eq(
48
+ 'Accept' => 'application/json; charset=utf-8',
49
+ 'User-Agent' => 'custom/user-agent'
50
+ )
51
+ end
52
+ end
53
+ end
54
+ context 'proxy' do
55
+ before do
56
+ Slack::Web::Client.configure do |config|
57
+ config.proxy = 'http://localhost:8080'
58
+ end
59
+ end
60
+ describe '#initialize' do
61
+ it 'sets proxy' do
62
+ expect(client.proxy).to eq 'http://localhost:8080'
63
+ end
64
+ it 'creates a connection with the proxy' do
65
+ expect(client.send(:connection).proxy.uri.to_s).to eq 'http://localhost:8080'
66
+ end
67
+ end
68
+ end
69
+ context 'SSL options' do
70
+ before do
71
+ Slack::Web::Client.configure do |config|
72
+ config.ca_path = '/ca_path'
73
+ config.ca_file = '/ca_file'
74
+ end
75
+ end
76
+ describe '#initialize' do
77
+ it 'sets ca_path and ca_file' do
78
+ expect(client.ca_path).to eq '/ca_path'
79
+ expect(client.ca_file).to eq '/ca_file'
80
+ end
81
+ it 'creates a connection with ssl options' do
82
+ ssl = client.send(:connection).ssl
83
+ expect(ssl.ca_path).to eq '/ca_path'
84
+ expect(ssl.ca_file).to eq '/ca_file'
85
+ end
86
+ end
87
+ end
88
+ end
31
89
  end
@@ -5,7 +5,7 @@ RSpec.shared_context 'connected client' do
5
5
  let(:socket) { double(Slack::RealTime::Socket, connected?: true) }
6
6
  before do
7
7
  allow(EM).to receive(:run).and_yield
8
- allow(Slack::RealTime::Socket).to receive(:new).with(url).and_return(socket)
8
+ allow(Slack::RealTime::Socket).to receive(:new).with(url, ping: 30).and_return(socket)
9
9
  allow(socket).to receive(:connect!).and_yield(ws)
10
10
  allow(ws).to receive(:on)
11
11
  client.start!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Doubrovkine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-25 00:00:00.000000000 Z
11
+ date: 2015-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -200,6 +200,7 @@ files:
200
200
  - lib/slack/real_time/api/ping.rb
201
201
  - lib/slack/real_time/api/typing.rb
202
202
  - lib/slack/real_time/client.rb
203
+ - lib/slack/real_time/config.rb
203
204
  - lib/slack/real_time/socket.rb
204
205
  - lib/slack/version.rb
205
206
  - lib/slack/web/api/endpoints.rb