breacan 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: bd1925ffbf6e5ae727056a78968ad71c79e299b5
4
+ data.tar.gz: e13e08f2d165b7e99bf6e52e51bae971ee1d9e71
5
+ SHA512:
6
+ metadata.gz: 7985138b2198118c6147f524a9adf9d9baf1682e66ea4ed7d587b11c6cf7131002553c5450a1199028880b37c2280f4248c9ccc162bb7311289382236f3c9a39
7
+ data.tar.gz: 6d87c8702becbc3b027ec1d4bd4d8957733f840bcf90a5e9f70a608d0348a14eb1a4aa73a6ac919cbf53fd1aeb6af221977b8fde107390cb7524825ce0d5327c
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in breacan.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
7
+ gem 'awesome_print'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 linyows
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,48 @@
1
+ <p align="center">
2
+ <img src="https://d262ilb51hltx0.cloudfront.net/max/2000/1*ryU77MsLt5tnp3ow8kqELA.png">
3
+ <strong>Breacan</strong>: Simple Ruby wrapper for Slack API.
4
+ </p>
5
+
6
+ [![ruby gem](https://img.shields.io/gem/v/breacan.svg?style=flat-square)][gem]
7
+ [gem]: https://rubygems.org/gems/breacan
8
+
9
+ Installation
10
+ ------------
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'breacan'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install breacan
25
+
26
+ Usage
27
+ -----
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ Contributing
32
+ ------------
33
+
34
+ 1. Fork it ( https://github.com/linyows/breacan/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create a new Pull Request
39
+
40
+ Authors
41
+ -------
42
+
43
+ - [linyows](https://github.com/linyows)
44
+
45
+ License
46
+ -------
47
+
48
+ The MIT License (MIT)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/breacan.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 'breacan/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "breacan"
8
+ spec.version = Breacan::VERSION
9
+ spec.authors = ["linyows"]
10
+ spec.email = ["linyows@gmail.com"]
11
+ spec.summary = %q{Simple Ruby wrapper for Slack API.}
12
+ spec.description = %q{Simple Ruby wrapper for Slack API.}
13
+ spec.homepage = "https://github.com/linyows/breacan"
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_dependency "sawyer", "~> 0.6"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
data/lib/breacan.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'breacan/version'
2
+ require 'breacan/client'
3
+
4
+ module Breacan
5
+ class << self
6
+ include Breacan::Configurable
7
+
8
+ def client
9
+ unless defined?(@client) && @client.same_options?(options)
10
+ @client = Breacan::Client.new(options)
11
+ end
12
+ @client
13
+ end
14
+
15
+ private
16
+
17
+ def method_missing(method_name, *args, &block)
18
+ return super unless client.respond_to?(method_name)
19
+ client.send(method_name, *args, &block)
20
+ end
21
+ end
22
+ end
23
+
24
+ Breacan.setup
@@ -0,0 +1,7 @@
1
+ module Breacan
2
+ module Authentication
3
+ def token_authenticated?
4
+ !!@access_token
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,109 @@
1
+ require 'sawyer'
2
+ require 'breacan/configurable'
3
+ require 'breacan/authentication'
4
+ require 'breacan/client/oauth'
5
+ require 'breacan/client/channels'
6
+ require 'breacan/client/chat'
7
+
8
+ module Breacan
9
+ class Client
10
+ include Breacan::Authentication
11
+ include Breacan::Configurable
12
+ include Breacan::Client::OAuth
13
+ include Breacan::Client::Channels
14
+ include Breacan::Client::Chat
15
+
16
+ CONVENIENCE_HEADERS = Set.new(%i(accept content_type))
17
+
18
+ def initialize(options = {})
19
+ Breacan::Configurable.keys.each do |key|
20
+ instance_variable_set(:"@#{key}", options[key] || Breacan.instance_variable_get(:"@#{key}"))
21
+ end
22
+ end
23
+
24
+ def same_options?(opts)
25
+ opts.hash == options.hash
26
+ end
27
+
28
+ def get(url, options = {})
29
+ request :get, url, parse_query_and_convenience_headers(options)
30
+ end
31
+
32
+ def post(url, options = {})
33
+ request :post, url, options
34
+ end
35
+
36
+ def put(url, options = {})
37
+ request :put, url, options
38
+ end
39
+
40
+ def patch(url, options = {})
41
+ request :patch, url, options
42
+ end
43
+
44
+ def delete(url, options = {})
45
+ request :delete, url, options
46
+ end
47
+
48
+ def head(url, options = {})
49
+ request :head, url, parse_query_and_convenience_headers(options)
50
+ end
51
+
52
+ def agent
53
+ @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
54
+ http.headers[:accept] = media_type
55
+ http.headers[:content_type] = media_type
56
+ http.headers[:user_agent] = user_agent
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def request(method, path, data, options = {})
63
+ if data.is_a?(Hash)
64
+ options[:query] = data.delete(:query) || {}
65
+ options[:headers] = data.delete(:headers) || {}
66
+ if accept = data.delete(:accept)
67
+ options[:headers][:accept] = accept
68
+ end
69
+ end
70
+ api_path = URI::Parser.new.escape("#{path.to_s}?token=#{@access_token}")
71
+ @last_response = response = agent.call(method, api_path, data, options)
72
+ response.data
73
+ end
74
+
75
+ def boolean_from_response(method, path, options = {})
76
+ request(method, path, options)
77
+ @last_response.status == 204
78
+ rescue Breacan::NotFound
79
+ false
80
+ end
81
+
82
+ def sawyer_options
83
+ opts = {
84
+ :links_parser => Sawyer::LinkParsers::Simple.new
85
+ }
86
+ conn_opts = @connection_options
87
+ conn_opts[:builder] = @middleware if @middleware
88
+ conn_opts[:proxy] = @proxy if @proxy
89
+ opts[:faraday] = Faraday.new(conn_opts)
90
+
91
+ opts
92
+ end
93
+
94
+ def parse_query_and_convenience_headers(options)
95
+ headers = options.fetch(:headers, {})
96
+ CONVENIENCE_HEADERS.each do |h|
97
+ if header = options.delete(h)
98
+ headers[h] = header
99
+ end
100
+ end
101
+ query = options.delete(:query)
102
+ opts = {:query => options}
103
+ opts[:query].merge!(query) if query && query.is_a?(Hash)
104
+ opts[:headers] = headers unless headers.empty?
105
+
106
+ opts
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,22 @@
1
+ module Breacan
2
+ class Client
3
+ module Channels
4
+ def channels_list
5
+ get 'channels.list'
6
+ end
7
+
8
+ def channels_info(*args)
9
+ get 'channels.info', args
10
+ end
11
+
12
+ def channels_history(*args)
13
+ get 'channels.history', args
14
+ end
15
+
16
+ def channels_info_by_name(name)
17
+ res = channels_list
18
+ res.channels.find { |ch| ch['name'] == name } if res.ok
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ module Breacan
2
+ class Client
3
+ module Chat
4
+ def chat_delete(*args)
5
+ delete 'chat.delete', args
6
+ end
7
+
8
+ def chat_post_message(*args)
9
+ get 'chat.postMessage', args
10
+ end
11
+
12
+ def chat_update(*args)
13
+ get 'chat.update', args
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module Breacan
2
+ class Client
3
+ module OAuth
4
+ def oauth_access(*args)
5
+ post 'oauth.access', args
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,60 @@
1
+ require 'breacan/response/raise_error'
2
+
3
+ module Breacan
4
+ module Configurable
5
+ OPTIONS_KEYS = %i(
6
+ access_token
7
+ team
8
+ api_endpoint
9
+ web_endpoint
10
+ user_agent
11
+ media_type
12
+ connection_options
13
+ proxy
14
+ middleware
15
+ )
16
+
17
+ attr_accessor(*OPTIONS_KEYS)
18
+
19
+ class << self
20
+ def keys
21
+ @keys ||= OPTIONS_KEYS
22
+ end
23
+ end
24
+
25
+ def configure
26
+ yield self
27
+ end
28
+
29
+ def api_endpoint
30
+ File.join(@api_endpoint, '')
31
+ end
32
+
33
+ def web_endpoint
34
+ File.join(@web_endpoint, '')
35
+ end
36
+
37
+ def reset!
38
+ @access_token = ENV['BREACAN_ACCESS_TOKEN']
39
+ @team = ENV['BREACAN_TEAM']
40
+ @api_endpoint = ENV['BREACAN_API_ENDPOINT'] || 'https://slack.com/api/'
41
+ @web_endpoint = ENV['BREACAN_WEB_ENDPOINT'] || "https://#{@team}.slack.com/"
42
+ @user_agent = "Breacan Ruby Gem #{Breacan::VERSION}"
43
+ @media_type = 'application/json'
44
+ @connection_options = { headers: { accept: media_type, user_agent: user_agent } }
45
+ @proxy = ENV['BREACAN_PROXY']
46
+ @middleware = Faraday::RackBuilder.new { |builder|
47
+ builder.use Breacan::Response::RaiseError
48
+ builder.adapter Faraday.default_adapter
49
+ }
50
+ self
51
+ end
52
+ alias setup reset!
53
+
54
+ def options
55
+ Hash[Breacan::Configurable.keys.map { |key|
56
+ [key, instance_variable_get(:"@#{key}")]
57
+ }]
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,104 @@
1
+ module Breacan
2
+ class Error < StandardError
3
+ def self.from_response(response)
4
+ if klass = case response[:status].to_i
5
+ when 400 then Breacan::BadRequest
6
+ when 401 then Breacan::Unauthorized
7
+ when 403 then Breacan::Forbidden
8
+ when 404 then Breacan::NotFound
9
+ when 406 then Breacan::NotAcceptable
10
+ when 409 then Breacan::Conflict
11
+ when 415 then Breacan::UnsupportedMediaType
12
+ when 422 then Breacan::UnprocessableEntity
13
+ when 400..499 then Breacan::ClientError
14
+ when 500 then Breacan::InternalServerError
15
+ when 501 then Breacan::NotImplemented
16
+ when 502 then Breacan::BadGateway
17
+ when 503 then Breacan::ServiceUnavailable
18
+ when 500..599 then Breacan::ServerError
19
+ end
20
+ klass.new(response)
21
+ end
22
+ end
23
+
24
+ def initialize(response = nil)
25
+ @response = response
26
+ super(build_error_message)
27
+ end
28
+
29
+ def errors
30
+ if data && data.is_a?(Hash)
31
+ data[:errors] || []
32
+ else
33
+ []
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def data
40
+ @data ||=
41
+ if (body = @response[:body]) && !body.empty?
42
+ if body.is_a?(String) &&
43
+ @response[:response_headers] &&
44
+ @response[:response_headers][:content_type] =~ /json/
45
+
46
+ Sawyer::Agent.serializer.decode(body)
47
+ else
48
+ body
49
+ end
50
+ else
51
+ nil
52
+ end
53
+ end
54
+
55
+ def response_message
56
+ case data
57
+ when Hash
58
+ data[:message]
59
+ when String
60
+ data
61
+ end
62
+ end
63
+
64
+ def response_error
65
+ "Error: #{data[:error]}" if data.is_a?(Hash) && data[:error]
66
+ end
67
+
68
+ def build_error_message
69
+ return nil if @response.nil?
70
+
71
+ message = "#{@response[:method].to_s.upcase} "
72
+ message << redact_url(@response[:url].to_s) + ": "
73
+ message << "#{@response[:status]} - "
74
+ message << "#{response_message}" unless response_message.nil?
75
+ message << "#{response_error}" unless response_error.nil?
76
+ message
77
+ end
78
+
79
+ def redact_url(url_string)
80
+ %w[client_secret access_token].each do |token|
81
+ url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token
82
+ end
83
+ url_string
84
+ end
85
+ end
86
+
87
+ class ClientError < Error; end
88
+ class BadRequest < ClientError; end
89
+ class Unauthorized < ClientError; end
90
+ class Forbidden < ClientError; end
91
+ class TooManyRequests < Forbidden; end
92
+ class TooManyLoginAttempts < Forbidden; end
93
+ class NotFound < ClientError; end
94
+ class NotAcceptable < ClientError; end
95
+ class Conflict < ClientError; end
96
+ class UnsupportedMediaType < ClientError; end
97
+ class UnprocessableEntity < ClientError; end
98
+ class ServerError < Error; end
99
+ class InternalServerError < ServerError; end
100
+ class NotImplemented < ServerError; end
101
+ class BadGateway < ServerError; end
102
+ class ServiceUnavailable < ServerError; end
103
+ class MissingContentType < ArgumentError; end
104
+ end
@@ -0,0 +1,16 @@
1
+ require 'faraday'
2
+ require 'breacan/error'
3
+
4
+ module Breacan
5
+ module Response
6
+ class RaiseError < Faraday::Response::Middleware
7
+ private
8
+
9
+ def on_complete(response)
10
+ if error = Breacan::Error.from_response(response)
11
+ raise error
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Breacan
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: breacan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - linyows
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sawyer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Simple Ruby wrapper for Slack API.
56
+ email:
57
+ - linyows@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - breacan.gemspec
68
+ - lib/breacan.rb
69
+ - lib/breacan/authentication.rb
70
+ - lib/breacan/client.rb
71
+ - lib/breacan/client/channels.rb
72
+ - lib/breacan/client/chat.rb
73
+ - lib/breacan/client/oauth.rb
74
+ - lib/breacan/configurable.rb
75
+ - lib/breacan/error.rb
76
+ - lib/breacan/response/raise_error.rb
77
+ - lib/breacan/version.rb
78
+ homepage: https://github.com/linyows/breacan
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Simple Ruby wrapper for Slack API.
102
+ test_files: []