signet 0.11.0 → 0.14.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +51 -15
  3. data/Gemfile +5 -4
  4. data/README.md +4 -6
  5. data/Rakefile +107 -37
  6. data/lib/signet.rb +17 -14
  7. data/lib/signet/errors.rb +4 -4
  8. data/lib/signet/oauth_1.rb +129 -154
  9. data/lib/signet/oauth_1/client.rb +309 -343
  10. data/lib/signet/oauth_1/credential.rb +40 -37
  11. data/lib/signet/oauth_1/server.rb +197 -203
  12. data/lib/signet/oauth_1/signature_methods/hmac_sha1.rb +11 -10
  13. data/lib/signet/oauth_1/signature_methods/plaintext.rb +8 -7
  14. data/lib/signet/oauth_1/signature_methods/rsa_sha1.rb +11 -11
  15. data/lib/signet/oauth_2.rb +41 -43
  16. data/lib/signet/oauth_2/client.rb +328 -313
  17. data/lib/signet/version.rb +2 -73
  18. data/signet.gemspec +37 -39
  19. data/spec/signet/oauth_1/client_spec.rb +313 -315
  20. data/spec/signet/oauth_1/credential_spec.rb +64 -56
  21. data/spec/signet/oauth_1/server_spec.rb +362 -362
  22. data/spec/signet/oauth_1/signature_methods/hmac_sha1_spec.rb +26 -26
  23. data/spec/signet/oauth_1/signature_methods/plaintext_spec.rb +28 -28
  24. data/spec/signet/oauth_1/signature_methods/rsa_sha1_spec.rb +34 -35
  25. data/spec/signet/oauth_1_spec.rb +553 -524
  26. data/spec/signet/oauth_2/client_spec.rb +652 -576
  27. data/spec/signet/oauth_2_spec.rb +88 -89
  28. data/spec/signet_spec.rb +41 -41
  29. data/spec/spec_helper.rb +7 -7
  30. data/spec/spec_helper_spec.rb +8 -8
  31. metadata +64 -52
  32. data/tasks/clobber.rake +0 -2
  33. data/tasks/gem.rake +0 -34
  34. data/tasks/git.rake +0 -40
  35. data/tasks/metrics.rake +0 -41
  36. data/tasks/spec.rake +0 -34
  37. data/tasks/wiki.rake +0 -38
  38. data/tasks/yard.rake +0 -21
@@ -12,15 +12,15 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'faraday'
16
- #require 'faraday/utils'
15
+ require "faraday"
16
+ # require 'faraday/utils'
17
17
 
18
- require 'stringio'
19
- require 'addressable/uri'
20
- require 'signet'
21
- require 'signet/errors'
22
- require 'signet/oauth_1'
23
- require 'signet/oauth_1/credential'
18
+ require "stringio"
19
+ require "addressable/uri"
20
+ require "signet"
21
+ require "signet/errors"
22
+ require "signet/oauth_1"
23
+ require "signet/oauth_1/credential"
24
24
 
25
25
  module Signet
26
26
  module OAuth1
@@ -53,8 +53,8 @@ module Signet
53
53
  # :client_credential_key => 'anonymous',
54
54
  # :client_credential_secret => 'anonymous'
55
55
  # )
56
- def initialize(options={})
57
- self.update!(options)
56
+ def initialize options = {}
57
+ update! options
58
58
  end
59
59
 
60
60
  ##
@@ -87,29 +87,29 @@ module Signet
87
87
  # )
88
88
  #
89
89
  # @see Signet::OAuth1::Client#initialize
90
- def update!(options={})
90
+ def update! options = {}
91
91
  # Normalize key to String to allow indifferent access.
92
- options = options.inject({}) { |accu, (k, v)| accu[k.to_s] = v; accu }
92
+ options = options.each_with_object({}) { |(k, v), accu| accu[k.to_s] = v; }
93
93
  self.temporary_credential_uri = options["temporary_credential_uri"]
94
94
  self.authorization_uri = options["authorization_uri"]
95
95
  self.token_credential_uri = options["token_credential_uri"]
96
96
  # Technically... this would allow you to pass in a :client key...
97
97
  # But that would be weird. Don't do that.
98
98
  self.client_credential_key =
99
- Signet::OAuth1.extract_credential_key_option("client", options)
99
+ Signet::OAuth1.extract_credential_key_option "client", options
100
100
  self.client_credential_secret =
101
- Signet::OAuth1.extract_credential_secret_option("client", options)
101
+ Signet::OAuth1.extract_credential_secret_option "client", options
102
102
  self.temporary_credential_key =
103
- Signet::OAuth1.extract_credential_key_option("temporary", options)
103
+ Signet::OAuth1.extract_credential_key_option "temporary", options
104
104
  self.temporary_credential_secret =
105
- Signet::OAuth1.extract_credential_secret_option("temporary", options)
105
+ Signet::OAuth1.extract_credential_secret_option "temporary", options
106
106
  self.token_credential_key =
107
- Signet::OAuth1.extract_credential_key_option("token", options)
107
+ Signet::OAuth1.extract_credential_key_option "token", options
108
108
  self.token_credential_secret =
109
- Signet::OAuth1.extract_credential_secret_option("token", options)
109
+ Signet::OAuth1.extract_credential_secret_option "token", options
110
110
  self.callback = options["callback"]
111
111
  self.two_legged = options["two_legged"] || false
112
- return self
112
+ self
113
113
  end
114
114
 
115
115
  ##
@@ -117,9 +117,9 @@ module Signet
117
117
  #
118
118
  # @return [Addressable::URI] The temporary credentials URI.
119
119
  def temporary_credential_uri
120
- return @temporary_credential_uri
120
+ @temporary_credential_uri
121
121
  end
122
- alias_method :request_token_uri, :temporary_credential_uri
122
+ alias request_token_uri temporary_credential_uri
123
123
 
124
124
  ##
125
125
  # Sets the temporary credentials URI for this client.
@@ -127,16 +127,16 @@ module Signet
127
127
  # @param [Addressable::URI, String, #to_str]
128
128
  # new_temporary_credential_uri
129
129
  # The temporary credentials URI.
130
- def temporary_credential_uri=(new_temporary_credential_uri)
131
- if new_temporary_credential_uri != nil
130
+ def temporary_credential_uri= new_temporary_credential_uri
131
+ if !new_temporary_credential_uri.nil?
132
132
  new_temporary_credential_uri =
133
- Addressable::URI.parse(new_temporary_credential_uri)
133
+ Addressable::URI.parse new_temporary_credential_uri
134
134
  @temporary_credential_uri = new_temporary_credential_uri
135
135
  else
136
136
  @temporary_credential_uri = nil
137
137
  end
138
138
  end
139
- alias_method :request_token_uri=, :temporary_credential_uri=
139
+ alias request_token_uri= temporary_credential_uri=
140
140
 
141
141
  ##
142
142
  # Returns the authorization URI that the user should be redirected to.
@@ -144,13 +144,13 @@ module Signet
144
144
  # @return [Addressable::URI] The authorization URI.
145
145
  #
146
146
  # @see Signet::OAuth1.generate_authorization_uri
147
- def authorization_uri(options={})
147
+ def authorization_uri options = {}
148
148
  options = options.merge(
149
- :temporary_credential_key => self.temporary_credential_key,
150
- :callback => self.callback
149
+ temporary_credential_key: temporary_credential_key,
150
+ callback: callback
151
151
  )
152
- return nil if @authorization_uri == nil
153
- return Addressable::URI.parse(
152
+ return nil if @authorization_uri.nil?
153
+ Addressable::URI.parse(
154
154
  ::Signet::OAuth1.generate_authorization_uri(
155
155
  @authorization_uri, options
156
156
  )
@@ -162,10 +162,10 @@ module Signet
162
162
  #
163
163
  # @param [Addressable::URI, String, #to_str] new_authorization_uri
164
164
  # The authorization URI.
165
- def authorization_uri=(new_authorization_uri)
166
- if new_authorization_uri != nil
165
+ def authorization_uri= new_authorization_uri
166
+ if !new_authorization_uri.nil?
167
167
  new_authorization_uri = Addressable::URI.send(
168
- new_authorization_uri.kind_of?(Hash) ? :new : :parse,
168
+ new_authorization_uri.is_a?(Hash) ? :new : :parse,
169
169
  new_authorization_uri
170
170
  )
171
171
  @authorization_uri = new_authorization_uri
@@ -179,19 +179,19 @@ module Signet
179
179
  #
180
180
  # @return [Addressable::URI] The token credential URI.
181
181
  def token_credential_uri
182
- return @token_credential_uri
182
+ @token_credential_uri
183
183
  end
184
- alias_method :access_token_uri, :token_credential_uri
184
+ alias access_token_uri token_credential_uri
185
185
 
186
186
  ##
187
187
  # Sets the token credential URI for this client.
188
188
  #
189
189
  # @param [Addressable::URI, Hash, String, #to_str] new_token_credential_uri
190
190
  # The token credential URI.
191
- def token_credential_uri=(new_token_credential_uri)
192
- if new_token_credential_uri != nil
191
+ def token_credential_uri= new_token_credential_uri
192
+ if !new_token_credential_uri.nil?
193
193
  new_token_credential_uri = Addressable::URI.send(
194
- new_token_credential_uri.kind_of?(Hash) ? :new : :parse,
194
+ new_token_credential_uri.is_a?(Hash) ? :new : :parse,
195
195
  new_token_credential_uri
196
196
  )
197
197
  @token_credential_uri = new_token_credential_uri
@@ -199,7 +199,7 @@ module Signet
199
199
  @token_credential_uri = nil
200
200
  end
201
201
  end
202
- alias_method :access_token_uri=, :token_credential_uri=
202
+ alias access_token_uri= token_credential_uri=
203
203
 
204
204
  # Lots of duplicated code here, but for the sake of auto-generating
205
205
  # documentation, we're going to let it slide. Oh well.
@@ -209,31 +209,31 @@ module Signet
209
209
  #
210
210
  # @return [Signet::OAuth1::Credential] The client credentials.
211
211
  def client_credential
212
- if self.client_credential_key && self.client_credential_secret
213
- return ::Signet::OAuth1::Credential.new(
214
- self.client_credential_key,
215
- self.client_credential_secret
212
+ if client_credential_key && client_credential_secret
213
+ ::Signet::OAuth1::Credential.new(
214
+ client_credential_key,
215
+ client_credential_secret
216
216
  )
217
- elsif !self.client_credential_key && !self.client_credential_secret
218
- return nil
217
+ elsif !client_credential_key && !client_credential_secret
218
+ nil
219
219
  else
220
220
  raise ArgumentError,
221
- "The client credential key and secret must be set."
221
+ "The client credential key and secret must be set."
222
222
  end
223
223
  end
224
- alias_method :consumer_token, :client_credential
224
+ alias consumer_token client_credential
225
225
 
226
226
  ##
227
227
  # Sets the client credential for this client.
228
228
  #
229
229
  # @param [Signet::OAuth1::Credential] new_client_credential
230
230
  # The client credentials.
231
- def client_credential=(new_client_credential)
232
- if new_client_credential != nil
233
- if !new_client_credential.kind_of?(::Signet::OAuth1::Credential)
231
+ def client_credential= new_client_credential
232
+ if !new_client_credential.nil?
233
+ unless new_client_credential.is_a? ::Signet::OAuth1::Credential
234
234
  raise TypeError,
235
- "Expected Signet::OAuth1::Credential, " +
236
- "got #{new_client_credential.class}."
235
+ "Expected Signet::OAuth1::Credential, " \
236
+ "got #{new_client_credential.class}."
237
237
  end
238
238
  @client_credential_key = new_client_credential.key
239
239
  @client_credential_secret = new_client_credential.secret
@@ -242,27 +242,27 @@ module Signet
242
242
  @client_credential_secret = nil
243
243
  end
244
244
  end
245
- alias_method :consumer_token=, :client_credential=
245
+ alias consumer_token= client_credential=
246
246
 
247
247
  ##
248
248
  # Returns the client credential key for this client.
249
249
  #
250
250
  # @return [String] The client credential key.
251
251
  def client_credential_key
252
- return @client_credential_key
252
+ @client_credential_key
253
253
  end
254
- alias_method :consumer_key, :client_credential_key
254
+ alias consumer_key client_credential_key
255
255
 
256
256
  ##
257
257
  # Sets the client credential key for this client.
258
258
  #
259
259
  # @param [String, #to_str] new_client_credential_key
260
260
  # The client credential key.
261
- def client_credential_key=(new_client_credential_key)
262
- if new_client_credential_key != nil
263
- if !new_client_credential_key.respond_to?(:to_str)
261
+ def client_credential_key= new_client_credential_key
262
+ if !new_client_credential_key.nil?
263
+ unless new_client_credential_key.respond_to? :to_str
264
264
  raise TypeError,
265
- "Can't convert #{new_client_credential_key.class} into String."
265
+ "Can't convert #{new_client_credential_key.class} into String."
266
266
  end
267
267
  new_client_credential_key = new_client_credential_key.to_str
268
268
  @client_credential_key = new_client_credential_key
@@ -270,28 +270,28 @@ module Signet
270
270
  @client_credential_key = nil
271
271
  end
272
272
  end
273
- alias_method :consumer_key=, :client_credential_key=
273
+ alias consumer_key= client_credential_key=
274
274
 
275
275
  ##
276
276
  # Returns the client credential secret for this client.
277
277
  #
278
278
  # @return [String] The client credential secret.
279
279
  def client_credential_secret
280
- return @client_credential_secret
280
+ @client_credential_secret
281
281
  end
282
- alias_method :consumer_secret, :client_credential_secret
282
+ alias consumer_secret client_credential_secret
283
283
 
284
284
  ##
285
285
  # Sets the client credential secret for this client.
286
286
  #
287
287
  # @param [String, #to_str] new_client_credential_secret
288
288
  # The client credential secret.
289
- def client_credential_secret=(new_client_credential_secret)
290
- if new_client_credential_secret != nil
291
- if !new_client_credential_secret.respond_to?(:to_str)
289
+ def client_credential_secret= new_client_credential_secret
290
+ if !new_client_credential_secret.nil?
291
+ unless new_client_credential_secret.respond_to? :to_str
292
292
  raise TypeError,
293
- "Can't convert #{new_client_credential_secret.class} " +
294
- "into String."
293
+ "Can't convert #{new_client_credential_secret.class} " \
294
+ "into String."
295
295
  end
296
296
  new_client_credential_secret = new_client_credential_secret.to_str
297
297
  @client_credential_secret = new_client_credential_secret
@@ -299,39 +299,39 @@ module Signet
299
299
  @client_credential_secret = nil
300
300
  end
301
301
  end
302
- alias_method :consumer_secret=, :client_credential_secret=
302
+ alias consumer_secret= client_credential_secret=
303
303
 
304
304
  ##
305
305
  # Returns the temporary credential for this client.
306
306
  #
307
307
  # @return [Signet::OAuth1::Credential] The temporary credentials.
308
308
  def temporary_credential
309
- if self.temporary_credential_key && self.temporary_credential_secret
310
- return ::Signet::OAuth1::Credential.new(
311
- self.temporary_credential_key,
312
- self.temporary_credential_secret
309
+ if temporary_credential_key && temporary_credential_secret
310
+ ::Signet::OAuth1::Credential.new(
311
+ temporary_credential_key,
312
+ temporary_credential_secret
313
313
  )
314
- elsif !self.temporary_credential_key &&
315
- !self.temporary_credential_secret
316
- return nil
314
+ elsif !temporary_credential_key &&
315
+ !temporary_credential_secret
316
+ nil
317
317
  else
318
318
  raise ArgumentError,
319
- "The temporary credential key and secret must be set."
319
+ "The temporary credential key and secret must be set."
320
320
  end
321
321
  end
322
- alias_method :request_token, :temporary_credential
322
+ alias request_token temporary_credential
323
323
 
324
324
  ##
325
325
  # Sets the temporary credential for this client.
326
326
  #
327
327
  # @param [Signet::OAuth1::Credential] new_temporary_credential
328
328
  # The temporary credentials.
329
- def temporary_credential=(new_temporary_credential)
330
- if new_temporary_credential != nil
331
- if !new_temporary_credential.kind_of?(::Signet::OAuth1::Credential)
329
+ def temporary_credential= new_temporary_credential
330
+ if !new_temporary_credential.nil?
331
+ unless new_temporary_credential.is_a? ::Signet::OAuth1::Credential
332
332
  raise TypeError,
333
- "Expected Signet::OAuth1::Credential, " +
334
- "got #{new_temporary_credential.class}."
333
+ "Expected Signet::OAuth1::Credential, " \
334
+ "got #{new_temporary_credential.class}."
335
335
  end
336
336
  @temporary_credential_key = new_temporary_credential.key
337
337
  @temporary_credential_secret = new_temporary_credential.secret
@@ -340,28 +340,28 @@ module Signet
340
340
  @temporary_credential_secret = nil
341
341
  end
342
342
  end
343
- alias_method :request_token=, :temporary_credential=
343
+ alias request_token= temporary_credential=
344
344
 
345
345
  ##
346
346
  # Returns the temporary credential key for this client.
347
347
  #
348
348
  # @return [String] The temporary credential key.
349
349
  def temporary_credential_key
350
- return @temporary_credential_key
350
+ @temporary_credential_key
351
351
  end
352
- alias_method :request_token_key, :temporary_credential_key
352
+ alias request_token_key temporary_credential_key
353
353
 
354
354
  ##
355
355
  # Sets the temporary credential key for this client.
356
356
  #
357
357
  # @param [String, #to_str] new_temporary_credential_key
358
358
  # The temporary credential key.
359
- def temporary_credential_key=(new_temporary_credential_key)
360
- if new_temporary_credential_key != nil
361
- if !new_temporary_credential_key.respond_to?(:to_str)
359
+ def temporary_credential_key= new_temporary_credential_key
360
+ if !new_temporary_credential_key.nil?
361
+ unless new_temporary_credential_key.respond_to? :to_str
362
362
  raise TypeError,
363
- "Can't convert #{new_temporary_credential_key.class} " +
364
- "into String."
363
+ "Can't convert #{new_temporary_credential_key.class} " \
364
+ "into String."
365
365
  end
366
366
  new_temporary_credential_key = new_temporary_credential_key.to_str
367
367
  @temporary_credential_key = new_temporary_credential_key
@@ -369,28 +369,28 @@ module Signet
369
369
  @temporary_credential_key = nil
370
370
  end
371
371
  end
372
- alias_method :request_token_key=, :temporary_credential_key=
372
+ alias request_token_key= temporary_credential_key=
373
373
 
374
374
  ##
375
375
  # Returns the temporary credential secret for this client.
376
376
  #
377
377
  # @return [String] The temporary credential secret.
378
378
  def temporary_credential_secret
379
- return @temporary_credential_secret
379
+ @temporary_credential_secret
380
380
  end
381
- alias_method :request_token_secret, :temporary_credential_secret
381
+ alias request_token_secret temporary_credential_secret
382
382
 
383
383
  ##
384
384
  # Sets the temporary credential secret for this client.
385
385
  #
386
386
  # @param [String, #to_str] new_temporary_credential_secret
387
387
  # The temporary credential secret.
388
- def temporary_credential_secret=(new_temporary_credential_secret)
389
- if new_temporary_credential_secret != nil
390
- if !new_temporary_credential_secret.respond_to?(:to_str)
388
+ def temporary_credential_secret= new_temporary_credential_secret
389
+ if !new_temporary_credential_secret.nil?
390
+ unless new_temporary_credential_secret.respond_to? :to_str
391
391
  raise TypeError,
392
- "Can't convert #{new_temporary_credential_secret.class} " +
393
- "into String."
392
+ "Can't convert #{new_temporary_credential_secret.class} " \
393
+ "into String."
394
394
  end
395
395
  new_temporary_credential_secret =
396
396
  new_temporary_credential_secret.to_str
@@ -399,39 +399,39 @@ module Signet
399
399
  @temporary_credential_secret = nil
400
400
  end
401
401
  end
402
- alias_method :request_token_secret=, :temporary_credential_secret=
402
+ alias request_token_secret= temporary_credential_secret=
403
403
 
404
404
  ##
405
405
  # Returns the token credential for this client.
406
406
  #
407
407
  # @return [Signet::OAuth1::Credential] The token credentials.
408
408
  def token_credential
409
- if self.token_credential_key && self.token_credential_secret
410
- return ::Signet::OAuth1::Credential.new(
411
- self.token_credential_key,
412
- self.token_credential_secret
409
+ if token_credential_key && token_credential_secret
410
+ ::Signet::OAuth1::Credential.new(
411
+ token_credential_key,
412
+ token_credential_secret
413
413
  )
414
- elsif !self.token_credential_key &&
415
- !self.token_credential_secret
416
- return nil
414
+ elsif !token_credential_key &&
415
+ !token_credential_secret
416
+ nil
417
417
  else
418
418
  raise ArgumentError,
419
- "The token credential key and secret must be set."
419
+ "The token credential key and secret must be set."
420
420
  end
421
421
  end
422
- alias_method :access_token, :token_credential
422
+ alias access_token token_credential
423
423
 
424
424
  ##
425
425
  # Sets the token credential for this client.
426
426
  #
427
427
  # @param [Signet::OAuth1::Credential] new_token_credential
428
428
  # The token credentials.
429
- def token_credential=(new_token_credential)
430
- if new_token_credential != nil
431
- if !new_token_credential.kind_of?(::Signet::OAuth1::Credential)
429
+ def token_credential= new_token_credential
430
+ if !new_token_credential.nil?
431
+ unless new_token_credential.is_a? ::Signet::OAuth1::Credential
432
432
  raise TypeError,
433
- "Expected Signet::OAuth1::Credential, " +
434
- "got #{new_token_credential.class}."
433
+ "Expected Signet::OAuth1::Credential, " \
434
+ "got #{new_token_credential.class}."
435
435
  end
436
436
  @token_credential_key = new_token_credential.key
437
437
  @token_credential_secret = new_token_credential.secret
@@ -440,28 +440,28 @@ module Signet
440
440
  @token_credential_secret = nil
441
441
  end
442
442
  end
443
- alias_method :access_token=, :token_credential=
443
+ alias access_token= token_credential=
444
444
 
445
445
  ##
446
446
  # Returns the token credential key for this client.
447
447
  #
448
448
  # @return [String] The token credential key.
449
449
  def token_credential_key
450
- return @token_credential_key
450
+ @token_credential_key
451
451
  end
452
- alias_method :access_token_key, :token_credential_key
452
+ alias access_token_key token_credential_key
453
453
 
454
454
  ##
455
455
  # Sets the token credential key for this client.
456
456
  #
457
457
  # @param [String, #to_str] new_token_credential_key
458
458
  # The token credential key.
459
- def token_credential_key=(new_token_credential_key)
460
- if new_token_credential_key != nil
461
- if !new_token_credential_key.respond_to?(:to_str)
459
+ def token_credential_key= new_token_credential_key
460
+ if !new_token_credential_key.nil?
461
+ unless new_token_credential_key.respond_to? :to_str
462
462
  raise TypeError,
463
- "Can't convert #{new_token_credential_key.class} " +
464
- "into String."
463
+ "Can't convert #{new_token_credential_key.class} " \
464
+ "into String."
465
465
  end
466
466
  new_token_credential_key = new_token_credential_key.to_str
467
467
  @token_credential_key = new_token_credential_key
@@ -469,28 +469,28 @@ module Signet
469
469
  @token_credential_key = nil
470
470
  end
471
471
  end
472
- alias_method :access_token_key=, :token_credential_key=
472
+ alias access_token_key= token_credential_key=
473
473
 
474
474
  ##
475
475
  # Returns the token credential secret for this client.
476
476
  #
477
477
  # @return [String] The token credential secret.
478
478
  def token_credential_secret
479
- return @token_credential_secret
479
+ @token_credential_secret
480
480
  end
481
- alias_method :access_token_secret, :token_credential_secret
481
+ alias access_token_secret token_credential_secret
482
482
 
483
483
  ##
484
484
  # Sets the token credential secret for this client.
485
485
  #
486
486
  # @param [String, #to_str] new_token_credential_secret
487
487
  # The token credential secret.
488
- def token_credential_secret=(new_token_credential_secret)
489
- if new_token_credential_secret != nil
490
- if !new_token_credential_secret.respond_to?(:to_str)
488
+ def token_credential_secret= new_token_credential_secret
489
+ if !new_token_credential_secret.nil?
490
+ unless new_token_credential_secret.respond_to? :to_str
491
491
  raise TypeError,
492
- "Can't convert #{new_token_credential_secret.class} " +
493
- "into String."
492
+ "Can't convert #{new_token_credential_secret.class} " \
493
+ "into String."
494
494
  end
495
495
  new_token_credential_secret =
496
496
  new_token_credential_secret.to_str
@@ -499,14 +499,14 @@ module Signet
499
499
  @token_credential_secret = nil
500
500
  end
501
501
  end
502
- alias_method :access_token_secret=, :token_credential_secret=
502
+ alias access_token_secret= token_credential_secret=
503
503
 
504
504
  ##
505
505
  # Returns the callback for this client.
506
506
  #
507
507
  # @return [String] The OAuth callback.
508
508
  def callback
509
- return @callback || ::Signet::OAuth1::OUT_OF_BAND
509
+ @callback || ::Signet::OAuth1::OUT_OF_BAND
510
510
  end
511
511
 
512
512
  ##
@@ -514,11 +514,11 @@ module Signet
514
514
  #
515
515
  # @param [String, #to_str] new_callback
516
516
  # The OAuth callback.
517
- def callback=(new_callback)
518
- if new_callback != nil
519
- if !new_callback.respond_to?(:to_str)
517
+ def callback= new_callback
518
+ if !new_callback.nil?
519
+ unless new_callback.respond_to? :to_str
520
520
  raise TypeError,
521
- "Can't convert #{new_callback.class} into String."
521
+ "Can't convert #{new_callback.class} into String."
522
522
  end
523
523
  new_callback = new_callback.to_str
524
524
  @callback = new_callback
@@ -533,7 +533,7 @@ module Signet
533
533
  # @return [TrueClass, FalseClass]
534
534
  # <code>true</code> for two-legged mode, <code>false</code> otherwise.
535
535
  def two_legged
536
- return @two_legged ||= false
536
+ @two_legged ||= false
537
537
  end
538
538
 
539
539
  ##
@@ -541,10 +541,10 @@ module Signet
541
541
  #
542
542
  # @param [TrueClass, FalseClass] new_two_legged
543
543
  # <code>true</code> for two-legged mode, <code>false</code> otherwise.
544
- def two_legged=(new_two_legged)
544
+ def two_legged= new_two_legged
545
545
  if new_two_legged != true && new_two_legged != false
546
546
  raise TypeError,
547
- "Expected true or false, got #{new_two_legged.class}."
547
+ "Expected true or false, got #{new_two_legged.class}."
548
548
  else
549
549
  @two_legged = new_two_legged
550
550
  end
@@ -557,20 +557,22 @@ module Signet
557
557
  #
558
558
  # @return [String] A serialized JSON representation of the client.
559
559
  def to_json
560
- return MultiJson.dump({
561
- 'temporary_credential_uri' => self.temporary_credential_uri,
562
- 'authorization_uri' => self.authorization_uri,
563
- 'token_credential_uri' => self.token_credential_uri,
564
- 'callback' => self.callback,
565
- 'two_legged' => self.two_legged,
566
- 'client_credential_key' => self.client_credential_key,
567
- 'client_credential_secret' => self.client_credential_secret,
568
- 'temporary_credential_key' => self.temporary_credential_key,
569
- 'temporary_credential_secret' => self.temporary_credential_secret,
570
- 'token_credential_key' => self.token_credential_key,
571
- 'token_credential_secret' => self.token_credential_secret
572
- })
560
+ MultiJson.dump(
561
+ "temporary_credential_uri" => temporary_credential_uri,
562
+ "authorization_uri" => authorization_uri,
563
+ "token_credential_uri" => token_credential_uri,
564
+ "callback" => callback,
565
+ "two_legged" => two_legged,
566
+ "client_credential_key" => client_credential_key,
567
+ "client_credential_secret" => client_credential_secret,
568
+ "temporary_credential_key" => temporary_credential_key,
569
+ "temporary_credential_secret" => temporary_credential_secret,
570
+ "token_credential_key" => token_credential_key,
571
+ "token_credential_secret" => token_credential_secret
572
+ )
573
573
  end
574
+ # rubocop:disable Metrics/AbcSize
575
+ # rubocop:disable Metrics/MethodLength
574
576
 
575
577
  ##
576
578
  # Generates a request for temporary credentials.
@@ -585,60 +587,57 @@ module Signet
585
587
  # The Authorization realm. See RFC 2617.
586
588
  #
587
589
  # @return [Array] The request object.
588
- def generate_temporary_credential_request(options={})
590
+ def generate_temporary_credential_request options = {}
589
591
  verifications = {
590
- :temporary_credential_uri => 'Temporary credentials URI',
591
- :client_credential_key => 'Client credential key',
592
- :client_credential_secret => 'Client credential secret'
592
+ temporary_credential_uri: "Temporary credentials URI",
593
+ client_credential_key: "Client credential key",
594
+ client_credential_secret: "Client credential secret"
593
595
  }
594
596
  # Make sure all required state is set
595
597
  verifications.each do |(key, _value)|
596
- unless self.send(key)
597
- raise ArgumentError, "#{key} was not set."
598
- end
598
+ raise ArgumentError, "#{key} was not set." unless send key
599
599
  end
600
600
  options = {
601
- :signature_method => 'HMAC-SHA1',
602
- :additional_parameters => [],
603
- :realm => nil,
604
- :connection => Faraday.default_connection
601
+ signature_method: "HMAC-SHA1",
602
+ additional_parameters: [],
603
+ realm: nil,
604
+ connection: Faraday.default_connection
605
605
  }.merge(options)
606
606
  method = :post
607
607
  parameters = ::Signet::OAuth1.unsigned_temporary_credential_parameters(
608
- :client_credential_key => self.client_credential_key,
609
- :callback => self.callback,
610
- :signature_method => options[:signature_method],
611
- :additional_parameters => options[:additional_parameters]
608
+ client_credential_key: client_credential_key,
609
+ callback: callback,
610
+ signature_method: options[:signature_method],
611
+ additional_parameters: options[:additional_parameters]
612
612
  )
613
613
  signature = ::Signet::OAuth1.sign_parameters(
614
614
  method,
615
- self.temporary_credential_uri,
615
+ temporary_credential_uri,
616
616
  parameters,
617
- self.client_credential_secret
617
+ client_credential_secret
618
618
  )
619
- parameters << ['oauth_signature', signature]
619
+ parameters << ["oauth_signature", signature]
620
620
  authorization_header = [
621
- 'Authorization',
621
+ "Authorization",
622
622
  ::Signet::OAuth1.generate_authorization_header(
623
623
  parameters, options[:realm]
624
624
  )
625
625
  ]
626
626
  headers = [authorization_header]
627
627
  if method == :post
628
- headers << ['Content-Type', 'application/x-www-form-urlencoded']
629
- headers << ['Content-Length', '0']
628
+ headers << ["Content-Type", "application/x-www-form-urlencoded"]
629
+ headers << ["Content-Length", "0"]
630
630
  end
631
- return options[:connection].build_request(method.to_s.downcase.to_sym) do |req|
631
+ options[:connection].build_request method.to_s.downcase.to_sym do |req|
632
632
  req.url(Addressable::URI.parse(
633
- self.temporary_credential_uri.to_str
633
+ temporary_credential_uri.to_str
634
634
  ).normalize.to_s)
635
- req.headers = Faraday::Utils::Headers.new(headers)
635
+ req.headers = Faraday::Utils::Headers.new headers
636
636
  end
637
637
  end
638
- alias_method(
639
- :generate_request_token_request,
640
- :generate_temporary_credential_request
641
- )
638
+ # rubocop:enable Metrics/AbcSize
639
+ # rubocop:enable Metrics/MethodLength
640
+ alias generate_request_token_request generate_temporary_credential_request
642
641
 
643
642
  ##
644
643
  # Transmits a request for a temporary credential. This method does not
@@ -664,36 +663,24 @@ module Signet
664
663
  # :scope => 'https://mail.google.com/mail/feed/atom'
665
664
  # }
666
665
  # )
667
- def fetch_temporary_credential(options={})
666
+ def fetch_temporary_credential options = {}
668
667
  options[:connection] ||= Faraday.default_connection
669
- request = self.generate_temporary_credential_request(options)
670
- request_env = request.to_env(options[:connection])
668
+ request = generate_temporary_credential_request options
669
+ request_env = request.to_env options[:connection]
671
670
  request_env[:request] ||= request
672
- response = options[:connection].app.call(request_env)
673
- if response.status.to_i == 200
674
- return ::Signet::OAuth1.parse_form_encoded_credentials(response.body)
675
- elsif [400, 401, 403].include?(response.status.to_i)
676
- message = 'Authorization failed.'
677
- if response.body.to_s.strip.length > 0
678
- message += " Server message:\n#{response.body.to_s.strip}"
679
- end
680
- raise ::Signet::AuthorizationError.new(
681
- message, :request => request, :response => response
682
- )
683
- else
684
- message = "Unexpected status code: #{response.status}."
685
- if response.body.to_s.strip.length > 0
686
- message += " Server message:\n#{response.body.to_s.strip}"
687
- end
688
- raise ::Signet::AuthorizationError.new(
689
- message, :request => request, :response => response
690
- )
691
- end
671
+ response = options[:connection].app.call request_env
672
+ return ::Signet::OAuth1.parse_form_encoded_credentials response.body if response.status.to_i == 200
673
+ message = if [400, 401, 403].include? response.status.to_i
674
+ "Authorization failed."
675
+ else
676
+ "Unexpected status code: #{response.status}."
677
+ end
678
+ message += " Server message:\n#{response.body.to_s.strip}" unless response.body.to_s.strip.empty?
679
+ raise ::Signet::AuthorizationError.new(
680
+ message, request: request, response: response
681
+ )
692
682
  end
693
- alias_method(
694
- :fetch_request_token,
695
- :fetch_temporary_credential
696
- )
683
+ alias fetch_request_token fetch_temporary_credential
697
684
 
698
685
  ##
699
686
  # Transmits a request for a temporary credential. This method updates
@@ -717,14 +704,13 @@ module Signet
717
704
  # client.fetch_temporary_credential!(:additional_parameters => {
718
705
  # :scope => 'https://mail.google.com/mail/feed/atom'
719
706
  # })
720
- def fetch_temporary_credential!(options={})
721
- credential = self.fetch_temporary_credential(options)
707
+ def fetch_temporary_credential! options = {}
708
+ credential = fetch_temporary_credential options
722
709
  self.temporary_credential = credential
723
710
  end
724
- alias_method(
725
- :fetch_request_token!,
726
- :fetch_temporary_credential!
727
- )
711
+ alias fetch_request_token! fetch_temporary_credential!
712
+ # rubocop:disable Metrics/AbcSize
713
+ # rubocop:disable Metrics/MethodLength
728
714
 
729
715
  ##
730
716
  # Generates a request for token credentials.
@@ -739,63 +725,59 @@ module Signet
739
725
  # The Authorization realm. See RFC 2617.
740
726
  #
741
727
  # @return [Array] The request object.
742
- def generate_token_credential_request(options={})
728
+ def generate_token_credential_request options = {}
743
729
  verifications = {
744
- :token_credential_uri => 'Token credentials URI',
745
- :client_credential_key => 'Client credential key',
746
- :client_credential_secret => 'Client credential secret',
747
- :temporary_credential_key => 'Temporary credential key',
748
- :temporary_credential_secret => 'Temporary credential secret'
730
+ token_credential_uri: "Token credentials URI",
731
+ client_credential_key: "Client credential key",
732
+ client_credential_secret: "Client credential secret",
733
+ temporary_credential_key: "Temporary credential key",
734
+ temporary_credential_secret: "Temporary credential secret"
749
735
  }
750
736
  # Make sure all required state is set
751
737
  verifications.each do |(key, _value)|
752
- unless self.send(key)
753
- raise ArgumentError, "#{key} was not set."
754
- end
738
+ raise ArgumentError, "#{key} was not set." unless send key
755
739
  end
756
740
  options = {
757
- :signature_method => 'HMAC-SHA1',
758
- :realm => nil,
759
- :connection => Faraday.default_connection
741
+ signature_method: "HMAC-SHA1",
742
+ realm: nil,
743
+ connection: Faraday.default_connection
760
744
  }.merge(options)
761
745
  method = :post
762
746
  parameters = ::Signet::OAuth1.unsigned_token_credential_parameters(
763
- :client_credential_key => self.client_credential_key,
764
- :temporary_credential_key => self.temporary_credential_key,
765
- :signature_method => options[:signature_method],
766
- :verifier => options[:verifier]
747
+ client_credential_key: client_credential_key,
748
+ temporary_credential_key: temporary_credential_key,
749
+ signature_method: options[:signature_method],
750
+ verifier: options[:verifier]
767
751
  )
768
752
  signature = ::Signet::OAuth1.sign_parameters(
769
753
  method,
770
- self.token_credential_uri,
754
+ token_credential_uri,
771
755
  parameters,
772
- self.client_credential_secret,
773
- self.temporary_credential_secret
756
+ client_credential_secret,
757
+ temporary_credential_secret
774
758
  )
775
- parameters << ['oauth_signature', signature]
759
+ parameters << ["oauth_signature", signature]
776
760
  authorization_header = [
777
- 'Authorization',
761
+ "Authorization",
778
762
  ::Signet::OAuth1.generate_authorization_header(
779
763
  parameters, options[:realm]
780
764
  )
781
765
  ]
782
766
  headers = [authorization_header]
783
- headers << ['Cache-Control', 'no-store']
767
+ headers << ["Cache-Control", "no-store"]
784
768
  if method == :post
785
- headers << ['Content-Type', 'application/x-www-form-urlencoded']
786
- headers << ['Content-Length', '0']
769
+ headers << ["Content-Type", "application/x-www-form-urlencoded"]
770
+ headers << ["Content-Length", "0"]
787
771
  end
788
- return options[:connection].build_request(method.to_s.downcase.to_sym) do |req|
772
+ options[:connection].build_request method.to_s.downcase.to_sym do |req|
789
773
  req.url(Addressable::URI.parse(
790
- self.token_credential_uri.to_str
774
+ token_credential_uri.to_str
791
775
  ).normalize.to_s)
792
- req.headers = Faraday::Utils::Headers.new(headers)
776
+ req.headers = Faraday::Utils::Headers.new headers
793
777
  end
794
778
  end
795
- alias_method(
796
- :generate_access_token_request,
797
- :generate_token_credential_request
798
- )
779
+ # rubocop:enable Metrics/MethodLength
780
+ alias generate_access_token_request generate_token_credential_request
799
781
 
800
782
  ##
801
783
  # Transmits a request for a token credential. This method does not
@@ -819,36 +801,25 @@ module Signet
819
801
  # token_credential = client.fetch_token_credential(
820
802
  # :verifier => '12345'
821
803
  # )
822
- def fetch_token_credential(options={})
804
+ def fetch_token_credential options = {}
823
805
  options[:connection] ||= Faraday.default_connection
824
- request = self.generate_token_credential_request(options)
825
- request_env = request.to_env(options[:connection])
806
+ request = generate_token_credential_request options
807
+ request_env = request.to_env options[:connection]
826
808
  request_env[:request] ||= request
827
- response = options[:connection].app.call(request_env)
828
- if response.status.to_i == 200
829
- return ::Signet::OAuth1.parse_form_encoded_credentials(response.body)
830
- elsif [400, 401, 403].include?(response.status.to_i)
831
- message = 'Authorization failed.'
832
- if response.body.to_s.strip.length > 0
833
- message += " Server message:\n#{response.body.to_s.strip}"
834
- end
835
- raise ::Signet::AuthorizationError.new(
836
- message, :request => request, :response => response
837
- )
838
- else
839
- message = "Unexpected status code: #{response.status}."
840
- if response.body.to_s.strip.length > 0
841
- message += " Server message:\n#{response.body.to_s.strip}"
842
- end
843
- raise ::Signet::AuthorizationError.new(
844
- message, :request => request, :response => response
845
- )
846
- end
809
+ response = options[:connection].app.call request_env
810
+ return ::Signet::OAuth1.parse_form_encoded_credentials response.body if response.status.to_i == 200
811
+ message = if [400, 401, 403].include? response.status.to_i
812
+ "Authorization failed."
813
+ else
814
+ "Unexpected status code: #{response.status}."
815
+ end
816
+ message += " Server message:\n#{response.body.to_s.strip}" unless response.body.to_s.strip.empty?
817
+ raise ::Signet::AuthorizationError.new(
818
+ message, request: request, response: response
819
+ )
847
820
  end
848
- alias_method(
849
- :fetch_access_token,
850
- :fetch_token_credential
851
- )
821
+ # rubocop:enable Metrics/AbcSize
822
+ alias fetch_access_token fetch_token_credential
852
823
 
853
824
  ##
854
825
  # Transmits a request for a token credential. This method updates
@@ -870,14 +841,15 @@ module Signet
870
841
  #
871
842
  # @example
872
843
  # client.fetch_token_credential!(:verifier => '12345')
873
- def fetch_token_credential!(options={})
874
- credential = self.fetch_token_credential(options)
844
+ def fetch_token_credential! options = {}
845
+ credential = fetch_token_credential options
875
846
  self.token_credential = credential
876
847
  end
877
- alias_method(
878
- :fetch_access_token!,
879
- :fetch_token_credential!
880
- )
848
+ alias fetch_access_token! fetch_token_credential!
849
+ # rubocop:disable Metrics/AbcSize
850
+ # rubocop:disable Metrics/CyclomaticComplexity
851
+ # rubocop:disable Metrics/MethodLength
852
+ # rubocop:disable Metrics/PerceivedComplexity
881
853
 
882
854
  ##
883
855
  # Generates an authenticated request for protected resources.
@@ -900,91 +872,85 @@ module Signet
900
872
  # The Authorization realm. See RFC 2617.
901
873
  #
902
874
  # @return [Array] The request object.
903
- def generate_authenticated_request(options={})
875
+ def generate_authenticated_request options = {}
904
876
  verifications = {
905
- :client_credential_key => 'Client credential key',
906
- :client_credential_secret => 'Client credential secret'
877
+ client_credential_key: "Client credential key",
878
+ client_credential_secret: "Client credential secret"
907
879
  }
908
- unless self.two_legged
880
+ unless two_legged
909
881
  verifications.update(
910
- :token_credential_key => 'Token credential key',
911
- :token_credential_secret => 'Token credential secret'
882
+ token_credential_key: "Token credential key",
883
+ token_credential_secret: "Token credential secret"
912
884
  )
913
885
  end
914
886
  # Make sure all required state is set
915
887
  verifications.each do |(key, _value)|
916
- unless self.send(key)
917
- raise ArgumentError, "#{key} was not set."
918
- end
888
+ raise ArgumentError, "#{key} was not set." unless send key
919
889
  end
920
890
  options = {
921
- :signature_method => 'HMAC-SHA1',
922
- :realm => nil,
923
- :connection => Faraday.default_connection
891
+ signature_method: "HMAC-SHA1",
892
+ realm: nil,
893
+ connection: Faraday.default_connection
924
894
  }.merge(options)
925
895
 
926
- if options[:request].kind_of?(Faraday::Request)
896
+ if options[:request].is_a? Faraday::Request
927
897
  request = options[:request]
928
898
  else
929
- if options[:request].kind_of?(Array)
899
+ if options[:request].is_a? Array
930
900
  method, uri, headers, body = options[:request]
931
901
  else
932
902
  method = options[:method] || :get
933
903
  uri = options[:uri]
934
904
  headers = options[:headers] || []
935
- body = options[:body] || ''
905
+ body = options[:body] || ""
936
906
  end
937
- headers = headers.to_a if headers.kind_of?(Hash)
907
+ headers = headers.to_a if headers.is_a? Hash
938
908
  request_components = {
939
- :method => method,
940
- :uri => uri,
941
- :headers => headers,
942
- :body => body
909
+ method: method,
910
+ uri: uri,
911
+ headers: headers,
912
+ body: body
943
913
  }
944
914
  # Verify that we have all pieces required to return an HTTP request
945
915
  request_components.each do |(key, value)|
946
- unless value
947
- raise ArgumentError, "Missing :#{key} parameter."
948
- end
916
+ raise ArgumentError, "Missing :#{key} parameter." unless value
949
917
  end
950
- if !body.kind_of?(String) && body.respond_to?(:each)
918
+ if !body.is_a?(String) && body.respond_to?(:each)
951
919
  # Just in case we get a chunked body
952
920
  merged_body = StringIO.new
953
921
  body.each do |chunk|
954
- merged_body.write(chunk)
922
+ merged_body.write chunk
955
923
  end
956
924
  body = merged_body.string
957
925
  end
958
- if !body.kind_of?(String)
959
- raise TypeError, "Expected String, got #{body.class}."
960
- end
926
+ raise TypeError, "Expected String, got #{body.class}." unless body.is_a? String
961
927
  method = method.to_s.downcase.to_sym
962
- request = options[:connection].build_request(method) do |req|
963
- req.url(Addressable::URI.parse(uri).normalize.to_s)
964
- req.headers = Faraday::Utils::Headers.new(headers)
928
+ request = options[:connection].build_request method do |req|
929
+ req.url Addressable::URI.parse(uri).normalize.to_s
930
+ req.headers = Faraday::Utils::Headers.new headers
965
931
  req.body = body
966
932
  end
967
933
  end
968
934
 
969
935
  parameters = ::Signet::OAuth1.unsigned_resource_parameters(
970
- :client_credential_key => self.client_credential_key,
971
- :token_credential_key => self.token_credential_key,
972
- :signature_method => options[:signature_method],
973
- :two_legged => self.two_legged
936
+ client_credential_key: client_credential_key,
937
+ token_credential_key: token_credential_key,
938
+ signature_method: options[:signature_method],
939
+ two_legged: two_legged
974
940
  )
975
941
 
976
- env = request.to_env(options[:connection])
942
+ env = request.to_env options[:connection]
977
943
 
978
- content_type = request['Content-Type'].to_s
979
- content_type = content_type.split(';', 2).first if content_type.index(';')
980
- if request.method == :post && content_type == 'application/x-www-form-urlencoded'
944
+ content_type = request["Content-Type"].to_s
945
+ content_type = content_type.split(";", 2).first if content_type.index ";"
946
+ if request.method == :post && content_type == "application/x-www-form-urlencoded"
981
947
  # Serializes the body in case a hash/array was passed. Noop if already string like
982
- encoder = Faraday::Request::UrlEncoded.new(lambda { |_env| })
983
- encoder.call(env)
948
+ encoder = Faraday::Request::UrlEncoded.new(->(_env) {})
949
+ encoder.call env
984
950
  request.body = env[:body]
985
951
 
986
- post_parameters = Addressable::URI.form_unencode(env[:body])
987
- parameters = parameters.concat(post_parameters)
952
+ post_parameters = Addressable::URI.form_unencode env[:body]
953
+ parameters = parameters.concat post_parameters
988
954
  end
989
955
 
990
956
  # No need to attach URI query parameters, the .sign_parameters
@@ -993,16 +959,21 @@ module Signet
993
959
  env[:method],
994
960
  env[:url],
995
961
  parameters,
996
- self.client_credential_secret,
997
- self.token_credential_secret
962
+ client_credential_secret,
963
+ token_credential_secret
998
964
  )
999
965
 
1000
- parameters << ['oauth_signature', signature]
1001
- request['Authorization'] = ::Signet::OAuth1.generate_authorization_header(
1002
- parameters, options[:realm])
1003
- request['Cache-Control'] = 'no-store'
1004
- return request
966
+ parameters << ["oauth_signature", signature]
967
+ request["Authorization"] = ::Signet::OAuth1.generate_authorization_header(
968
+ parameters, options[:realm]
969
+ )
970
+ request["Cache-Control"] = "no-store"
971
+ request
1005
972
  end
973
+ # rubocop:enable Metrics/AbcSize
974
+ # rubocop:enable Metrics/CyclomaticComplexity
975
+ # rubocop:enable Metrics/MethodLength
976
+ # rubocop:enable Metrics/PerceivedComplexity
1006
977
 
1007
978
  ##
1008
979
  # Transmits a request for a protected resource.
@@ -1043,25 +1014,20 @@ module Signet
1043
1014
  # )
1044
1015
  #
1045
1016
  # @return [Array] The response object.
1046
- def fetch_protected_resource(options={})
1017
+ def fetch_protected_resource options = {}
1047
1018
  options[:connection] ||= Faraday.default_connection
1048
- request = self.generate_authenticated_request(options)
1049
- request_env = request.to_env(options[:connection])
1019
+ request = generate_authenticated_request options
1020
+ request_env = request.to_env options[:connection]
1050
1021
  request_env[:request] ||= request
1051
- response = options[:connection].app.call(request_env)
1052
- if response.status.to_i == 401
1053
- # When accessing a protected resource, we only want to raise an
1054
- # error for 401 responses.
1055
- message = 'Authorization failed.'
1056
- if response.body.to_s.strip.length > 0
1057
- message += " Server message:\n#{response.body.to_s.strip}"
1058
- end
1059
- raise ::Signet::AuthorizationError.new(
1060
- message, :request => request, :response => response
1061
- )
1062
- else
1063
- return response
1064
- end
1022
+ response = options[:connection].app.call request_env
1023
+ return response unless response.status.to_i == 401
1024
+ # When accessing a protected resource, we only want to raise an
1025
+ # error for 401 responses.
1026
+ message = "Authorization failed."
1027
+ message += " Server message:\n#{response.body.to_s.strip}" unless response.body.to_s.strip.empty?
1028
+ raise ::Signet::AuthorizationError.new(
1029
+ message, request: request, response: response
1030
+ )
1065
1031
  end
1066
1032
  end
1067
1033
  end