slack-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 541dc284ef2a2942a179bc3fb6e2a6bc2680a9cf
4
+ data.tar.gz: e6072a5aa8026a15cbcb32f873d2006d480acd7e
5
+ SHA512:
6
+ metadata.gz: 573b5b20ee3b8e0c05b458ee993a3f94519ae17043422dfa7141a0c502fb3fd08c4edd4e518194326322a1d511e3b32c41cd46444b2d1c0ae9428954110db1dc
7
+ data.tar.gz: 0fc48c5f5cb3292606f292e57b0ccf9653c4e7955202ba34ab2229a34fc5cf923fa7f84c598b82257128e067568702bb3339c1cf9d7e86ea5c2d995898c7cb53
data/.gitignore ADDED
@@ -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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slack.gemspec
4
+ gemspec
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,37 @@
1
+ # Slack
2
+
3
+ A Ruby wrapper for the Slack API
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'slack-api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install slack-api
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require "slack"
23
+
24
+ Slack.configure do |config|
25
+ config.token = "YOUR_TOKEN"
26
+ end
27
+
28
+ Slack.auth_test
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( http://github.com/aki017/slack-ruby-gem/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 "slack/error"
2
+ require "slack/configuration"
3
+ require "slack/api"
4
+ require "slack/client"
5
+ require "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)
25
+ return client.respond_to?(method) || super
26
+ end
27
+ end
data/lib/slack/api.rb ADDED
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../connection', __FILE__)
2
+ require File.expand_path('../request', __FILE__)
3
+
4
+ module Slack
5
+ # @private
6
+ class API
7
+ # @private
8
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
9
+
10
+ # Creates a new API
11
+ def initialize(options={})
12
+ options = Slack.options.merge(options)
13
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
14
+ send("#{key}=", options[key])
15
+ end
16
+ end
17
+
18
+ include Connection
19
+ include Request
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module Slack
2
+ class Client < API
3
+ METHODS = %w(users.list channels.history channels.mark
4
+ channels.list files.upload files.list
5
+ im.history im.list groups.history groups.list
6
+ search.all search.files search.messages
7
+ chat.postMessage auth.test)
8
+
9
+ def respond_to?(method)
10
+ return METHODS.include?(method.id2name.gsub("_", ".")) || super
11
+ end
12
+
13
+ def method_missing(action, *args)
14
+ get(action.id2name.gsub("_", "."), *args)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,64 @@
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
+ ].freeze
15
+
16
+ # The adapter that will be used to connect if none is set
17
+ #
18
+ # @note The default faraday adapter is Net::HTTP.
19
+ DEFAULT_ADAPTER = Faraday.default_adapter
20
+
21
+ # By default, don't set an token
22
+ DEFAULT_TOKEN = nil
23
+
24
+ # The endpoint that will be used to connect if none is set
25
+ #
26
+ # @note There is no reason to use any other endpoint at this time
27
+ DEFAULT_ENDPOINT = 'https://slack.com/api/'.freeze
28
+
29
+ # By default, don't use a proxy server
30
+ DEFAULT_PROXY = nil
31
+
32
+ # The user agent that will be sent to the API endpoint if none is set
33
+ DEFAULT_USER_AGENT = "Slack Ruby Gem #{Slack::VERSION}".freeze
34
+
35
+ # @private
36
+ attr_accessor *VALID_OPTIONS_KEYS
37
+
38
+ # When this module is extended, set all configuration options to their default values
39
+ def self.extended(base)
40
+ base.reset
41
+ end
42
+
43
+ # Convenience method to allow configuration options to be set in a block
44
+ def configure
45
+ yield self
46
+ end
47
+
48
+ # Create a hash of options and their values
49
+ def options
50
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
51
+ option.merge!(key => send(key))
52
+ end
53
+ end
54
+
55
+ # Reset all configuration options to defaults
56
+ def reset
57
+ self.adapter = DEFAULT_ADAPTER
58
+ self.token = DEFAULT_TOKEN
59
+ self.endpoint = DEFAULT_ENDPOINT
60
+ self.user_agent = DEFAULT_USER_AGENT
61
+ self.proxy = DEFAULT_PROXY
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,24 @@
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
+ }
15
+
16
+ Faraday::Connection.new(options) do |connection|
17
+ connection.use Faraday::Request::UrlEncoded
18
+ connection.use Faraday::Response::ParseJson
19
+ connection.use FaradayMiddleware::RaiseHttpException
20
+ connection.adapter(adapter)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module Slack
2
+ # Custom error class for rescuing from all Slack errors
3
+ class Error < StandardError; end
4
+
5
+ # Raised when Slack returns the HTTP status code 400
6
+ class BadRequest < Error; end
7
+
8
+ # Raised when Slack returns the HTTP status code 404
9
+ class NotFound < Error; end
10
+
11
+ # Raised when Slack returns the HTTP status code 500
12
+ class InternalServerError < Error; end
13
+
14
+ # Raised when Slack returns the HTTP status code 503
15
+ class ServiceUnavailable < Error; end
16
+
17
+ # Raised when a subscription payload hash is invalid
18
+ class InvalidSignature < Error; end
19
+ end
@@ -0,0 +1,41 @@
1
+ module Slack
2
+ # Defines HTTP request methods
3
+ module Request
4
+ # Perform an HTTP GET request
5
+ def get(path, options={})
6
+ request(:get, path, options)
7
+ end
8
+
9
+ # Perform an HTTP POST request
10
+ def post(path, options={})
11
+ request(:post, path, options)
12
+ end
13
+
14
+ # Perform an HTTP PUT request
15
+ def put(path, options={})
16
+ request(:put, path, options)
17
+ end
18
+
19
+ # Perform an HTTP DELETE request
20
+ def delete(path, options={})
21
+ request(:delete, path, options)
22
+ end
23
+
24
+ private
25
+
26
+ # Perform an HTTP request
27
+ def request(method, path, options)
28
+ options.merge!(token: token)
29
+ response = connection.send(method) do |request|
30
+ case method
31
+ when :get, :delete
32
+ request.url(path, options)
33
+ when :post, :put
34
+ request.path = path
35
+ request.body = options unless options.empty?
36
+ end
37
+ end
38
+ return response.body
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Slack
2
+ VERSION = "0.0.1"
3
+ end
data/slack.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "slack/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slack-api"
8
+ spec.version = Slack::VERSION
9
+ spec.authors = ["aki017"]
10
+ spec.email = ["aki@aki017.info"]
11
+ spec.summary = %q{A Ruby wrapper for the Slack API}
12
+ spec.description = %q{A Ruby wrapper for the Slack API}
13
+ spec.homepage = "https://github.com/aki017/slack-ruby-gem"
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.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rspec", "~> 2.4"
23
+ spec.add_development_dependency "webmock", "~> 1.6"
24
+ spec.add_runtime_dependency "faraday", [">= 0.7", "<= 0.9"]
25
+ spec.add_runtime_dependency "faraday_middleware", "~> 0.8"
26
+ spec.add_runtime_dependency "multi_json", ">= 1.0.3", "~> 1.0"
27
+ end
28
+
@@ -0,0 +1,7 @@
1
+ RSpec.configure do |config|
2
+ config.treat_symbols_as_metadata_keys_with_true_values = true
3
+ config.run_all_when_everything_filtered = true
4
+ config.filter_run :focus
5
+
6
+ config.order = 'random'
7
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slack-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - aki017
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: webmock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ - - "<="
63
+ - !ruby/object:Gem::Version
64
+ version: '0.9'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0.7'
72
+ - - "<="
73
+ - !ruby/object:Gem::Version
74
+ version: '0.9'
75
+ - !ruby/object:Gem::Dependency
76
+ name: faraday_middleware
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.8'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.8'
89
+ - !ruby/object:Gem::Dependency
90
+ name: multi_json
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 1.0.3
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '1.0'
99
+ type: :runtime
100
+ prerelease: false
101
+ version_requirements: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 1.0.3
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '1.0'
109
+ description: A Ruby wrapper for the Slack API
110
+ email:
111
+ - aki@aki017.info
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files: []
115
+ files:
116
+ - ".gitignore"
117
+ - ".rspec"
118
+ - Gemfile
119
+ - LICENSE.txt
120
+ - README.md
121
+ - Rakefile
122
+ - lib/faraday/raise_http_exception.rb
123
+ - lib/slack.rb
124
+ - lib/slack/api.rb
125
+ - lib/slack/client.rb
126
+ - lib/slack/configuration.rb
127
+ - lib/slack/connection.rb
128
+ - lib/slack/error.rb
129
+ - lib/slack/request.rb
130
+ - lib/slack/version.rb
131
+ - slack.gemspec
132
+ - spec/spec_helper.rb
133
+ homepage: https://github.com/aki017/slack-ruby-gem
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.2.0
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: A Ruby wrapper for the Slack API
157
+ test_files:
158
+ - spec/spec_helper.rb