laziness 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +44 -0
- data/LICENSE +20 -0
- data/README.md +93 -0
- data/Rakefile +6 -0
- data/laziness.gemspec +25 -0
- data/lib/laziness/api/base.rb +43 -0
- data/lib/laziness/api/channels.rb +30 -0
- data/lib/laziness/api/users.rb +20 -0
- data/lib/laziness/api.rb +5 -0
- data/lib/laziness/base.rb +19 -0
- data/lib/laziness/channel.rb +4 -0
- data/lib/laziness/client.rb +17 -0
- data/lib/laziness/errors.rb +97 -0
- data/lib/laziness/user.rb +4 -0
- data/lib/laziness/version.rb +3 -0
- data/lib/laziness.rb +15 -0
- data/spec/laziness/api/channels_spec.rb +52 -0
- data/spec/laziness/api/users_spec.rb +34 -0
- data/spec/laziness/client_spec.rb +9 -0
- data/spec/laziness/errors_spec.rb +166 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/fixtures/channels_info.json +28 -0
- data/spec/support/fixtures/channels_list.json +30 -0
- data/spec/support/fixtures/successful_response.json +1 -0
- data/spec/support/fixtures/users_info.json +10 -0
- data/spec/support/fixtures/users_list.json +19 -0
- data/spec/support/slack_stub_factory.rb +10 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0d1f36acf80cdc40517d0ac06da0ac25d22b7d3a
|
4
|
+
data.tar.gz: c785e7e7379b5b859d9e9dfae0a8fe5e1edf953e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e91075ad319f1ef40640283609e33b873495a147692b97b95434edd954556f514e462c8e1ac845e00de41c55542d09fbe30433403f6377e5a4527d96580cf4a
|
7
|
+
data.tar.gz: 5331629668c4ee42cafb4f70d087d7c3cafd25ccc6a81e2b798cd02a920fc33df0135c9fdcd15ef9a3a2e13aef99417a0232569f396d4e2ac68d9f4a836d38cb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.4
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
laziness (0.1.0)
|
5
|
+
hashie
|
6
|
+
httparty
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
addressable (2.3.6)
|
12
|
+
crack (0.4.2)
|
13
|
+
safe_yaml (~> 1.0.0)
|
14
|
+
diff-lcs (1.2.5)
|
15
|
+
hashie (3.3.1)
|
16
|
+
httparty (0.13.3)
|
17
|
+
json (~> 1.8)
|
18
|
+
multi_xml (>= 0.5.2)
|
19
|
+
json (1.8.1)
|
20
|
+
multi_xml (0.5.5)
|
21
|
+
rspec (3.1.0)
|
22
|
+
rspec-core (~> 3.1.0)
|
23
|
+
rspec-expectations (~> 3.1.0)
|
24
|
+
rspec-mocks (~> 3.1.0)
|
25
|
+
rspec-core (3.1.7)
|
26
|
+
rspec-support (~> 3.1.0)
|
27
|
+
rspec-expectations (3.1.2)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.1.0)
|
30
|
+
rspec-mocks (3.1.3)
|
31
|
+
rspec-support (~> 3.1.0)
|
32
|
+
rspec-support (3.1.2)
|
33
|
+
safe_yaml (1.0.3)
|
34
|
+
webmock (1.18.0)
|
35
|
+
addressable (>= 2.3.6)
|
36
|
+
crack (>= 0.3.2)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
laziness!
|
43
|
+
rspec
|
44
|
+
webmock
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Jamie Wright
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
laziness
|
2
|
+
========
|
3
|
+
|
4
|
+
## DESCRIPTION
|
5
|
+
|
6
|
+
A [Slack](http://slack.com) [API](http://api.slack.com) wrapper written in Ruby.
|
7
|
+
|
8
|
+
## INSTALLATION
|
9
|
+
|
10
|
+
Add this line to your Gemfile:
|
11
|
+
|
12
|
+
```
|
13
|
+
gem 'laziness'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
```
|
19
|
+
bundle install
|
20
|
+
```
|
21
|
+
|
22
|
+
Or install it yourself:
|
23
|
+
|
24
|
+
```
|
25
|
+
gem install laziness
|
26
|
+
```
|
27
|
+
|
28
|
+
## USAGE
|
29
|
+
|
30
|
+
```
|
31
|
+
client = Slack.client(access_token: "your-access-token")
|
32
|
+
```
|
33
|
+
|
34
|
+
### Channels
|
35
|
+
|
36
|
+
```
|
37
|
+
client.channels.all # lists out all the channels
|
38
|
+
client.channels.find(channel_id) # get info about a specific channel
|
39
|
+
client.channels.archive!(channel_id) # archives the specific channel
|
40
|
+
|
41
|
+
channel = Slack::Channel.new(name: "testing")
|
42
|
+
client.channels.create(channel) # creates the new channel
|
43
|
+
|
44
|
+
client.channels.history(channel_id, latest: DateTime.now, oldest: 2.weeks.ago, count: 1000) # lists out the messages for the specific channels
|
45
|
+
client.channels.invite(channel_id, user_id) # invites the specific user to the specific channel
|
46
|
+
```
|
47
|
+
|
48
|
+
#### TODO:
|
49
|
+
|
50
|
+
- [ ] [channels.history](https://api.slack.com/methods/channels.history)
|
51
|
+
- [ ] [channels.invite](https://api.slack.com/methods/channels.invite)
|
52
|
+
- [ ] [channels.join](https://api.slack.com/methods/channels.join)
|
53
|
+
- [ ] [channels.kick](https://api.slack.com/methods/channels.kick)
|
54
|
+
- [ ] [channels.leave](https://api.slack.com/methods/channels.leave)
|
55
|
+
- [ ] [channels.mark](https://api.slack.com/methods/channels.mark)
|
56
|
+
- [ ] [channels.rename](https://api.slack.com/methods/channels.rename)
|
57
|
+
- [ ] [channels.setPurpose](https://api.slack.com/methods/channels.setPurpose)
|
58
|
+
- [ ] [channels.setTopic](https://api.slack.com/methods/channels.setTopic)
|
59
|
+
|
60
|
+
### Chat
|
61
|
+
|
62
|
+
### Emoji
|
63
|
+
|
64
|
+
### Files
|
65
|
+
|
66
|
+
### Groups
|
67
|
+
|
68
|
+
### IM
|
69
|
+
|
70
|
+
### OAuth
|
71
|
+
|
72
|
+
### Presence
|
73
|
+
|
74
|
+
### Search
|
75
|
+
|
76
|
+
### Stars
|
77
|
+
|
78
|
+
### Users
|
79
|
+
|
80
|
+
```
|
81
|
+
client.users.all # lists out all the users
|
82
|
+
client.users.find(user_id) # get info about a specific user
|
83
|
+
client.users.set_active # sets the current user (defined by the access_token) as active
|
84
|
+
```
|
85
|
+
|
86
|
+
## CONTRIBUTING
|
87
|
+
|
88
|
+
1. Clone the repository `git clone https://github.com/brilliantfantastic/laziness`
|
89
|
+
1. Create a feature branch `git checkout -b my-awesome-feature`
|
90
|
+
1. Codez!
|
91
|
+
1. Commit your changes (small commits please)
|
92
|
+
1. Push your new branch `git push origin my-awesome-feature`
|
93
|
+
1. Create a pull request `hub pull-request -b brilliantfantastic:master -h brilliantfantastic:my-awesome-feature`
|
data/Rakefile
ADDED
data/laziness.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'laziness/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "laziness"
|
8
|
+
spec.version = Slack::VERSION
|
9
|
+
spec.authors = ["Jamie Wright"]
|
10
|
+
spec.email = ["jamie@brilliantfantastic.com"]
|
11
|
+
spec.summary = "A Slack API wrapper written in Ruby."
|
12
|
+
spec.description = "Laziness wraps the Slack API in a Ruby gem so Ruby programs can easily communicate with the Slack API (http://api.slack.com)."
|
13
|
+
spec.homepage = "http://github.com/brilliantfantastic/laziness"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 2.0'
|
22
|
+
|
23
|
+
spec.add_runtime_dependency('httparty')
|
24
|
+
spec.add_runtime_dependency('hashie')
|
25
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Slack
|
2
|
+
module API
|
3
|
+
class Base
|
4
|
+
attr_reader :access_token
|
5
|
+
|
6
|
+
def initialize(access_token)
|
7
|
+
@access_token = access_token
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def base_path
|
13
|
+
"https://slack.com/api/"
|
14
|
+
end
|
15
|
+
|
16
|
+
def request(method, access_token, path, arguments={})
|
17
|
+
full_path = "#{base_path}#{path}?token=#{access_token}"
|
18
|
+
arguments.each_pair { |key, value| full_path = "#{full_path}&#{key}=#{value}" }
|
19
|
+
options = {
|
20
|
+
headers: {
|
21
|
+
"Accept" => "application/json",
|
22
|
+
"Content-Type" => "application/json; charset=utf-8"
|
23
|
+
}
|
24
|
+
}
|
25
|
+
response = HTTParty.send method, full_path, options
|
26
|
+
handle_exceptions response
|
27
|
+
response
|
28
|
+
end
|
29
|
+
|
30
|
+
def handle_exceptions(response)
|
31
|
+
parsed = JSON.parse(response)
|
32
|
+
if !parsed['ok']
|
33
|
+
klass = "#{parsed['error'].gsub(/(^|_)(.)/) { $2.upcase }}Error"
|
34
|
+
if Slack.const_defined? klass
|
35
|
+
raise Slack.const_get(klass)
|
36
|
+
else
|
37
|
+
raise Slack::APIError.new parsed['error']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Slack
|
2
|
+
module API
|
3
|
+
class Channels < Base
|
4
|
+
def all(exclude_archived=false)
|
5
|
+
response = request :get, access_token, 'channels.list', exclude_archived: exclude_archived ? 1 : 0
|
6
|
+
Slack::Channel.parse response, 'channels'
|
7
|
+
end
|
8
|
+
|
9
|
+
def find(id)
|
10
|
+
response = request :get, access_token, 'channels.info', channel: id
|
11
|
+
Slack::Channel.parse response, 'channel'
|
12
|
+
end
|
13
|
+
|
14
|
+
def create(name)
|
15
|
+
response = request :post, access_token, 'channels.create', name: name
|
16
|
+
Slack::Channel.parse response, 'channel'
|
17
|
+
end
|
18
|
+
|
19
|
+
def archive(id)
|
20
|
+
request :post, access_token, 'channels.archive', channel: id
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def unarchive(id)
|
25
|
+
request :post, access_token, 'channels.unarchive', channel: id
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Slack
|
2
|
+
module API
|
3
|
+
class Users < Base
|
4
|
+
def all
|
5
|
+
response = request :get, access_token, 'users.list'
|
6
|
+
Slack::User.parse response, 'members'
|
7
|
+
end
|
8
|
+
|
9
|
+
def find(id)
|
10
|
+
response = request :get, access_token, 'users.info', user: id
|
11
|
+
Slack::User.parse response, 'user'
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_active
|
15
|
+
request :post, access_token, 'users.setActive'
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/laziness/api.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
|
3
|
+
module Slack
|
4
|
+
class Base < Hashie::Mash
|
5
|
+
class << self
|
6
|
+
def parse(request, key=nil)
|
7
|
+
parsed = JSON.parse(request)
|
8
|
+
parsed = parsed[key] if key && parsed[key]
|
9
|
+
if parsed.is_a? Array
|
10
|
+
models = []
|
11
|
+
parsed.each { |attributes| models << new(attributes) }
|
12
|
+
models
|
13
|
+
else
|
14
|
+
new parsed
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Slack
|
2
|
+
class Client
|
3
|
+
attr_reader :access_token
|
4
|
+
|
5
|
+
def initialize(access_token)
|
6
|
+
@access_token = access_token
|
7
|
+
end
|
8
|
+
|
9
|
+
def users
|
10
|
+
@users ||= Slack::API::Users.new(access_token)
|
11
|
+
end
|
12
|
+
|
13
|
+
def channels
|
14
|
+
@channels ||= Slack::API::Channels.new(access_token)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module Slack
|
2
|
+
class APIError < StandardError
|
3
|
+
def initialize(message)
|
4
|
+
super message
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class NotAuthedError < APIError
|
9
|
+
def initialize
|
10
|
+
super "No authentication token provided."
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class InvalidAuthError < APIError
|
15
|
+
def initialize
|
16
|
+
super "Invalid authentication token."
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class AccountInactiveError < APIError
|
21
|
+
def initialize
|
22
|
+
super "Authentication token is for a deleted user or team."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class RestrictedActionError < APIError
|
27
|
+
def initialize
|
28
|
+
super "A team preference prevents authenticated user from archiving."
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class UserNotFoundError < APIError
|
33
|
+
def initialize
|
34
|
+
super "User value is invalid."
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class UserNotVisibleError < APIError
|
39
|
+
def initialize
|
40
|
+
super "Not authorized to view the requested user."
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class UserIsBotError < APIError
|
45
|
+
def initialize
|
46
|
+
super "This method cannot be called by a bot user."
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class UserIsRestrictedError < APIError
|
51
|
+
def initialize
|
52
|
+
super "This method cannot be called by a restricted user or single channel guest."
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class ChannelNotFoundError < APIError
|
57
|
+
def initialize
|
58
|
+
super "Channel value is invalid."
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class AlreadyArchivedError < APIError
|
63
|
+
def initialize
|
64
|
+
super "Channel has already been archived."
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class NotArchivedError < APIError
|
69
|
+
def initialize
|
70
|
+
super "Channel is not archived."
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class NameTakenError < APIError
|
75
|
+
def initialize
|
76
|
+
super "A channel cannot be created with the given name."
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class NoChannelError < APIError
|
81
|
+
def initialize
|
82
|
+
super "Value passed for name was empty."
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class CantArchiveGeneralError < APIError
|
87
|
+
def initialize
|
88
|
+
super "You cannot archive the general channel."
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class LastRaChannelError < APIError
|
93
|
+
def initialize
|
94
|
+
super "You cannot archive the last channel for a restricted account."
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/lib/laziness.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'laziness/version'
|
2
|
+
require 'laziness/api'
|
3
|
+
require 'laziness/base'
|
4
|
+
require 'laziness/channel'
|
5
|
+
require 'laziness/client'
|
6
|
+
require 'laziness/errors'
|
7
|
+
require 'laziness/user'
|
8
|
+
|
9
|
+
module Slack
|
10
|
+
class << self
|
11
|
+
def client(attributes={})
|
12
|
+
Slack::Client.new(attributes[:access_token])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
describe Slack::API:: Channels do
|
2
|
+
let(:access_token) { "12345" }
|
3
|
+
subject { Slack::API::Channels.new access_token }
|
4
|
+
|
5
|
+
describe '.all' do
|
6
|
+
it 'returns all the channels for an account' do
|
7
|
+
stub_slack_request :get, "channels.list?exclude_archived=0&token=#{access_token}", 'channels_list.json'
|
8
|
+
|
9
|
+
channels = subject.all
|
10
|
+
expect(channels.length).to eq 1
|
11
|
+
expect(channels[0].id).to eq "C02BLAH"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.find' do
|
16
|
+
it 'returns the specific channel with the specified id' do
|
17
|
+
stub_slack_request :get, "channels.info?channel=C02BLAH&token=#{access_token}", 'channels_info.json'
|
18
|
+
|
19
|
+
channel = subject.find("C02BLAH")
|
20
|
+
expect(channel.id).to eq "C02BLAH"
|
21
|
+
expect(channel.name).to eq "tour"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.create' do
|
26
|
+
it 'creates a new channel and returns the specified channel' do
|
27
|
+
stub_slack_request :post, "channels.create?name=tour&token=#{access_token}", 'channels_info.json'
|
28
|
+
|
29
|
+
channel = subject.create("tour")
|
30
|
+
expect(channel.id).to eq "C02BLAH"
|
31
|
+
expect(channel.name).to eq "tour"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.archive' do
|
36
|
+
it 'marks the channel as archived' do
|
37
|
+
stub = stub_slack_request :post, "channels.archive?channel=C02BLAH&token=#{access_token}", 'successful_response.json'
|
38
|
+
|
39
|
+
expect(subject.archive("C02BLAH")).to be_nil
|
40
|
+
expect(stub).to have_been_requested
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '.unarchive' do
|
45
|
+
it 'marks the channel as unarchived' do
|
46
|
+
stub = stub_slack_request :post, "channels.unarchive?channel=C02BLAH&token=#{access_token}", 'successful_response.json'
|
47
|
+
|
48
|
+
expect(subject.unarchive("C02BLAH")).to be_nil
|
49
|
+
expect(stub).to have_been_requested
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
describe Slack::API::Users do
|
2
|
+
let(:access_token) { "12345" }
|
3
|
+
subject { Slack::API::Users.new access_token }
|
4
|
+
|
5
|
+
describe '.all' do
|
6
|
+
it 'returns all the users for an account' do
|
7
|
+
stub_slack_request :get, "users.list?token=#{access_token}", 'users_list.json'
|
8
|
+
|
9
|
+
users = subject.all
|
10
|
+
expect(users.length).to eq 2
|
11
|
+
expect(users[0].id).to eq "U024BLAH"
|
12
|
+
expect(users[1].id).to eq "U024BLAH2"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '.find' do
|
17
|
+
it 'returns the specific user with the specified id' do
|
18
|
+
stub_slack_request :get, "users.info?user=U024BLAH&token=#{access_token}", 'users_info.json'
|
19
|
+
|
20
|
+
user = subject.find("U024BLAH")
|
21
|
+
expect(user.id).to eq "U024BLAH"
|
22
|
+
expect(user.name).to eq "jimmy"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '.set_active' do
|
27
|
+
it 'marks the user as active' do
|
28
|
+
stub = stub_slack_request :post, "users.setActive?token=#{access_token}", 'successful_response.json'
|
29
|
+
|
30
|
+
expect(subject.set_active).to be_nil
|
31
|
+
expect(stub).to have_been_requested
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,166 @@
|
|
1
|
+
describe 'API errors' do
|
2
|
+
let(:access_token) { "12345" }
|
3
|
+
subject { Slack::Client.new access_token }
|
4
|
+
|
5
|
+
# user_not_visible - The requested user is not visible to the calling user
|
6
|
+
|
7
|
+
it 'raises an not authed error if a no authentication token was provided' do
|
8
|
+
response = {
|
9
|
+
ok: false,
|
10
|
+
error: "not_authed"
|
11
|
+
}
|
12
|
+
stub_request(:get, "https://slack.com/api/users.list?token=#{access_token}").
|
13
|
+
to_return(status: 200, body: response.to_json)
|
14
|
+
expect { subject.users.all }.to raise_error Slack::NotAuthedError
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'raises an invalid auth error if an invalid authentication token was provided' do
|
18
|
+
response = {
|
19
|
+
ok: false,
|
20
|
+
error: "invalid_auth"
|
21
|
+
}
|
22
|
+
stub_request(:get, "https://slack.com/api/users.list?token=#{access_token}").
|
23
|
+
to_return(status: 200, body: response.to_json)
|
24
|
+
expect { subject.users.all }.to raise_error Slack::InvalidAuthError
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'raises an account inactive error if the authentication token is for a deleted user' do
|
28
|
+
response = {
|
29
|
+
ok: false,
|
30
|
+
error: "account_inactive"
|
31
|
+
}
|
32
|
+
stub_request(:get, "https://slack.com/api/users.list?token=#{access_token}").
|
33
|
+
to_return(status: 200, body: response.to_json)
|
34
|
+
expect { subject.users.all }.to raise_error Slack::AccountInactiveError
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'raises a user not found error if the user id was invalid' do
|
38
|
+
response = {
|
39
|
+
ok: false,
|
40
|
+
error: "user_not_found"
|
41
|
+
}
|
42
|
+
stub_request(:get, "https://slack.com/api/users.list?token=#{access_token}").
|
43
|
+
to_return(status: 200, body: response.to_json)
|
44
|
+
expect { subject.users.all }.to raise_error Slack::UserNotFoundError
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'raises a user not visible error if the user does not have permission to see the requested user' do
|
48
|
+
response = {
|
49
|
+
ok: false,
|
50
|
+
error: "user_not_visible"
|
51
|
+
}
|
52
|
+
stub_request(:get, "https://slack.com/api/users.list?token=#{access_token}").
|
53
|
+
to_return(status: 200, body: response.to_json)
|
54
|
+
expect { subject.users.all }.to raise_error Slack::UserNotVisibleError
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'raises a user is bot error if the user is a bot user' do
|
58
|
+
response = {
|
59
|
+
ok: false,
|
60
|
+
error: "user_is_bot"
|
61
|
+
}
|
62
|
+
stub_request(:get, "https://slack.com/api/users.list?token=#{access_token}").
|
63
|
+
to_return(status: 200, body: response.to_json)
|
64
|
+
expect { subject.users.all }.to raise_error Slack::UserIsBotError
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'raises a user is restricted error if the user is not allowed to perform the action' do
|
68
|
+
response = {
|
69
|
+
ok: false,
|
70
|
+
error: "user_is_restricted"
|
71
|
+
}
|
72
|
+
stub_request(:get, "https://slack.com/api/users.list?token=#{access_token}").
|
73
|
+
to_return(status: 200, body: response.to_json)
|
74
|
+
expect { subject.users.all }.to raise_error Slack::UserIsRestrictedError
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'raises a channel not found error if the channel id was invalid' do
|
78
|
+
response = {
|
79
|
+
ok: false,
|
80
|
+
error: "channel_not_found"
|
81
|
+
}
|
82
|
+
stub_request(:get, "https://slack.com/api/channels.list?exclude_archived=0&token=#{access_token}").
|
83
|
+
to_return(status: 200, body: response.to_json)
|
84
|
+
expect { subject.channels.all }.to raise_error Slack::ChannelNotFoundError
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'raises a channel already archived error if the channel id was already archived' do
|
88
|
+
response = {
|
89
|
+
ok: false,
|
90
|
+
error: "already_archived"
|
91
|
+
}
|
92
|
+
stub_request(:get, "https://slack.com/api/channels.list?exclude_archived=0&token=#{access_token}").
|
93
|
+
to_return(status: 200, body: response.to_json)
|
94
|
+
expect { subject.channels.all }.to raise_error Slack::AlreadyArchivedError
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'raises a channel not archived error if the channel id is not archived' do
|
98
|
+
response = {
|
99
|
+
ok: false,
|
100
|
+
error: "not_archived"
|
101
|
+
}
|
102
|
+
stub_request(:get, "https://slack.com/api/channels.list?exclude_archived=0&token=#{access_token}").
|
103
|
+
to_return(status: 200, body: response.to_json)
|
104
|
+
expect { subject.channels.all }.to raise_error Slack::NotArchivedError
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'raises a general channel archived error if the channel is the general channel' do
|
108
|
+
response = {
|
109
|
+
ok: false,
|
110
|
+
error: "cant_archive_general"
|
111
|
+
}
|
112
|
+
stub_request(:get, "https://slack.com/api/channels.list?exclude_archived=0&token=#{access_token}").
|
113
|
+
to_return(status: 200, body: response.to_json)
|
114
|
+
expect { subject.channels.all }.to raise_error Slack::CantArchiveGeneralError
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'raises a last restricted account error if the channel is restricted' do
|
118
|
+
response = {
|
119
|
+
ok: false,
|
120
|
+
error: "last_ra_channel"
|
121
|
+
}
|
122
|
+
stub_request(:get, "https://slack.com/api/channels.list?exclude_archived=0&token=#{access_token}").
|
123
|
+
to_return(status: 200, body: response.to_json)
|
124
|
+
expect { subject.channels.all }.to raise_error Slack::LastRaChannelError
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'raises a restricted action error if the user does not have the correct permissions' do
|
128
|
+
response = {
|
129
|
+
ok: false,
|
130
|
+
error: "restricted_action"
|
131
|
+
}
|
132
|
+
stub_request(:get, "https://slack.com/api/channels.list?exclude_archived=0&token=#{access_token}").
|
133
|
+
to_return(status: 200, body: response.to_json)
|
134
|
+
expect { subject.channels.all }.to raise_error Slack::RestrictedActionError
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'raises a name taken error if the channel name already exists' do
|
138
|
+
response = {
|
139
|
+
ok: false,
|
140
|
+
error: "name_taken"
|
141
|
+
}
|
142
|
+
stub_request(:get, "https://slack.com/api/channels.list?exclude_archived=0&token=#{access_token}").
|
143
|
+
to_return(status: 200, body: response.to_json)
|
144
|
+
expect { subject.channels.all }.to raise_error Slack::NameTakenError
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'raises a no channel error if the channel name was not provided' do
|
148
|
+
response = {
|
149
|
+
ok: false,
|
150
|
+
error: "no_channel"
|
151
|
+
}
|
152
|
+
stub_request(:get, "https://slack.com/api/channels.list?exclude_archived=0&token=#{access_token}").
|
153
|
+
to_return(status: 200, body: response.to_json)
|
154
|
+
expect { subject.channels.all }.to raise_error Slack::NoChannelError
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'raises an http error if the error is not known' do
|
158
|
+
response = {
|
159
|
+
ok: false,
|
160
|
+
error: "blah"
|
161
|
+
}
|
162
|
+
stub_request(:get, "https://slack.com/api/users.list?token=#{access_token}").
|
163
|
+
to_return(status: 200, body: response.to_json)
|
164
|
+
expect { subject.users.all }.to raise_error Slack::APIError
|
165
|
+
end
|
166
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'webmock/rspec'
|
2
|
+
require 'support/slack_stub_factory'
|
3
|
+
|
4
|
+
require 'laziness'
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.expect_with :rspec do |expectations|
|
8
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
9
|
+
end
|
10
|
+
|
11
|
+
config.mock_with :rspec do |mocks|
|
12
|
+
mocks.verify_partial_doubles = true
|
13
|
+
end
|
14
|
+
|
15
|
+
config.include SlackStubFactory
|
16
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
{
|
2
|
+
"ok": true,
|
3
|
+
"channel": {
|
4
|
+
"id": "C02BLAH",
|
5
|
+
"name": "tour",
|
6
|
+
"is_channel": true,
|
7
|
+
"created": 1403798029,
|
8
|
+
"creator": "U024BLAH",
|
9
|
+
"is_archived": false,
|
10
|
+
"is_general": false,
|
11
|
+
"is_member": true,
|
12
|
+
"members": [
|
13
|
+
"U024BLAH",
|
14
|
+
"U024BLAH2"
|
15
|
+
],
|
16
|
+
"topic": {
|
17
|
+
"value": "",
|
18
|
+
"creator": "",
|
19
|
+
"last_set": 0
|
20
|
+
},
|
21
|
+
"purpose": {
|
22
|
+
"value": "A place to discuss the upcoming tour",
|
23
|
+
"creator": "U024BLAH",
|
24
|
+
"last_set": 1403798029
|
25
|
+
},
|
26
|
+
"num_members": 2
|
27
|
+
}
|
28
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
{
|
2
|
+
"ok": true,
|
3
|
+
"channels": [
|
4
|
+
{
|
5
|
+
"id": "C02BLAH",
|
6
|
+
"name": "tour",
|
7
|
+
"is_channel": true,
|
8
|
+
"created": 1403798029,
|
9
|
+
"creator": "U024BLAH",
|
10
|
+
"is_archived": false,
|
11
|
+
"is_general": false,
|
12
|
+
"is_member": true,
|
13
|
+
"members": [
|
14
|
+
"U024BLAH",
|
15
|
+
"U024BLAH2"
|
16
|
+
],
|
17
|
+
"topic": {
|
18
|
+
"value": "",
|
19
|
+
"creator": "",
|
20
|
+
"last_set": 0
|
21
|
+
},
|
22
|
+
"purpose": {
|
23
|
+
"value": "A place to discuss the upcoming tour",
|
24
|
+
"creator": "U024BLAH",
|
25
|
+
"last_set": 1403798029
|
26
|
+
},
|
27
|
+
"num_members": 2
|
28
|
+
}
|
29
|
+
]
|
30
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
{ "ok": true }
|
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"ok": true,
|
3
|
+
"members": [
|
4
|
+
{
|
5
|
+
"id": "U024BLAH",
|
6
|
+
"name": "jimmy",
|
7
|
+
"deleted": false,
|
8
|
+
"status": "null",
|
9
|
+
"real_name": "Jimmy Page"
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"id": "U024BLAH2",
|
13
|
+
"name": "robert",
|
14
|
+
"deleted": false,
|
15
|
+
"status": "null",
|
16
|
+
"real_name": "Robert Plant"
|
17
|
+
}
|
18
|
+
]
|
19
|
+
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module SlackStubFactory
|
2
|
+
def stub_slack_request(method, path, fixture)
|
3
|
+
full_path = "https://slack.com/api/#{path}"
|
4
|
+
stub_request(method, full_path).to_return(status: 200, body: slack_json_fixture(fixture))
|
5
|
+
end
|
6
|
+
|
7
|
+
def slack_json_fixture(fixture)
|
8
|
+
File.read(File.join('spec', 'support', 'fixtures', fixture)).strip.gsub(/\n\s*/, "")
|
9
|
+
end
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: laziness
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jamie Wright
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hashie
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Laziness wraps the Slack API in a Ruby gem so Ruby programs can easily
|
42
|
+
communicate with the Slack API (http://api.slack.com).
|
43
|
+
email:
|
44
|
+
- jamie@brilliantfantastic.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".ruby-version"
|
52
|
+
- Gemfile
|
53
|
+
- Gemfile.lock
|
54
|
+
- LICENSE
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- laziness.gemspec
|
58
|
+
- lib/laziness.rb
|
59
|
+
- lib/laziness/api.rb
|
60
|
+
- lib/laziness/api/base.rb
|
61
|
+
- lib/laziness/api/channels.rb
|
62
|
+
- lib/laziness/api/users.rb
|
63
|
+
- lib/laziness/base.rb
|
64
|
+
- lib/laziness/channel.rb
|
65
|
+
- lib/laziness/client.rb
|
66
|
+
- lib/laziness/errors.rb
|
67
|
+
- lib/laziness/user.rb
|
68
|
+
- lib/laziness/version.rb
|
69
|
+
- spec/laziness/api/channels_spec.rb
|
70
|
+
- spec/laziness/api/users_spec.rb
|
71
|
+
- spec/laziness/client_spec.rb
|
72
|
+
- spec/laziness/errors_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- spec/support/fixtures/channels_info.json
|
75
|
+
- spec/support/fixtures/channels_list.json
|
76
|
+
- spec/support/fixtures/successful_response.json
|
77
|
+
- spec/support/fixtures/users_info.json
|
78
|
+
- spec/support/fixtures/users_list.json
|
79
|
+
- spec/support/slack_stub_factory.rb
|
80
|
+
homepage: http://github.com/brilliantfantastic/laziness
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '2.0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.2.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: A Slack API wrapper written in Ruby.
|
104
|
+
test_files:
|
105
|
+
- spec/laziness/api/channels_spec.rb
|
106
|
+
- spec/laziness/api/users_spec.rb
|
107
|
+
- spec/laziness/client_spec.rb
|
108
|
+
- spec/laziness/errors_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
- spec/support/fixtures/channels_info.json
|
111
|
+
- spec/support/fixtures/channels_list.json
|
112
|
+
- spec/support/fixtures/successful_response.json
|
113
|
+
- spec/support/fixtures/users_info.json
|
114
|
+
- spec/support/fixtures/users_list.json
|
115
|
+
- spec/support/slack_stub_factory.rb
|