Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: slack-ruby/slack-ruby-client
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.8.1
Choose a base ref
...
head repository: slack-ruby/slack-ruby-client
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.9.0
Choose a head ref
  • 11 commits
  • 30 files changed
  • 3 contributors

Commits on Apr 28, 2017

  1. Copy the full SHA
    81066f4 View commit details
  2. Copy the full SHA
    8ef6bbd View commit details
  3. Merge pull request #145 from dblock/rtm-connect

    Call rtm_connect instead of rtm_start by default.
    dblock authored Apr 28, 2017
    Copy the full SHA
    e6438f6 View commit details

Commits on Apr 30, 2017

  1. Fix #146: ThreadError: Target thread must not be current thread and u…

    …ndefined method running?.
    dblock committed Apr 30, 2017
    Copy the full SHA
    c0aa49b View commit details
  2. Copy the full SHA
    9ed4d21 View commit details

Commits on May 22, 2017

  1. Add Ruby 2.4.1

    Izumiya Keisuke committed May 22, 2017
    Copy the full SHA
    2c17221 View commit details
  2. Merge pull request #151 from syguer/add_2.4

    CI against Ruby 2.4.1
    dblock authored May 22, 2017
    Copy the full SHA
    27fe39f View commit details

Commits on Jul 25, 2017

  1. Raise a dedicated error class with 429 responses

    Ensure the error message includes the value of the "Retry-After" header
    so the client knows when it's safe to retry requests.
    greggroth committed Jul 25, 2017
    Copy the full SHA
    dd0e69e View commit details

Commits on Jul 27, 2017

  1. Namespace error classes in Slack::Web::Api::Errors

    I moved `Slack::Web::Api::Error` to
    `Slack::Web::Api::Errors::SlackError` but left the orginal constant
    aliased to the new one.
    greggroth committed Jul 27, 2017
    Copy the full SHA
    40f9a0b View commit details
  2. Merge pull request #154 from greggroth/429-errors

    Raise a dedicated error class with 429 responses
    dblock authored Jul 27, 2017
    Copy the full SHA
    727b49e View commit details

Commits on Aug 6, 2017

  1. Preparing for release, 0.9.0.

    dblock committed Aug 6, 2017
    Copy the full SHA
    418e1ac View commit details
30 changes: 20 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-05-03 16:35:06 -0400 using RuboCop version 0.35.0.
# on 2017-04-30 12:02:22 -0400 using RuboCop version 0.35.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 4
# Offense count: 3
Lint/HandleExceptions:
Exclude:
- 'lib/slack-ruby-client.rb'
- 'lib/slack/real_time/concurrency/celluloid.rb'
- 'spec/slack/real_time/concurrency/celluloid_spec.rb'
- 'spec/slack/real_time/concurrency/eventmachine_spec.rb'

@@ -21,20 +20,20 @@ Lint/UnusedBlockArgument:
Exclude:
- 'lib/slack/messages/formatting.rb'

# Offense count: 15
# Offense count: 14
Metrics/AbcSize:
Max: 44

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 156
Max: 165

# Offense count: 2
# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 9

# Offense count: 595
# Offense count: 720
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 288
@@ -48,7 +47,7 @@ Metrics/MethodLength:
Metrics/PerceivedComplexity:
Max: 11

# Offense count: 55
# Offense count: 59
# Configuration parameters: Exclude.
Style/Documentation:
Enabled: false
@@ -59,12 +58,12 @@ Style/FileName:
Exclude:
- 'lib/slack-ruby-client.rb'

# Offense count: 100
# Offense count: 116
# Configuration parameters: AllowedVariables.
Style/GlobalVars:
Enabled: false

# Offense count: 17
# Offense count: 21
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/MethodName:
Enabled: false
@@ -76,6 +75,17 @@ Style/ModuleFunction:
- 'lib/slack/real_time/config.rb'
- 'lib/slack/web/config.rb'

# Offense count: 1
Style/MultilineTernaryOperator:
Exclude:
- 'spec/support/real_time/connected_client.rb'

# Offense count: 1
# Cop supports --auto-correct.
Style/RescueModifier:
Exclude:
- 'lib/slack/real_time/concurrency/celluloid.rb'

# Offense count: 2
# Cop supports --auto-correct.
Style/SpecialGlobalVars:
26 changes: 16 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -2,22 +2,28 @@ language: ruby

cache: bundler

rvm:
- 2.2
- 2.1
- 2.0
- rbx-2
- jruby-19mode
- ruby-head
- jruby-head

matrix:
include:
- rvm: 2.3.1
- rvm: 2.4.1
script:
- bundle exec danger
- rvm: 2.3.1
- rvm: 2.4.1
env: CONCURRENCY=celluloid-io
- rvm: 2.3.1
- rvm: 2.4.1
env: CONCURRENCY=faye-websocket
- rvm: 2.3.4
env: CONCURRENCY=celluloid-io
- rvm: 2.3.4
env: CONCURRENCY=faye-websocket
- rvm: 2.2
- rvm: 2.1
- rvm: 2.0
- rvm: rbx-2
- rvm: jruby-19mode
- rvm: ruby-head
- rvm: jruby-head
allow_failures:
- rvm: ruby-head
- rvm: jruby-head
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### 0.9.0 (8/6/2017)

* [#146](https://github.com/slack-ruby/slack-ruby-client/issues/146): Fix: `undefined method running?` and `ThreadError: Target thread must not be current thread` with `Celluloid::IO` - [@dblock](https://github.com/dblock).
* [#145](https://github.com/slack-ruby/slack-ruby-client/pull/145): Automatically select `rtm_connect` vs. `rtm_start` - [@dblock](https://github.com/dblock).
* [#154](https://github.com/slack-ruby/slack-ruby-client/pull/154): Raise a dedicated error class with 429 responses - [@greggroth](https://github.com/greggroth).
* [#154](https://github.com/slack-ruby/slack-ruby-client/pull/154): Namespace error classes under `Slack::Web::Api::Errors` module - [@greggroth](https://github.com/greggroth).

### 0.8.1 (4/28/2017)

* Added `exclude_members` option to `channels_list` Web API - [@dblock](https://github.com/dblock).
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ A Ruby client for the Slack [Web](https://api.slack.com/web) and [RealTime Messa

## Stable Release

You're reading the documentation for the **stable** release of slack-ruby-client, 0.8.1. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
You're reading the documentation for the **stable** release of slack-ruby-client, 0.9.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.

## Installation

@@ -244,11 +244,11 @@ Tracking with a local store can be disabled with `Slack::RealTime::Client.new(st

#### Slack::RealTime::Stores::Store

The default store that tracks all changes.
The default store that tracks all changes. By default the client will be connected using `rtm_start`.

#### Slack::RealTime::Stores::Starter

A smaller store that only stores and tracks information about the bot user, but not channels, users, groups, ims or bots.
A smaller store that only stores and tracks information about the bot user, but not channels, users, groups, ims or bots. By default the client will be connected using `rtm_connect`.

### Configuring Slack::RealTime::Client

@@ -272,15 +272,26 @@ token | Slack API token.
websocket_ping | The number of seconds that indicates how often the WebSocket should send ping frames, default is 30.
websocket_proxy | Connect via proxy, include `:origin` and `:headers`.
store_class | Local store class name, default is an in-memory `Slack::RealTime::Stores::Store`.
start_options | Options to pass into `rtm.start`, default is `{ request: { timeout: 180 } }`.
start_method | Optional start method, either `:rtm_start` or `:rtm_connect`.
start_options | Options to pass into `rtm.start` or `rtm.connect`, default is `{ request: { timeout: 180 } }`.
logger | Optional `Logger` instance that logs RealTime requests and socket data.

Note that the RealTime client uses a Web client to obtain the WebSocket URL via [rtm.start](https://api.slack.com/methods/rtm.start). While `token` and `logger` options are passed down from the RealTime client, you may also configure Web client options via `Slack::Web::Client.configure` as described above.
Note that the RealTime client uses a Web client to obtain the WebSocket URL via [rtm.start](https://api.slack.com/methods/rtm.start) or [rtm.connect](https://api.slack.com/methods/rtm.connect). While `token` and `logger` options are passed down from the RealTime client, you may also configure Web client options via `Slack::Web::Client.configure` as described above.

See a fully working example in [examples/hi_real_time](examples/hi_real_time/hi.rb).

![](examples/hi_real_time/hi.gif)

### Connection Methods

The RealTime client uses either [rtm.start](https://api.slack.com/methods/rtm.start) or [rtm.connect](https://api.slack.com/methods/rtm.connect) to open a connection. The former retrieves a lot of team information while the latter only serves connection purposes and is preferred. You should let the library choose the right method for you based on the `store_class` used and override this behavior with `start_method` when necessary.

```ruby
Slack::RealTime::Client.config do |config|
config.start_method = :rtm_start
end
```

### Combining RealTime and Web Clients

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.
16 changes: 16 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
Upgrading Slack-Ruby-Client
===========================

### Upgrading to >= 0.9.0

#### Changes in How the RTM Client Connects

The RealTime client now automatically chooses either [rtm.start](https://api.slack.com/methods/rtm.start) or [rtm.connect](https://api.slack.com/methods/rtm.connect) to open a connection. The former retrieves a lot of team information while the latter only serves connection purposes and is newer and preferred, while the latter is required to use the full `Slack::RealTime::Stores::Store` storage class.

Prior versions always used `rtm.start`, to restore this behavior, configure `start_method`.

```ruby
Slack::RealTime::Client.config do |config|
config.start_method = :rtm_start
end
```

See [#145](https://github.com/slack-ruby/slack-ruby-client/pull/145) for more information.

### Upgrading to >= 0.8.0

The default timeout for `rtm.start` has been increased from 60 to 180 seconds via `Slack::RealTime::Client.config.start_options[:request][:timeout]`. If you're explicitly setting `start_options` in your application, preserve the value by merging settings instead of replacing the entire `start_options` value.
2 changes: 2 additions & 0 deletions lib/slack-ruby-client.rb
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@
# ignore, only used in users_search
end
require_relative 'slack/web/config'
require_relative 'slack/web/api/errors/slack_error'
require_relative 'slack/web/api/errors/too_many_requests_error'
require_relative 'slack/web/api/error'
require_relative 'slack/web/faraday/response/raise_error'
require_relative 'slack/web/faraday/connection'
12 changes: 11 additions & 1 deletion lib/slack/real_time/client.rb
Original file line number Diff line number Diff line change
@@ -107,13 +107,23 @@ def run_loop
# @return [Slack::RealTime::Socket]
def build_socket
fail ClientAlreadyStartedError if started?
start = web_client.rtm_start(start_options)
start = web_client.send(rtm_start_method, start_options)
data = Slack::Messages::Message.new(start)
@url = data.url
@store = @store_class.new(data) if @store_class
socket_class.new(@url, socket_options)
end

def rtm_start_method
if start_method
start_method
elsif @store_class == Slack::RealTime::Stores::Store
:rtm_start
else
:rtm_connect
end
end

def socket_options
socket_options = {}
socket_options[:ping] = websocket_ping if websocket_ping
6 changes: 3 additions & 3 deletions lib/slack/real_time/concurrency/celluloid.rb
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ def run_loop
logger.debug("#{self.class}##{__method__}") { e }
driver.emit(:close, WebSocket::Driver::CloseEvent.new(1001, 'server closed connection')) unless @closing
ensure
current_actor.terminate if current_actor.alive? && current_actor.running?
current_actor.terminate if current_actor.alive? rescue nil
end

def close
@@ -90,8 +90,8 @@ def join
end

def build_socket
socket = TCPSocket.new(addr, port)
socket = SSLSocket.new(socket, build_ssl_context) if secure?
socket = ::Celluloid::IO::TCPSocket.new(addr, port)
socket = ::Celluloid::IO::SSLSocket.new(socket, build_ssl_context) if secure?
socket
end

2 changes: 2 additions & 0 deletions lib/slack/real_time/config.rb
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ class NoConcurrencyError < StandardError; end
:websocket_ping,
:websocket_proxy,
:concurrency,
:start_method,
:start_options,
:store_class,
:logger
@@ -22,6 +23,7 @@ def reset
self.websocket_proxy = nil
self.token = nil
self.concurrency = method(:detect_concurrency)
self.start_method = nil
self.start_options = { request: { timeout: 180 } }
self.store_class = Slack::RealTime::Store
self.logger = nil
2 changes: 1 addition & 1 deletion lib/slack/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Slack
VERSION = '0.8.1'.freeze
VERSION = '0.9.0'.freeze
end
9 changes: 1 addition & 8 deletions lib/slack/web/api/error.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
module Slack
module Web
module Api
class Error < ::Faraday::Error
attr_reader :response

def initialize(message, response = nil)
@response = response
super message
end
end
Error = Errors::SlackError
end
end
end
16 changes: 16 additions & 0 deletions lib/slack/web/api/errors/slack_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Slack
module Web
module Api
module Errors
class SlackError < ::Faraday::Error
attr_reader :response

def initialize(message, response = nil)
@response = response
super message
end
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/slack/web/api/errors/too_many_requests_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Slack
module Web
module Api
module Errors
class TooManyRequestsError < ::Faraday::Error
attr_reader :response

def initialize(response)
@response = response
end

def message
"Retry after #{retry_after} seconds"
end

def retry_after
response.headers['retry-after'].to_i
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/slack/web/api/mixins/ids.id.rb
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ def id_for(key, name, prefix, list_method, not_found_error)
end
end

fail Slack::Web::Api::Error, not_found_error
fail Slack::Web::Api::Errors::SlackError, not_found_error
end
end
end
2 changes: 1 addition & 1 deletion lib/slack/web/api/slack-api-ref
Submodule slack-api-ref updated 179 files
2 changes: 1 addition & 1 deletion lib/slack/web/faraday/connection.rb
Original file line number Diff line number Diff line change
@@ -21,10 +21,10 @@ def connection
::Faraday::Connection.new(endpoint, options) do |connection|
connection.use ::Faraday::Request::Multipart
connection.use ::Faraday::Request::UrlEncoded
connection.use ::Faraday::Response::RaiseError
connection.use ::Slack::Web::Faraday::Response::RaiseError
connection.use ::FaradayMiddleware::Mashify, mash_class: Slack::Messages::Message
connection.use ::FaradayMiddleware::ParseJson
connection.use ::Faraday::Response::RaiseError
connection.response :logger, logger if logger
connection.adapter ::Faraday.default_adapter
end
10 changes: 7 additions & 3 deletions lib/slack/web/faraday/response/raise_error.rb
Original file line number Diff line number Diff line change
@@ -4,9 +4,13 @@ module Faraday
module Response
class RaiseError < ::Faraday::Response::Middleware
def on_complete(env)
body = env.body
return if body['ok']
fail Slack::Web::Api::Error.new(body['error'], env.response)
if env.status == 429
fail Slack::Web::Api::Errors::TooManyRequestsError, env.response
elsif (body = env.body) && body['ok']
return
else
fail Slack::Web::Api::Errors::SlackError.new(body['error'], env.response)
end
end
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/slack/web/429_error.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading