slack-web-api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.gitmodules +3 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/Guardfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +13 -0
- data/examples/basic.rb +35 -0
- data/lib/faraday/raise_http_exception.rb +51 -0
- data/lib/slack.rb +27 -0
- data/lib/slack/api.rb +24 -0
- data/lib/slack/client.rb +4 -0
- data/lib/slack/configuration.rb +76 -0
- data/lib/slack/connection.rb +29 -0
- data/lib/slack/endpoint.rb +43 -0
- data/lib/slack/endpoint/api.rb +23 -0
- data/lib/slack/endpoint/auth.rb +19 -0
- data/lib/slack/endpoint/channels.rb +236 -0
- data/lib/slack/endpoint/chat.rb +86 -0
- data/lib/slack/endpoint/emoji.rb +19 -0
- data/lib/slack/endpoint/files.rb +100 -0
- data/lib/slack/endpoint/groups.rb +262 -0
- data/lib/slack/endpoint/im.rb +90 -0
- data/lib/slack/endpoint/mpim.rb +90 -0
- data/lib/slack/endpoint/oauth.rb +31 -0
- data/lib/slack/endpoint/pins.rb +64 -0
- data/lib/slack/endpoint/presence.rb +22 -0
- data/lib/slack/endpoint/reactions.rb +94 -0
- data/lib/slack/endpoint/search.rb +80 -0
- data/lib/slack/endpoint/stars.rb +65 -0
- data/lib/slack/endpoint/team.rb +57 -0
- data/lib/slack/endpoint/usergroups.rb +122 -0
- data/lib/slack/endpoint/users.rb +78 -0
- data/lib/slack/error.rb +19 -0
- data/lib/slack/request.rb +41 -0
- data/lib/slack/version.rb +3 -0
- data/slack.gemspec +38 -0
- data/spec/api_spec.rb +23 -0
- data/spec/auth_spec.rb +22 -0
- data/spec/channels_spec.rb +247 -0
- data/spec/spec_helper.rb +86 -0
- metadata +313 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5c83a89cf20026e6fa6c74cfdc06f94bea085bc8
|
4
|
+
data.tar.gz: aa2a9c6fea05deb01bde20b385faa0f6567d50b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a3315e3600aea6e1169d3aac7388e6e85ee8e8e58f03c206700f337dd705a0529321cbb87ddb5f7b34a21be36102a79d8fccb4bb399aae69a2f5d63cd586eca3
|
7
|
+
data.tar.gz: 64a3d8839c95d9ade22b5bd500773325debc1b17f086df6c5d8ab07e20137a3129f77acd9c9d30f0f3c2aa37e73ab50d5a7b8660c3abd466ccdbd73eec460b4d
|
data/.gitignore
ADDED
data/.gitmodules
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
clearing true
|
2
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
3
|
+
require "guard/rspec/dsl"
|
4
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
5
|
+
|
6
|
+
rspec = dsl.rspec
|
7
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
8
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
9
|
+
watch(rspec.spec_files)
|
10
|
+
|
11
|
+
ruby = dsl.ruby
|
12
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 aki017
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Slack Web API
|
2
|
+
|
3
|
+
A Ruby wrapper for the Slack Web API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'slack-web-api'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install slack-web-api
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require "slack"
|
23
|
+
|
24
|
+
client = Slack::Client.new token: token
|
25
|
+
client.auth_test
|
26
|
+
```
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it ( http://github.com/aki017/slack-ruby-gem/fork )
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require_relative "./lib/generators/tasks/generate"
|
3
|
+
|
4
|
+
Bundler.setup :default, :development
|
5
|
+
|
6
|
+
require 'rspec/core'
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
10
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
11
|
+
end
|
12
|
+
|
13
|
+
task default: [:spec]
|
data/examples/basic.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "slack"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
token = ENV["TOKEN"] || (print "Token: "; gets.strip)
|
5
|
+
client = Slack::Client.new token: token
|
6
|
+
|
7
|
+
# Get users list
|
8
|
+
puts "Get users list"
|
9
|
+
users = Hash[client.users_list["members"].map{|m| [m["id"], m["name"]]}]
|
10
|
+
|
11
|
+
puts YAML.dump users
|
12
|
+
puts
|
13
|
+
puts
|
14
|
+
|
15
|
+
# Get channels list
|
16
|
+
puts "Get channels list"
|
17
|
+
channels = client.channels_list["channels"]
|
18
|
+
puts YAML.dump channels
|
19
|
+
puts
|
20
|
+
puts
|
21
|
+
|
22
|
+
channels.each do |c|
|
23
|
+
puts "- id: #{c["id"]}, name: #{c["name"]}"
|
24
|
+
|
25
|
+
# Get channel histry
|
26
|
+
messages = client.channels_history(channel: "#{c["id"]}")["messages"]
|
27
|
+
messages.each do |message|
|
28
|
+
user_name = users[message["user"]]
|
29
|
+
text = message["text"].inspect
|
30
|
+
puts " - #{user_name}: #{text}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
sleep 1
|
35
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module FaradayMiddleware
|
5
|
+
# @private
|
6
|
+
class RaiseHttpException < Faraday::Middleware
|
7
|
+
def call(env)
|
8
|
+
@app.call(env).on_complete do |response|
|
9
|
+
case response[:status].to_i
|
10
|
+
when 400
|
11
|
+
raise Slack::BadRequest, error_message_400(response)
|
12
|
+
when 404
|
13
|
+
raise Slack::NotFound, error_message_400(response)
|
14
|
+
when 500
|
15
|
+
raise Slack::InternalServerError, error_message_500(response, "Something is technically wrong.")
|
16
|
+
when 503
|
17
|
+
raise Slack::ServiceUnavailable, error_message_500(response, "Slack is rate limiting your requests.")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(app)
|
23
|
+
super app
|
24
|
+
@parser = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def error_message_400(response)
|
30
|
+
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def error_body(body)
|
34
|
+
# body gets passed as a string, not sure if it is passed as something else from other spots?
|
35
|
+
if not body.nil? and not body.empty? and body.kind_of?(String)
|
36
|
+
# removed multi_json thanks to wesnolte's commit
|
37
|
+
body = ::JSON.parse(body)
|
38
|
+
end
|
39
|
+
|
40
|
+
if body.nil?
|
41
|
+
nil
|
42
|
+
elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty?
|
43
|
+
": #{body['meta']['error_message']}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def error_message_500(response, body=nil)
|
48
|
+
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/slack.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative "slack/error"
|
2
|
+
require_relative "slack/configuration"
|
3
|
+
require_relative "slack/api"
|
4
|
+
require_relative "slack/client"
|
5
|
+
require_relative "slack/version"
|
6
|
+
|
7
|
+
module Slack
|
8
|
+
extend Configuration
|
9
|
+
|
10
|
+
# Alias for Slack::Client.new
|
11
|
+
#
|
12
|
+
# @return [Slack::Client]
|
13
|
+
def self.client(options={})
|
14
|
+
Slack::Client.new(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Delegate to Slack::Client
|
18
|
+
def self.method_missing(method, *args, &block)
|
19
|
+
return super unless client.respond_to?(method)
|
20
|
+
client.send(method, *args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Delegate to Slack::Client
|
24
|
+
def self.respond_to?(method, include_all=false)
|
25
|
+
return client.respond_to?(method, include_all) || super
|
26
|
+
end
|
27
|
+
end
|
data/lib/slack/api.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path('../connection', __FILE__)
|
2
|
+
require File.expand_path('../request', __FILE__)
|
3
|
+
require File.expand_path('../configuration', __FILE__)
|
4
|
+
require File.expand_path('../endpoint', __FILE__)
|
5
|
+
|
6
|
+
module Slack
|
7
|
+
# @private
|
8
|
+
class API
|
9
|
+
# @private
|
10
|
+
attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
|
11
|
+
|
12
|
+
# Creates a new API
|
13
|
+
def initialize(options={})
|
14
|
+
options = Slack.options.merge(options)
|
15
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
16
|
+
send("#{key}=", options[key])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
include Connection
|
21
|
+
include Request
|
22
|
+
include Endpoint
|
23
|
+
end
|
24
|
+
end
|
data/lib/slack/client.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require File.expand_path('../version', __FILE__)
|
3
|
+
|
4
|
+
module Slack
|
5
|
+
# Defines constants and methods related to configuration
|
6
|
+
module Configuration
|
7
|
+
# An array of valid keys in the options hash when configuring a {Slack::API}
|
8
|
+
VALID_OPTIONS_KEYS = [
|
9
|
+
:adapter,
|
10
|
+
:token,
|
11
|
+
:endpoint,
|
12
|
+
:user_agent,
|
13
|
+
:proxy,
|
14
|
+
:ca_path,
|
15
|
+
:ca_file,
|
16
|
+
:middlewares
|
17
|
+
].freeze
|
18
|
+
|
19
|
+
# The adapter that will be used to connect if none is set
|
20
|
+
#
|
21
|
+
# @note The default faraday adapter is Net::HTTP.
|
22
|
+
DEFAULT_ADAPTER = Faraday.default_adapter
|
23
|
+
|
24
|
+
# By default, don't set an token
|
25
|
+
DEFAULT_TOKEN = nil
|
26
|
+
|
27
|
+
# The endpoint that will be used to connect if none is set
|
28
|
+
#
|
29
|
+
# @note There is no reason to use any other endpoint at this time
|
30
|
+
DEFAULT_ENDPOINT = 'https://slack.com/api/'.freeze
|
31
|
+
|
32
|
+
# By default, don't use a proxy server
|
33
|
+
DEFAULT_PROXY = nil
|
34
|
+
|
35
|
+
# The user agent that will be sent to the API endpoint if none is set
|
36
|
+
DEFAULT_USER_AGENT = "Slack Ruby Gem #{Slack::VERSION}".freeze
|
37
|
+
|
38
|
+
# Default openssl CA_PATH and CA_FILE path
|
39
|
+
DEFAULT_CA_PATH = %x[ openssl version -a | grep OPENSSLDIR | awk '{print $2}'|sed -e 's/\"//g' ]
|
40
|
+
DEFAULT_CA_FILE = "#{DEFAULT_CA_PATH}/ca-certificates.crt"
|
41
|
+
|
42
|
+
DEFAULT_MIDDLEWARES = []
|
43
|
+
|
44
|
+
# @private
|
45
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
46
|
+
|
47
|
+
# When this module is extended, set all configuration options to their default values
|
48
|
+
def self.extended(base)
|
49
|
+
base.reset
|
50
|
+
end
|
51
|
+
|
52
|
+
# Convenience method to allow configuration options to be set in a block
|
53
|
+
def configure
|
54
|
+
yield self
|
55
|
+
end
|
56
|
+
|
57
|
+
# Create a hash of options and their values
|
58
|
+
def options
|
59
|
+
VALID_OPTIONS_KEYS.inject({}) do |option, key|
|
60
|
+
option.merge!(key => send(key))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Reset all configuration options to defaults
|
65
|
+
def reset
|
66
|
+
self.adapter = DEFAULT_ADAPTER
|
67
|
+
self.token = DEFAULT_TOKEN
|
68
|
+
self.endpoint = DEFAULT_ENDPOINT
|
69
|
+
self.user_agent = DEFAULT_USER_AGENT
|
70
|
+
self.proxy = DEFAULT_PROXY
|
71
|
+
self.ca_path = DEFAULT_CA_PATH
|
72
|
+
self.ca_file = DEFAULT_CA_FILE
|
73
|
+
self.middlewares = DEFAULT_MIDDLEWARES
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
Dir[File.expand_path('../../faraday/*.rb', __FILE__)].each{|f| require f}
|
3
|
+
|
4
|
+
module Slack
|
5
|
+
# @private
|
6
|
+
module Connection
|
7
|
+
private
|
8
|
+
|
9
|
+
def connection
|
10
|
+
options = {
|
11
|
+
:headers => {'Accept' => "application/json; charset=utf-8", 'User-Agent' => user_agent},
|
12
|
+
:proxy => proxy,
|
13
|
+
:url => endpoint,
|
14
|
+
:ssl => { :ca_path => ca_path, :ca_file => ca_file },
|
15
|
+
}
|
16
|
+
|
17
|
+
Faraday::Connection.new(options) do |connection|
|
18
|
+
Array(middlewares).each do |middleware|
|
19
|
+
connection.use middleware
|
20
|
+
end
|
21
|
+
connection.use Faraday::Request::Multipart
|
22
|
+
connection.use Faraday::Request::UrlEncoded
|
23
|
+
connection.use Faraday::Response::ParseJson
|
24
|
+
connection.use FaradayMiddleware::RaiseHttpException
|
25
|
+
connection.adapter(adapter)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# This file was auto-generated by lib/generators/tasks/generate.rb
|
2
|
+
#
|
3
|
+
require_relative 'endpoint/api'
|
4
|
+
require_relative 'endpoint/auth'
|
5
|
+
require_relative 'endpoint/channels'
|
6
|
+
require_relative 'endpoint/chat'
|
7
|
+
require_relative 'endpoint/emoji'
|
8
|
+
require_relative 'endpoint/files'
|
9
|
+
require_relative 'endpoint/groups'
|
10
|
+
require_relative 'endpoint/im'
|
11
|
+
require_relative 'endpoint/mpim'
|
12
|
+
require_relative 'endpoint/oauth'
|
13
|
+
require_relative 'endpoint/pins'
|
14
|
+
require_relative 'endpoint/presence'
|
15
|
+
require_relative 'endpoint/reactions'
|
16
|
+
require_relative 'endpoint/search'
|
17
|
+
require_relative 'endpoint/stars'
|
18
|
+
require_relative 'endpoint/team'
|
19
|
+
require_relative 'endpoint/usergroups'
|
20
|
+
require_relative 'endpoint/users'
|
21
|
+
|
22
|
+
module Slack
|
23
|
+
module Endpoint
|
24
|
+
include Api
|
25
|
+
include Auth
|
26
|
+
include Channels
|
27
|
+
include Chat
|
28
|
+
include Emoji
|
29
|
+
include Files
|
30
|
+
include Groups
|
31
|
+
include Im
|
32
|
+
include Mpim
|
33
|
+
include Oauth
|
34
|
+
include Pins
|
35
|
+
include Presence
|
36
|
+
include Reactions
|
37
|
+
include Search
|
38
|
+
include Stars
|
39
|
+
include Team
|
40
|
+
include Usergroups
|
41
|
+
include Users
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# This file was auto-generated by lib/generators/tasks/generate.rb
|
2
|
+
|
3
|
+
module Slack
|
4
|
+
module Endpoint
|
5
|
+
module Api
|
6
|
+
#
|
7
|
+
# This method helps you test your calling code.
|
8
|
+
#
|
9
|
+
# @option options [Object] :error
|
10
|
+
# Error response to return
|
11
|
+
# @option options [Object] :foo
|
12
|
+
# example property to return
|
13
|
+
# @see https://api.slack.com/methods/api.test
|
14
|
+
# @see https://github.com/aki017/slack-api-docs/blob/master/methods/api.test.md
|
15
|
+
# @see https://github.com/aki017/slack-api-docs/blob/master/methods/api.test.json
|
16
|
+
def api_test(options={})
|
17
|
+
options[:attachments] = options[:attachments].to_json if Hash === options[:attachments]
|
18
|
+
post("api.test", options)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|