slack.rb 0.0.3 → 0.0.4

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: e6828b51e4fa4cc0e3d4be1a86e0ce62e4c33e7d
4
- data.tar.gz: f52cd2d62c1ef973fc1b95d369fc0b8dc5814e2d
3
+ metadata.gz: ad9860bb84883992baebd1ab9b5ebafcc03964db
4
+ data.tar.gz: ae0b5bdff9a86994253721fc96285559b2c26ed4
5
5
  SHA512:
6
- metadata.gz: 6921716b6f042892736ac5bfddf3ee910e43688fe38ffc7c9c0d2454769e18aec88ac31c0c8da6c6e9c5020a7db498eeabd4f343ebf5df00b41b338b1de64efb
7
- data.tar.gz: 69c7b8984626d14246602794529ce23a48728da3443b1f32c446eae9f4a44a6d00ca64710241fd8d2f33081c03e88e7beee85f431a0d271579c950f7d32cc919
6
+ metadata.gz: a54b6d2120b7a5d5cea8d0f514070d7f0bcda377e1f445baae3041800556780a2c680f877a3145e9672d292c4e736c35ed4f25663fec3198090450b0c5a875cd
7
+ data.tar.gz: 41ce86e35e2d96720f7c2323c88296429ac66c18e24ab5213b34cf2eb3dcd37440c2f18ae527c259b3be3b15f47a90e706122292db0a47046f122242a96d596d
data/Gemfile CHANGED
@@ -14,7 +14,7 @@ group :test do
14
14
  gem 'rspec', '~> 3.0.0.beta2'
15
15
  gem 'simplecov', :require => false
16
16
  gem 'vcr', '~> 2.4.0'
17
- gem 'webmock', '>= 1.9.3'
17
+ gem 'webmock', '~> 1.9.3'
18
18
  end
19
19
 
20
20
  gemspec
@@ -1,8 +1,47 @@
1
1
  require "slack/version"
2
2
 
3
3
  module Slack
4
+ require 'slack/response/raise_error'
4
5
  require "slack/error"
5
6
  require "slack/connection"
6
7
  require "slack/payload"
7
8
  require "slack/client"
9
+
10
+ # Default API endpoint
11
+ API_ENDPOINT = "https://slack.com/api/".freeze
12
+
13
+ # Default User Agent header string
14
+ USER_AGENT = "Slack Ruby Gem #{Slack::VERSION}".freeze
15
+
16
+ # Default media type
17
+ MEDIA_TYPE = "application/json"
18
+
19
+ # Default API endpoint from ENV or {API_ENDPOINT}
20
+ # @return [String]
21
+ def api_endpoint
22
+ ENV['SLACK_API_ENDPOINT'] || API_ENDPOINT
23
+ end
24
+
25
+ # Default media type from ENV or {MEDIA_TYPE}
26
+ # @return [String]
27
+ def default_media_type
28
+ ENV['SLACK_DEFAULT_MEDIA_TYPE'] || MEDIA_TYPE
29
+ end
30
+
31
+ # Default User-Agent header string from ENV or {USER_AGENT}
32
+ # @return [String]
33
+ def user_agent
34
+ ENV['SLACK_USER_AGENT'] || USER_AGENT
35
+ end
36
+
37
+ # Default options for Faraday::Connection
38
+ # @return [Hash]
39
+ def connection_options
40
+ {
41
+ :headers => {
42
+ :accept => default_media_type,
43
+ :user_agent => user_agent
44
+ }
45
+ }
46
+ end
8
47
  end
@@ -28,18 +28,14 @@ module Slack
28
28
  def connection
29
29
  Faraday.new(base_url) do |c|
30
30
  c.use(Faraday::Request::UrlEncoded)
31
+ c.use(Slack::Response::RaiseError)
31
32
  c.adapter(Faraday.default_adapter)
32
33
  end
33
34
  end
34
35
 
35
36
  def handle_response(response)
36
37
  body = JSON.parse(response.body)
37
-
38
- if ["true", 1].include? body['ok']
39
- true
40
- else
41
- raise Slack::Error.new(response.body)
42
- end
38
+ true if ["true", 1].include? body['ok']
43
39
  end
44
40
  end
45
41
  end
@@ -1,4 +1,54 @@
1
1
  module Slack
2
- class Error < StandardError ; end
3
- class UnknownResponseCode < StandardError; end
2
+ class Error < StandardError
3
+
4
+ # Returns the appropriate Slack::Error sublcass based
5
+ # on error message
6
+ #
7
+ # @param [Hash] response HTTP response
8
+ # @return [Slack::Error]
9
+ def self.from_response(response)
10
+ status = response[:status].to_i
11
+ headers = response[:response_headers]
12
+ body = JSON.parse(response.body)
13
+ ok = ["true", 1].include? body['ok']
14
+ error = body['error']
15
+
16
+ unless ok
17
+ if klass = case error
18
+ when "invalid_auth" then Slack::InvalidAuth
19
+ when "account_inactive" then Slack::AccountInactive
20
+ when "channel_not_found" then Slack::ChannelNotFound
21
+ when "is_archived" then Slack::IsArchived
22
+ end
23
+ klass.new(response)
24
+ end
25
+ end
26
+ end
27
+
28
+ def initialize(response=nil)
29
+ @response = response
30
+ super(build_error_message)
31
+ end
32
+
33
+ def build_error_message
34
+ return nil if @response.nil?
35
+
36
+ message = "#{@response['method'].to_s.upcase} "
37
+ message << "#{@response['url'].to_s}: "
38
+ message << "#{@response['status']} - "
39
+ message
40
+ end
41
+ end
42
+
43
+ # Raised when a invalid authentication token was given
44
+ class InvalidAuth < Error; end
45
+
46
+ # Raised when authentication token is for a deleted user or team
47
+ class AccountInactive < Error; end
48
+
49
+ # Raised when value passed for channel was invalid
50
+ class ChannelNotFound < Error; end
51
+
52
+ # Raised when Channel has been archived
53
+ class IsArchived < Error; end
4
54
  end
@@ -0,0 +1,19 @@
1
+ require 'faraday'
2
+ require 'slack/error'
3
+
4
+ module Slack
5
+ # Faraday response middleware
6
+ module Response
7
+
8
+ class RaiseError < Faraday::Response::Middleware
9
+
10
+ private
11
+
12
+ def on_complete(response)
13
+ if error = Slack::Error.from_response(response)
14
+ raise error
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Slack
2
- VERSION = "0.0.3".freeze
2
+ VERSION = "0.0.4".freeze
3
3
  end
@@ -38,7 +38,7 @@ describe Slack::Client do
38
38
  it "returns channel not found error" do
39
39
  expect{
40
40
  @client.post_message("May the force be with you", "channel-name")
41
- }.to raise_error(Slack::Error)
41
+ }.to raise_error(Slack::ChannelNotFound)
42
42
 
43
43
  params = {text: "May the force be with you", channel: "#channel-name", token: test_slack_token}
44
44
  assert_requested :post, slack_url_with_params("/chat.postMessage", params)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bastian Bartmann
@@ -58,6 +58,7 @@ files:
58
58
  - lib/slack/connection.rb
59
59
  - lib/slack/error.rb
60
60
  - lib/slack/payload.rb
61
+ - lib/slack/response/raise_error.rb
61
62
  - lib/slack/version.rb
62
63
  - script/bootstrap
63
64
  - script/cibuild