slack-web-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +6 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +13 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +33 -0
  10. data/Rakefile +13 -0
  11. data/examples/basic.rb +35 -0
  12. data/lib/faraday/raise_http_exception.rb +51 -0
  13. data/lib/slack.rb +27 -0
  14. data/lib/slack/api.rb +24 -0
  15. data/lib/slack/client.rb +4 -0
  16. data/lib/slack/configuration.rb +76 -0
  17. data/lib/slack/connection.rb +29 -0
  18. data/lib/slack/endpoint.rb +43 -0
  19. data/lib/slack/endpoint/api.rb +23 -0
  20. data/lib/slack/endpoint/auth.rb +19 -0
  21. data/lib/slack/endpoint/channels.rb +236 -0
  22. data/lib/slack/endpoint/chat.rb +86 -0
  23. data/lib/slack/endpoint/emoji.rb +19 -0
  24. data/lib/slack/endpoint/files.rb +100 -0
  25. data/lib/slack/endpoint/groups.rb +262 -0
  26. data/lib/slack/endpoint/im.rb +90 -0
  27. data/lib/slack/endpoint/mpim.rb +90 -0
  28. data/lib/slack/endpoint/oauth.rb +31 -0
  29. data/lib/slack/endpoint/pins.rb +64 -0
  30. data/lib/slack/endpoint/presence.rb +22 -0
  31. data/lib/slack/endpoint/reactions.rb +94 -0
  32. data/lib/slack/endpoint/search.rb +80 -0
  33. data/lib/slack/endpoint/stars.rb +65 -0
  34. data/lib/slack/endpoint/team.rb +57 -0
  35. data/lib/slack/endpoint/usergroups.rb +122 -0
  36. data/lib/slack/endpoint/users.rb +78 -0
  37. data/lib/slack/error.rb +19 -0
  38. data/lib/slack/request.rb +41 -0
  39. data/lib/slack/version.rb +3 -0
  40. data/slack.gemspec +38 -0
  41. data/spec/api_spec.rb +23 -0
  42. data/spec/auth_spec.rb +22 -0
  43. data/spec/channels_spec.rb +247 -0
  44. data/spec/spec_helper.rb +86 -0
  45. metadata +313 -0
@@ -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
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,3 @@
1
+ [submodule "slack-api-docs"]
2
+ path = slack-api-docs
3
+ url = git@github.com:aki017/slack-api-docs
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 2.1
3
+ - 2.2
4
+
5
+ git:
6
+ submodules: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slack.gemspec
4
+ gemspec
@@ -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
@@ -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.
@@ -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
@@ -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]
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,4 @@
1
+ module Slack
2
+ class Client < API
3
+ end
4
+ end
@@ -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