github_api 0.14.2 → 0.14.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c509b47b9406a28c186ff6f1a0f9634b20a4ce35
4
- data.tar.gz: c4a47665d1ab2f0170afd089cbf2b6e4612eb18e
3
+ metadata.gz: 7fe5afce457fa73fed135b79c28fc1083e1d0f9d
4
+ data.tar.gz: fc77be5cbda3f746328797d7e1b1e9de5c5fdafb
5
5
  SHA512:
6
- metadata.gz: 5e20bca947fe37fc506743485b515e35dbcb7e6fddce356492549729055da1420a9a8f538f55bced926eed2d0bb60a0575e47f029eb974fbcd0903d5fb717768
7
- data.tar.gz: 3cf563db4fe0944d9768ebb063e12b3b18d64797cc687e0fe53bf5c58b24cc70ed477db2840e3ca6968e936ce7211f6955f1f38967d224feb35f374a7ca1ab5f
6
+ metadata.gz: 6c37cb56444c0a0a02546eb7960bbfdfb18308afad58a31f32da34fdf103e99cb25a7de3dc127066dd199d45e52e7703b0fd2e1b56162d40b341d53bd7e22a22
7
+ data.tar.gz: 9f7a7b7daabd4ff6094e2cc222522f8ad19c3403f024c071deb7880f86fb171b5ea86b5f21e73f2d47c244f949a38e29de349e41b945a26335613f313ee70875
@@ -19,5 +19,69 @@ module Github #:nodoc
19
19
  "\nResolution:\n #{@resolution}"
20
20
  end
21
21
  end
22
+
23
+ # Raised when invalid options are passed to a request body
24
+ class InvalidOptions < ClientError
25
+ def initialize(invalid, valid)
26
+ super(
27
+ generate_message(
28
+ problem: "Invalid option #{invalid.keys.join(', ')} provided for this request.",
29
+ summary: "Github gem checks the request parameters passed to ensure that github api is not hit unnecessairly and to fail fast.",
30
+ resolution: "Valid options are: #{valid.join(', ')}, make sure these are the ones you are using"
31
+ )
32
+ )
33
+ end
34
+ end
35
+
36
+ # Raised when invalid options are passed to a request body
37
+ class RequiredParams < ClientError
38
+ def initialize(provided, required)
39
+ super(
40
+ generate_message(
41
+ problem: "Missing required parameters: #{provided.keys.join(', ')} provided for this request.",
42
+ summary: "Github gem checks the request parameters passed to ensure that github api is not hit unnecessairly and to fail fast.",
43
+ resolution: "Required parameters are: #{required.join(', ')}, make sure these are the ones you are using"
44
+ )
45
+ )
46
+ end
47
+ end
48
+
49
+ # Raised when invalid options are passed to a request body
50
+ class UnknownMedia < ClientError
51
+ def initialize(file)
52
+ super(
53
+ generate_message(
54
+ problem: "Unknown content type for: '#{file}' provided for this request.",
55
+ summary: "Github gem infers the content type of the resource by calling the mime-types gem type inference.",
56
+ resolution: "Please install mime-types gem to infer the resource content type."
57
+ )
58
+ )
59
+ end
60
+ end
61
+
62
+ # Raised when invalid options are passed to a request body
63
+ class UnknownValue < ClientError
64
+ def initialize(key, value, permitted)
65
+ super(
66
+ generate_message(
67
+ problem: "Wrong value of '#{value}' for the parameter: #{key} provided for this request.",
68
+ summary: "Github gem checks the request parameters passed to ensure that github api is not hit unnecessairly and fails fast.",
69
+ resolution: "Permitted values are: #{permitted}, make sure these are the ones you are using"
70
+ )
71
+ )
72
+ end
73
+ end
74
+
75
+ class Validations < ClientError
76
+ def initialize(errors)
77
+ super(
78
+ generate_message(
79
+ problem: "Attempted to send request with nil arguments for #{errors.keys.join(', ')}.",
80
+ summary: 'Each request expects certain number of required arguments.',
81
+ resolution: 'Double check that the provided arguments are set to some value that is neither nil nor empty string.'
82
+ )
83
+ )
84
+ end
85
+ end
22
86
  end # Error
23
87
  end # Github
@@ -6,56 +6,183 @@ module Github
6
6
  # Raised when GitHub returns any of the HTTP status codes
7
7
  module Error
8
8
  class ServiceError < GithubError
9
- attr_reader :http_headers, :body, :status
9
+ # Add http status code method to error type
10
+ #
11
+ # @param [Integer] code
12
+ # the status code
13
+ #
14
+ # @api public
15
+ def self.http_status_code(code)
16
+ define_method(:http_status_code) { code }
17
+ end
18
+
19
+ # A mapping of status codes and error types
20
+ #
21
+ # @return [Hash[Integer, Object]]
22
+ #
23
+ # @api public
24
+ def self.errors
25
+ @errors ||= Hash[
26
+ descendants.map { |klass| [klass.new({}).http_status_code, klass] }
27
+ ]
28
+ end
10
29
 
11
30
  MIN_BODY_LENGTH = 2
12
31
 
32
+ # Crate a ServiceError
33
+ #
34
+ # @param [Hash[Symbol]] response
35
+ #
36
+ # @api public
13
37
  def initialize(response)
14
- @http_headers = response[:response_headers]
15
- @body = parse_body(response[:body])
16
- @status = response[:status]
38
+ @headers = response[:response_headers]
39
+ @body = response[:body]
40
+ @status = response[:status]
17
41
 
18
42
  super(create_message(response))
19
43
  end
20
44
 
45
+ private
46
+
47
+ # Create full error message
48
+ #
49
+ # @param [Hash[Symbol]] response
50
+ # the http response
51
+ #
52
+ # @return [String]
53
+ # the error message
54
+ #
55
+ # @api private
21
56
  def create_message(response)
22
- "#{response[:method].to_s.upcase} #{response[:url]}: #{@status} #{@body}"
57
+ return if response.nil?
58
+
59
+ message = "#{response[:method].to_s.upcase} "
60
+ message << "#{response[:url]}: "
61
+ message << "#{@status} - #{parse_body(@body)}"
62
+ message
23
63
  end
24
64
 
25
- def decode_body(body)
26
- if body.respond_to?(:to_str) && body.length >= MIN_BODY_LENGTH
65
+ # Decode body information if in JSON format
66
+ #
67
+ # @param [String] body
68
+ # the response body
69
+ #
70
+ # @api private
71
+ def decode_data(body)
72
+ if body.respond_to?(:to_str) &&
73
+ body.length >= MIN_BODY_LENGTH &&
74
+ @headers[:content_type] =~ /json/
75
+
27
76
  JSON.parse(body, symbolize_names: true)
28
77
  else
29
78
  body
30
79
  end
31
80
  end
32
81
 
82
+ # @api private
33
83
  def parse_body(body)
34
- body = decode_body(body)
84
+ data = decode_data(body)
35
85
 
36
- return '' if body.nil? || body.empty?
86
+ return '' if data.nil? || data.empty?
37
87
 
38
- if body[:error]
39
- body[:error]
40
- elsif body[:errors]
41
- error = Array(body[:errors]).first
42
- error.is_a?(Hash) ? error[:message] : error
43
- elsif body[:message]
44
- body[:message]
45
- else
46
- ''
88
+ case data
89
+ when Hash
90
+ message = data[:message] ? data[:message] : ' '
91
+ docs = data[:documentation_url]
92
+ error = create_error_summary(data)
93
+ message << error if error
94
+ message << "\nSee: #{docs}" if docs
95
+ message
96
+ when String
97
+ data
47
98
  end
48
99
  end
49
100
 
50
- def self.http_status_code(code)
51
- define_method(:http_status_code) { code }
52
- end
53
-
54
- def self.errors
55
- @errors ||= Hash[
56
- descendants.map { |klass| [klass.new({}).http_status_code, klass] }
57
- ]
101
+ # Create error summary from response body
102
+ #
103
+ # @return [String]
104
+ #
105
+ # @api private
106
+ def create_error_summary(data)
107
+ if data[:error]
108
+ return "\nError: #{data[:error]}"
109
+ elsif data[:errors]
110
+ message = "\nErrors:\n"
111
+ message << data[:errors].map do |error|
112
+ "Error: #{error.map { |k, v| "#{k}: #{v}" }.join(', ')}"
113
+ end.join("\n")
114
+ end
58
115
  end
59
116
  end # ServiceError
117
+
118
+ # Raised when Github returns the HTTP status code 400
119
+ class BadRequest < ServiceError
120
+ http_status_code 400
121
+ end
122
+
123
+ # Raised when GitHub returns the HTTP status code 401
124
+ class Unauthorized < ServiceError
125
+ http_status_code 401
126
+ end
127
+
128
+ # Raised when Github returns the HTTP status code 403
129
+ class Forbidden < ServiceError
130
+ http_status_code 403
131
+ end
132
+
133
+ # Raised when Github returns the HTTP status code 404
134
+ class NotFound < ServiceError
135
+ http_status_code 404
136
+ end
137
+
138
+ # Raised when Github returns the HTTP status code 405
139
+ class MethodNotAllowed < ServiceError
140
+ http_status_code 405
141
+ end
142
+
143
+ # Raised when Github returns the HTTP status code 406
144
+ class NotAcceptable < ServiceError
145
+ http_status_code 406
146
+ end
147
+
148
+ # Raised when GitHub returns the HTTP status code 409
149
+ class Conflict < ServiceError
150
+ http_status_code 409
151
+ end
152
+
153
+ # Raised when GitHub returns the HTTP status code 414
154
+ class UnsupportedMediaType < ServiceError
155
+ http_status_code 414
156
+ end
157
+
158
+ # Raised when GitHub returns the HTTP status code 422
159
+ class UnprocessableEntity < ServiceError
160
+ http_status_code 422
161
+ end
162
+
163
+ # Raised when GitHub returns the HTTP status code 451
164
+ class UnavailableForLegalReasons < ServiceError
165
+ http_status_code 451
166
+ end
167
+
168
+ # Raised when Github returns the HTTP status code 500
169
+ class InternalServerError < ServiceError
170
+ http_status_code 500
171
+ end
172
+
173
+ # Raised when Github returns the HTTP status code 501
174
+ class NotImplemented < ServiceError
175
+ http_status_code 501
176
+ end
177
+
178
+ # Raised when Github returns the HTTP status code 502
179
+ class BadGateway < ServiceError
180
+ http_status_code 502
181
+ end
182
+
183
+ # Raised when GitHub returns the HTTP status code 503
184
+ class ServiceUnavailable < ServiceError
185
+ http_status_code 503
186
+ end
60
187
  end # Error
61
188
  end # Github
@@ -23,15 +23,9 @@ module Github
23
23
  def backtrace
24
24
  @response_message ? @response_message.backtrace : super
25
25
  end
26
-
27
26
  end # GithubError
28
27
  end # Error
29
28
  end # Github
30
29
 
31
30
  require 'github_api/error/service_error'
32
31
  require 'github_api/error/client_error'
33
- Dir[File.dirname(__FILE__) + '/error/*.rb'].sort.each do |path|
34
- filename = File.basename(path)
35
- next if ['service_error.rb', 'client_error.rb'].include?(filename)
36
- require "github_api/error/#{filename}"
37
- end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Github
4
- VERSION = "0.14.2"
4
+ VERSION = "0.14.3"
5
5
  end # Github
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.2
4
+ version: 0.14.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-28 00:00:00.000000000 Z
11
+ date: 2016-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -62,16 +62,16 @@ dependencies:
62
62
  name: oauth2
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - ">="
65
+ - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '0'
67
+ version: 1.0.0
68
68
  type: :runtime
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - ">="
72
+ - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '0'
74
+ version: 1.0.0
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: descendants_tracker
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -204,21 +204,8 @@ files:
204
204
  - lib/github_api/core_ext/ordered_hash.rb
205
205
  - lib/github_api/deprecation.rb
206
206
  - lib/github_api/error.rb
207
- - lib/github_api/error/bad_request.rb
208
207
  - lib/github_api/error/client_error.rb
209
- - lib/github_api/error/forbidden.rb
210
- - lib/github_api/error/internal_server_error.rb
211
- - lib/github_api/error/invalid_options.rb
212
- - lib/github_api/error/not_acceptable.rb
213
- - lib/github_api/error/not_found.rb
214
- - lib/github_api/error/required_params.rb
215
208
  - lib/github_api/error/service_error.rb
216
- - lib/github_api/error/service_unavailable.rb
217
- - lib/github_api/error/unauthorized.rb
218
- - lib/github_api/error/unknown_media.rb
219
- - lib/github_api/error/unknown_value.rb
220
- - lib/github_api/error/unprocessable_entity.rb
221
- - lib/github_api/error/validations.rb
222
209
  - lib/github_api/ext/faraday.rb
223
210
  - lib/github_api/middleware.rb
224
211
  - lib/github_api/mime_type.rb
@@ -1,14 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github
4
- module Error
5
- # Raised when Github returns the HTTP status code 400
6
- class BadRequest < ServiceError
7
- http_status_code 400
8
-
9
- def initialize(response)
10
- super(response)
11
- end
12
- end
13
- end
14
- end # Github
@@ -1,14 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when Github returns the HTTP status code 403
5
- module Error
6
- class Forbidden < ServiceError
7
- http_status_code 403
8
-
9
- def initialize(response)
10
- super(response)
11
- end
12
- end
13
- end # Error
14
- end # Github
@@ -1,15 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when Github returns the HTTP status code 500
5
- module Error
6
- class InternalServerError < ServiceError
7
- http_status_code 500
8
-
9
- def initialize(response)
10
- super(response)
11
- end
12
-
13
- end # InternalServerError
14
- end # Error
15
- end # Github
@@ -1,18 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when invalid options are passed to a request body
5
- module Error
6
- class InvalidOptions < ClientError
7
- def initialize(invalid, valid)
8
- super(
9
- generate_message(
10
- :problem => "Invalid option #{invalid.keys.join(', ')} provided for this request.",
11
- :summary => "Github gem checks the request parameters passed to ensure that github api is not hit unnecessairly and to fail fast.",
12
- :resolution => "Valid options are: #{valid.join(', ')}, make sure these are the ones you are using"
13
- )
14
- )
15
- end
16
- end
17
- end # Error
18
- end # Github
@@ -1,15 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when GitHub returns the HTTP status code 406
5
- module Error
6
- class NotAcceptable < ServiceError
7
- http_status_code 406
8
-
9
- def initialize(response)
10
- super(response)
11
- end
12
-
13
- end # NotAcceptable
14
- end # Error
15
- end # Github
@@ -1,14 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when Github returns the HTTP status code 404
5
- module Error
6
- class NotFound < ServiceError
7
- http_status_code 404
8
-
9
- def initialize(env)
10
- super(env)
11
- end
12
- end
13
- end # Error
14
- end # Github
@@ -1,18 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when invalid options are passed to a request body
5
- module Error
6
- class RequiredParams < ClientError
7
- def initialize(provided, required)
8
- super(
9
- generate_message(
10
- :problem => "Missing required parameters: #{provided.keys.join(', ')} provided for this request.",
11
- :summary => "Github gem checks the request parameters passed to ensure that github api is not hit unnecessairly and to fail fast.",
12
- :resolution => "Required parameters are: #{required.join(', ')}, make sure these are the ones you are using"
13
- )
14
- )
15
- end
16
- end
17
- end # Error
18
- end # Github
@@ -1,15 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when Github returns the HTTP status code 503
5
- module Error
6
- class ServiceUnavailable < ServiceError
7
- http_status_code 503
8
-
9
- def initialize(response)
10
- super(response)
11
- end
12
-
13
- end # ServiceUnavailable
14
- end # Error
15
- end # Github
@@ -1,15 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when Github returns the HTTP status code 401
5
- module Error
6
- class Unauthorized < ServiceError
7
- http_status_code 401
8
-
9
- def initialize(response)
10
- super(response)
11
- end
12
-
13
- end # Unauthorized
14
- end # Error
15
- end # Github
@@ -1,18 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when invalid options are passed to a request body
5
- module Error
6
- class UnknownMedia < ClientError
7
- def initialize(file)
8
- super(
9
- generate_message(
10
- :problem => "Unknown content type for: '#{file}' provided for this request.",
11
- :summary => "Github gem infers the content type of the resource by calling the mime-types gem type inference.",
12
- :resolution => "Please install mime-types gem to infer the resource content type."
13
- )
14
- )
15
- end
16
- end
17
- end # Error
18
- end # Github
@@ -1,18 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when invalid options are passed to a request body
5
- module Error
6
- class UnknownValue < ClientError
7
- def initialize(key, value, permitted)
8
- super(
9
- generate_message(
10
- :problem => "Wrong value of '#{value}' for the parameter: #{key} provided for this request.",
11
- :summary => "Github gem checks the request parameters passed to ensure that github api is not hit unnecessairly and fails fast.",
12
- :resolution => "Permitted values are: #{permitted}, make sure these are the ones you are using"
13
- )
14
- )
15
- end
16
- end
17
- end # Error
18
- end # Github
@@ -1,14 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when Github returns the HTTP status code 422
5
- module Error
6
- class UnprocessableEntity < ServiceError
7
- http_status_code 422
8
-
9
- def initialize(response)
10
- super(response)
11
- end
12
- end
13
- end # Error
14
- end # Github
@@ -1,18 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Github #:nodoc
4
- # Raised when passed parameters are missing or contain wrong values.
5
- module Error
6
- class Validations < ClientError
7
- def initialize(errors)
8
- super(
9
- generate_message(
10
- :problem => "Attempted to send request with nil arguments for #{errors.keys.join(', ')}.",
11
- :summary => 'Each request expects certain number of required arguments.',
12
- :resolution => 'Double check that the provided arguments are set to some value that is neither nil nor empty string.'
13
- )
14
- )
15
- end
16
- end
17
- end # Error
18
- end # Github