slack-api-wrapper 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dedcbbf1b36c42b4eb57b2ed9ae32426db56e031
4
- data.tar.gz: 72720e24918c02d42e573c659ca31e3540207a1f
3
+ metadata.gz: 4f09a1ec435533832987741dbb6bb3c8f041c94d
4
+ data.tar.gz: 8bb3fc0fc558d9ddf956e7ce49cc63bbbee35093
5
5
  SHA512:
6
- metadata.gz: 8795bfe54af70f6317508ece2bea2c0d2ea94feeb242f47ed6db8a82a95ac4563e2c3effeebc2d322d520d46455836c93fa4e4538154e773729969ee9596c9ef
7
- data.tar.gz: b0de368f122442d43af86c55aab8372f7a73c20244a7b15348dce275623bfa566abd1ce5880a8fd08ad781fed113f625011e804dfa764201057d02c0aa1da3fc
6
+ metadata.gz: 037aea41e976706faf3ac23e1fa9330b8fd2e1d732ec5a791998aa81cfafa2fd60cb6c45502b5e982e5c1affe562d258a9efa684fc1d4df5fcdbb7f2fa77e6bd
7
+ data.tar.gz: 0647730f20756766677fb8b20adbc2b8031e969ede4832c76e31f59b2404b02f81899f81c567c8e55f78d277917002f4fb2566ba037ba978e70f199bdc3aa5cb
data/.codeclimate.yml ADDED
@@ -0,0 +1,2 @@
1
+ languages:
2
+ Ruby: true
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
File without changes
data/.travis.yml CHANGED
@@ -1,3 +1,20 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.5
3
+ - 2.0.0
4
+ - 2.1
5
+ - 2.2.2
6
+ - ruby-head
7
+ - jruby
8
+ - jruby-head
9
+
10
+ sudo: false
11
+
12
+ script: 'bundle exec rake spec'
13
+
14
+ cache: bundler
15
+
16
+ matrix:
17
+ allow_failures:
18
+ - rvm: ruby-head
19
+ - rvm: jruby-head
20
+ fast_finish: true
data/.yardopts ADDED
@@ -0,0 +1,9 @@
1
+ --protected
2
+ --no-private
3
+ --embed-mixin ClassMethods
4
+ --markup markdown
5
+ -
6
+ README.md
7
+ CHANGELOG.md
8
+ CODE_OF_CONDUCT.md
9
+ MIT-LICENSE
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ### 0.1.0 2015-09-26
2
+ * Remove oauth logic (use omniauth-slack instead)
3
+ * Improve error handling and fix some bugs with them
4
+ * Make all api request thru a post method instead of a get
5
+ * Add pins and reactions endpoints
6
+ * Complete README
7
+ * Add this file
8
+
9
+ ### 0.0.6 2015-03-16
10
+ * initial functional release
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
File without changes
data/README.md CHANGED
@@ -1,30 +1,70 @@
1
- Slack API Wrapper [![Build Status](https://travis-ci.org/gssbzn/slack-api-wrapper.svg)](https://travis-ci.org/gssbzn/slack-api-wrapper)
2
- =========================
1
+ # Slack API Wrapper
2
+ [![Gem Version](https://badge.fury.io/rb/slack-api-wrapper.svg)](http://badge.fury.io/rb/slack-api-wrapper)
3
+ [![Build Status](https://travis-ci.org/gssbzn/slack-api-wrapper.svg)](https://travis-ci.org/gssbzn/slack-api-wrapper)
4
+ [![Code Climate](https://codeclimate.com/github/gssbzn/slack-api-wrapper/badges/gpa.svg)](https://codeclimate.com/github/gssbzn/slack-api-wrapper)
5
+ [![Coverage Status](https://coveralls.io/repos/gssbzn/slack-api-wrapper/badge.svg?branch=master&service=github)](https://coveralls.io/github/gssbzn/slack-api-wrapper)
3
6
 
4
7
  A library that provides a plain function-call interface to the Slack API web endpoints.
5
8
 
6
- This a work in progress, many API endpoint implementations are still missing,
7
- I expect to add them as soon as possible or you can help by making a pull request.
8
-
9
9
  ## Installation
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
- gem 'slack-api-wrapper'
13
+ ```ruby
14
+ gem 'slack-api-wrapper'
15
+ ```
14
16
 
15
17
  And then execute:
16
18
 
17
- $ bundle
19
+ ```console
20
+ bundle
21
+ ```
18
22
 
19
23
  Or install it yourself as:
20
24
 
21
- $ gem install slack-api-wrapper
25
+ ```console
26
+ gem install 'slack-api-wrapper
27
+ ```
22
28
 
23
29
  ## Usage
24
30
 
25
- TODO: Write usage instructions here
31
+ You need to get an api token to use the ruby client.
32
+ You can follow Slack documentation about it (https://api.slack.com/docs/oauth),
33
+ or use [omniauth-slack](https://github.com/kmrshntr/omniauth-slack) to get one.
34
+
35
+ ```ruby
36
+ # Initialize client
37
+ client = Slack::Client.new api_token
38
+
39
+ # Test user token
40
+ response = client.auth_test
41
+ puts "id: #{response['user_id']}, name: #{response['user']}"
42
+
43
+ # Get user teammates
44
+ response = client.users_list
45
+ response['members'].each do |user|
46
+ puts "id: #{user['id']}, name: #{user['name']}"
47
+ end
48
+
49
+ # Get user channels
50
+ response = client.channels_list
51
+ response['channels'].each do |channel|
52
+ puts "id: #{channel['id']}, name: #{channel['name']}"
53
+ end
26
54
 
27
- You can see a demo here [slack-web-test](https://github.com/gssbzn/slack-web-test)
55
+ # Get user private groups
56
+ response = client.groups_list
57
+ response['groups'].each do |group|
58
+ puts "id: #{group['id']}, name: #{group['name']}"
59
+ end
60
+
61
+ # Posting a message
62
+ response = client.chat_post_message('channel'=> 'CXXXX', 'text'=> 'Testing Slack API', 'as_user'=> true)
63
+ ```
64
+
65
+ ## Additional Resources
66
+
67
+ Slack API Docs: https://api.slack.com/
28
68
 
29
69
  ## Contributing
30
70
 
@@ -34,7 +74,6 @@ You can see a demo here [slack-web-test](https://github.com/gssbzn/slack-web-tes
34
74
  4. Push to the branch (`git push origin my-new-feature`)
35
75
  5. Create a new Pull Request
36
76
 
37
-
38
77
  ## License
39
78
 
40
79
  MIT License. Copyright 2015 Gustavo Bazan. http://gustavobazan.com
data/Rakefile CHANGED
@@ -1,7 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
7
-
6
+ task default: :spec
@@ -2,11 +2,10 @@ require 'net/https'
2
2
  require 'cgi'
3
3
  require 'json'
4
4
 
5
- require_relative "slack/version"
6
- require_relative "slack/error"
7
- require_relative "slack/session"
8
- require_relative "slack/oauth2"
9
- require_relative "slack/client"
5
+ require_relative 'slack/version'
6
+ require_relative 'slack/error'
7
+ require_relative 'slack/session'
8
+ require_relative 'slack/client'
10
9
 
11
10
  # Copyright (c) 2015 Gustavo Bazan
12
11
  # MIT License
@@ -15,104 +14,28 @@ require_relative "slack/client"
15
14
  #
16
15
  # * {Slack::Client} is the class used to interact with the slack end points.
17
16
  # * {Slack::Error} is raised when Slack encounters an error.
18
- # * {Slack::Oauth2} handles oauth2 authentication.
19
17
  #
20
18
  module Slack
21
19
  # Slack url
22
20
  WEB_SERVER = 'slack.com'
23
- # Slack api path
24
- API_SERVER = 'slack.com/api'
25
21
 
26
- # Removes nil params
22
+ # Parse response. You probably shouldn't be calling this directly.
23
+ # This takes responses from the server and parses them.
24
+ # It also checks for errors and raises exceptions with the appropriate messages.
27
25
  #
28
- # @param [Hash] params
29
- # API call arguments
30
- # @return [Hash]
31
- def self.clean_params(params)
32
- r = {}
33
- params.each do |k,v|
34
- r[k] = v.to_s unless v.nil?
35
- end
36
- r
37
- end
38
-
39
- # Convert params to query string
40
- #
41
- # @param [Hash] params
42
- # API call arguments
43
- # @return [String]
44
- def self.make_query_string(params)
45
- clean_params(params).collect {|k,v|
46
- CGI.escape(k) + "=" + CGI.escape(v)
47
- }.join("&")
48
- end
49
-
50
- # Handle http requests
51
- # @param [URI::HTTPS] uri
52
- # API uri
53
- # @param [Object] request
54
- # request object
55
- # @return [Net::HTTPResponse]
56
- def self.do_http(uri, request)
57
-
58
- http = Net::HTTP.new(uri.host, uri.port)
59
-
60
- http.use_ssl = true
61
-
62
- # Let then know about us
63
- request['User-Agent'] = "SlackRubyAPIWrapper"
64
-
65
- begin
66
- http.request(request)
67
- rescue OpenSSL::SSL::SSLError => e
68
- raise SlackError.new("SSL error connecting to Slack.")
69
- end
70
- end
71
-
72
- # Parse response. You probably shouldn't be calling this directly. This takes responses from the server
73
- # and parses them. It also checks for errors and raises exceptions with the appropriate messages.
74
26
  # @param [Net::HTTPResponse] response
75
27
  # @param [Boolean] raw if return raw data
76
28
  # @raise [SlackError]
77
29
  # @raise [SlackAuthError]
78
- def self.parse_response(response, raw=false)
79
- if response.kind_of?(Net::HTTPServerError)
80
- raise SlackError.new("Slack Server Error: #{response} - #{response.body}", response)
81
- elsif response.kind_of?(Net::HTTPUnauthorized)
82
- raise SlackAuthError.new("User is not authenticated.", response)
83
- elsif !response.kind_of?(Net::HTTPSuccess)
84
- begin
85
- d = JSON.parse(response.body)
86
- rescue
87
- raise SlackError.new("Slack Server Error: body=#{response.body}", response)
88
- end
89
- if d['error']
90
- raise SlackError.new(d['error'], response)
91
- else
92
- raise SlackError.new(response.body, response)
93
- end
30
+ def self.parse_response(response, raw = false)
31
+ if response.is_a?(Net::HTTPServerError)
32
+ fail Slack::Error.new("Slack Server Error: #{response} - #{response.body}", response)
94
33
  end
95
-
96
34
  return response.body if raw
97
-
98
- begin
99
- return JSON.parse(response.body)
100
- rescue JSON::ParserError
101
- raise SlackError.new("Unable to parse JSON response: #{response.body}", response)
102
- end
103
- end
104
-
105
- # A string comparison function that is resistant to timing attacks. If you're comparing a
106
- # string you got from the outside world with a string that is supposed to be a secret, use
107
- # this function to check equality.
108
- # @param [String] a
109
- # @param [String] b
110
- # @return [Boolean] whether the strings are equal
111
- def self.safe_string_equals(a, b)
112
- if a.length != b.length
113
- false
114
- else
115
- a.chars.zip(b.chars).map {|ac,bc| ac == bc}.all?
116
- end
35
+ d = JSON.parse(response.body)
36
+ fail Slack::Error.new(d['error'], response) if d['error']
37
+ return d
38
+ rescue JSON::ParserError
39
+ raise Slack::Error.new("Unable to parse JSON response: #{response.body}", response)
117
40
  end
118
41
  end
data/lib/slack/client.rb CHANGED
@@ -5,18 +5,17 @@ require_relative 'session'
5
5
  require_relative 'web'
6
6
 
7
7
  module Slack
8
- # Use this class to make Slack API calls. You'll need to obtain an OAuth 2 access token
9
- # first; you can get one using {Slack::OAuth2::Flow}.
8
+ # Use this class to make Slack API calls.
9
+ # You'll need to obtain an OAuth 2 access token
10
10
  class Client
11
11
  include Web
12
12
 
13
13
  # @param [String] oauth2_access_token user token
14
14
  def initialize(oauth2_access_token)
15
- if oauth2_access_token.is_a?(String)
16
- @session = Session.new(oauth2_access_token)
17
- else
18
- raise ArgumentError.new("oauth2_access_token doesn't have a valid type")
15
+ unless oauth2_access_token.is_a?(String)
16
+ fail ArgumentError, 'bad type for oauth2_access_token (expecting String)'
19
17
  end
18
+ @session = Session.new(oauth2_access_token)
20
19
  end
21
20
  end
22
21
  end
data/lib/slack/error.rb CHANGED
@@ -8,10 +8,9 @@ module Slack
8
8
  # @!attribute error
9
9
  # @return [String] the name of the error
10
10
  class Error < RuntimeError
11
-
12
11
  attr_accessor :http_response, :error
13
12
 
14
- def initialize(error, http_response=nil)
13
+ def initialize(error, http_response = nil)
15
14
  @error = error
16
15
  @http_response = http_response
17
16
  end
@@ -22,11 +21,4 @@ module Slack
22
21
  "#{error}"
23
22
  end
24
23
  end
25
-
26
- # This is the error raised on Authentication failures. Usually this means
27
- # one of three things
28
- # * Your user failed to go to the authorize url and approve your application
29
- # * You set an invalid or expired token and secret on your Session
30
- # * Your user deauthorized the application after you stored a valid token and secret
31
- class AuthError < Error; end
32
24
  end
@@ -0,0 +1,56 @@
1
+ module Slack
2
+ # Methods to do http requests
3
+ module Request
4
+ # Convert params to query string
5
+ #
6
+ # @param [Hash] params
7
+ # API call arguments
8
+ # @return [String]
9
+ def make_query_string(params)
10
+ clean_params(params).collect do |k, v|
11
+ CGI.escape(k) + '=' + CGI.escape(v)
12
+ end.join('&')
13
+ end
14
+
15
+ # Handle http requests
16
+ # @param [URI::HTTPS] uri
17
+ # API uri
18
+ # @param [Object] request
19
+ # request object
20
+ # @return [Net::HTTPResponse]
21
+ def do_http(uri, request)
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ http.use_ssl = true
24
+ # Let then know about us
25
+ request['User-Agent'] = 'SlackRubyAPIWrapper'
26
+ begin
27
+ http.request(request)
28
+ rescue OpenSSL::SSL::SSLError => e
29
+ raise Slack::Error, 'SSL error connecting to Slack.'
30
+ end
31
+ end
32
+
33
+ def do_http_with_body(uri, request, body) # :nodoc:
34
+ unless body.nil?
35
+ if body.is_a?(Hash)
36
+ request.set_form_data clean_params(body)
37
+ else
38
+ s = body.to_s
39
+ request['Content-Length'] = s.length
40
+ request.body = s
41
+ end
42
+ end
43
+ do_http(uri, request)
44
+ end
45
+
46
+ private
47
+
48
+ def clean_params(params) # :nodoc:
49
+ r = {}
50
+ params.each do |k, v|
51
+ r[k] = v.to_s unless v.nil?
52
+ end
53
+ r
54
+ end
55
+ end
56
+ end
data/lib/slack/session.rb CHANGED
@@ -1,81 +1,46 @@
1
1
  # Copyright (c) 2015 Gustavo Bazan
2
2
  # MIT License
3
+ require_relative 'request'
3
4
 
4
5
  module Slack
5
-
6
6
  class Session # :nodoc:
7
+ include Request
7
8
 
8
9
  # @param [String] oauth2_access_token user token
9
10
  def initialize(oauth2_access_token)
10
11
  unless oauth2_access_token.is_a?(String)
11
- raise "bad type for oauth2_access_token (expecting String)"
12
+ fail ArgumentError, 'bad type for oauth2_access_token (expecting String)'
12
13
  end
13
14
  @access_token = oauth2_access_token
14
15
  end
15
16
 
16
- private
17
-
18
- def build_url(path) # :nodoc:
19
- host = Slack::WEB_SERVER
20
- full_path = "/api/#{path}"
21
- URI::HTTPS.build({host: host, path: full_path})
22
- end
23
-
24
- def build_url_with_params(path, params) # :nodoc:
25
- target = build_url(path)
26
- target.query = Slack::make_query_string(params)
27
- target
28
- end
29
-
30
- protected
31
-
32
- def do_http(uri, request) # :nodoc:
33
- Slack::do_http(uri, request)
34
- end
35
-
36
- public
37
-
38
- def do_get(path, params=nil, headers=nil) # :nodoc:
17
+ def do_get(path, params = nil) # :nodoc:
39
18
  params ||= {}
40
- params["token"] = @access_token
19
+ params['token'] = @access_token
41
20
  uri = build_url_with_params(path, params)
42
21
  do_http(uri, Net::HTTP::Get.new(uri.request_uri))
43
22
  end
44
23
 
45
- def do_http_with_body(uri, request, body) # :nodoc:
46
- if body != nil
47
- if body.is_a?(Hash)
48
- request.set_form_data(Slack::clean_params(body))
49
- elsif body.respond_to?(:read)
50
- if body.respond_to?(:length)
51
- request["Content-Length"] = body.length.to_s
52
- elsif body.respond_to?(:stat) && body.stat.respond_to?(:size)
53
- request["Content-Length"] = body.stat.size.to_s
54
- else
55
- raise ArgumentError, "Don't know how to handle 'body' (responds to 'read' but not to 'length' or 'stat.size')."
56
- end
57
- request.body_stream = body
58
- else
59
- s = body.to_s
60
- request["Content-Length"] = s.length
61
- request.body = s
62
- end
63
- end
64
- do_http(uri, request)
65
- end
66
-
67
- def do_post(path, params=nil, headers=nil) # :nodoc:
24
+ def do_post(path, params = nil, headers = nil) # :nodoc:
25
+ headers ||= {}
26
+ headers['Authorization'] = "Bearer #{@access_token}"
68
27
  params ||= {}
69
- params["token"] = @access_token
28
+ params['token'] = @access_token
70
29
  uri = build_url(path)
71
30
  do_http_with_body(uri, Net::HTTP::Post.new(uri.request_uri, headers), params)
72
31
  end
73
32
 
74
- def do_put(path, params=nil, headers=nil, body=nil) # :nodoc:
75
- params ||= {}
76
- params["token"] = @access_token
77
- uri = build_url_with_params(path, params)
78
- do_http_with_body(uri, Net::HTTP::Put.new(uri.request_uri, headers), body)
33
+ private
34
+
35
+ def build_url(path) # :nodoc:
36
+ full_path = "/api/#{path}"
37
+ URI::HTTPS.build(host: Slack::WEB_SERVER, path: full_path)
38
+ end
39
+
40
+ def build_url_with_params(path, params) # :nodoc:
41
+ target = build_url(path)
42
+ target.query = make_query_string(params)
43
+ target
79
44
  end
80
45
  end
81
46
  end