@@ -26,7 +26,7 @@ def reset!
26
26
end
27
27
28
28
def compile
29
- @instance = self . new
29
+ @instance = new
30
30
end
31
31
32
32
def change!
@@ -81,12 +81,12 @@ def do_not_route_options!
81
81
# version 'v2'
82
82
#
83
83
# get '/main' do
84
- # {: some => 'data'}
84
+ # {some: 'data'}
85
85
# end
86
86
#
87
87
# version 'v1' do
88
88
# get '/main' do
89
- # {: legacy => 'data'}
89
+ # {legacy: 'data'}
90
90
# end
91
91
# end
92
92
# end
@@ -95,7 +95,7 @@ def version(*args, &block)
95
95
if args . any?
96
96
options = args . pop if args . last . is_a? Hash
97
97
options ||= { }
98
- options = { : using => :path } . merge! ( options )
98
+ options = { using : :path } . merge ( options )
99
99
100
100
raise Grape ::Exceptions ::MissingVendorOption . new if options [ :using ] == :header && !options . has_key? ( :vendor )
101
101
@@ -111,7 +111,7 @@ def version(*args, &block)
111
111
112
112
# Add a description to the next namespace or function.
113
113
def desc ( description , options = { } )
114
- @last_description = options . merge ( : description => description )
114
+ @last_description = options . merge ( description : description )
115
115
end
116
116
117
117
# Specify the default format for the API's serializers.
@@ -209,9 +209,7 @@ def rescue_from(*args, &block)
209
209
end
210
210
211
211
options = args . last . is_a? ( Hash ) ? args . pop : { }
212
- if options . has_key? ( :with )
213
- handler ||= proc { options [ :with ] }
214
- end
212
+ handler ||= proc { options [ :with ] } if options . has_key? ( :with )
215
213
216
214
if handler
217
215
args . each do |arg |
@@ -220,8 +218,12 @@ def rescue_from(*args, &block)
220
218
end
221
219
222
220
imbue ( :rescue_options , options )
223
- set ( :rescue_all , true ) and return if args . include? ( :all )
224
- imbue ( :rescued_errors , args )
221
+
222
+ if args . include? ( :all )
223
+ set ( :rescue_all , true )
224
+ else
225
+ imbue ( :rescued_errors , args )
226
+ end
225
227
end
226
228
227
229
# Allows you to specify a default representation entity for a
@@ -230,10 +232,10 @@ def rescue_from(*args, &block)
230
232
#
231
233
# @example
232
234
# class ExampleAPI < Grape::API
233
- # represent User, : with => Entity::User
235
+ # represent User, with: Entity::User
234
236
#
235
237
# get '/me' do
236
- # present current_user # : with => Entity::User is assumed
238
+ # present current_user # with: Entity::User is assumed
237
239
# end
238
240
# end
239
241
#
@@ -276,7 +278,7 @@ def helpers(new_mod = nil, &block)
276
278
include new_mod
277
279
end
278
280
end
279
- mod . class_eval &block if block_given?
281
+ mod . class_eval ( &block ) if block_given?
280
282
set ( :helpers , mod )
281
283
else
282
284
mod = Module . new
@@ -292,7 +294,7 @@ def helpers(new_mod = nil, &block)
292
294
# only `:http_basic`, `:http_digest` and `:oauth2` are supported.
293
295
def auth ( type = nil , options = { } , &block )
294
296
if type
295
- set ( :auth , { : type => type . to_sym , : proc => block } . merge ( options ) )
297
+ set ( :auth , { type : type . to_sym , proc : block } . merge ( options ) )
296
298
else
297
299
settings [ :auth ]
298
300
end
@@ -318,14 +320,14 @@ def mount(mounts)
318
320
mounts . each_pair do |app , path |
319
321
if app . respond_to? ( :inherit_settings , true )
320
322
app_settings = settings . clone
321
- mount_path = Rack ::Mount ::Utils . normalize_path ( [ settings [ :mount_path ] , path ] . compact . join ( "/" ) )
323
+ mount_path = Rack ::Mount ::Utils . normalize_path ( [ settings [ :mount_path ] , path ] . compact . join ( "/" ) )
322
324
app_settings . set :mount_path , mount_path
323
325
app . inherit_settings ( app_settings )
324
326
end
325
327
endpoints << Grape ::Endpoint . new ( settings . clone , {
326
- : method => :any ,
327
- : path => path ,
328
- : app => app
328
+ method : :any ,
329
+ path : path ,
330
+ app : app
329
331
} )
330
332
end
331
333
end
@@ -339,14 +341,14 @@ def mount(mounts)
339
341
# @example Defining a basic route.
340
342
# class MyAPI < Grape::API
341
343
# route(:any, '/hello') do
342
- # {: hello => 'world'}
344
+ # {hello: 'world'}
343
345
# end
344
346
# end
345
347
def route ( methods , paths = [ '/' ] , route_options = { } , &block )
346
348
endpoint_options = {
347
- : method => methods ,
348
- : path => paths ,
349
- : route_options => ( @namespace_description || { } ) . deep_merge ( @last_description || { } ) . deep_merge ( route_options || { } )
349
+ method : methods ,
350
+ path : paths ,
351
+ route_options : ( @namespace_description || { } ) . deep_merge ( @last_description || { } ) . deep_merge ( route_options || { } )
350
352
}
351
353
endpoints << Grape ::Endpoint . new ( settings . clone , endpoint_options , &block )
352
354
@@ -366,13 +368,33 @@ def after(&block)
366
368
imbue ( :afters , [ block ] )
367
369
end
368
370
369
- def get ( paths = [ '/' ] , options = { } , &block ) ; route ( 'GET' , paths , options , &block ) end
370
- def post ( paths = [ '/' ] , options = { } , &block ) ; route ( 'POST' , paths , options , &block ) end
371
- def put ( paths = [ '/' ] , options = { } , &block ) ; route ( 'PUT' , paths , options , &block ) end
372
- def head ( paths = [ '/' ] , options = { } , &block ) ; route ( 'HEAD' , paths , options , &block ) end
373
- def delete ( paths = [ '/' ] , options = { } , &block ) ; route ( 'DELETE' , paths , options , &block ) end
374
- def options ( paths = [ '/' ] , options = { } , &block ) ; route ( 'OPTIONS' , paths , options , &block ) end
375
- def patch ( paths = [ '/' ] , options = { } , &block ) ; route ( 'PATCH' , paths , options , &block ) end
371
+ def get ( paths = [ '/' ] , options = { } , &block )
372
+ route ( 'GET' , paths , options , &block )
373
+ end
374
+
375
+ def post ( paths = [ '/' ] , options = { } , &block )
376
+ route ( 'POST' , paths , options , &block )
377
+ end
378
+
379
+ def put ( paths = [ '/' ] , options = { } , &block )
380
+ route ( 'PUT' , paths , options , &block )
381
+ end
382
+
383
+ def head ( paths = [ '/' ] , options = { } , &block )
384
+ route ( 'HEAD' , paths , options , &block )
385
+ end
386
+
387
+ def delete ( paths = [ '/' ] , options = { } , &block )
388
+ route ( 'DELETE' , paths , options , &block )
389
+ end
390
+
391
+ def options ( paths = [ '/' ] , options = { } , &block )
392
+ route ( 'OPTIONS' , paths , options , &block )
393
+ end
394
+
395
+ def patch ( paths = [ '/' ] , options = { } , &block )
396
+ route ( 'PATCH' , paths , options , &block )
397
+ end
376
398
377
399
def namespace ( space = nil , options = { } , &block )
378
400
if space || block_given?
@@ -427,7 +449,10 @@ def use(middleware_class, *args, &block)
427
449
# and arguments that are currently applied to the
428
450
# application.
429
451
def middleware
430
- settings . stack . inject ( [ ] ) { |a , s | a += s [ :middleware ] if s [ :middleware ] ; a }
452
+ settings . stack . inject ( [ ] ) do |a , s |
453
+ a += s [ :middleware ] if s [ :middleware ]
454
+ a
455
+ end
431
456
end
432
457
433
458
# An array of API routes.
@@ -440,9 +465,11 @@ def versions
440
465
end
441
466
442
467
def cascade ( value = nil )
443
- value . nil? ?
444
- ( settings . has_key? ( :cascade ) ? !! settings [ :cascade ] : true ) :
468
+ if value . nil?
469
+ settings . has_key? ( :cascade ) ? !!settings [ :cascade ] : true
470
+ else
445
471
set ( :cascade , value )
472
+ end
446
473
end
447
474
448
475
protected
@@ -459,15 +486,15 @@ def prepare_routes
459
486
# block passed in. Allows for simple 'before' setups
460
487
# of settings stack pushes.
461
488
def nest ( *blocks , &block )
462
- blocks . reject! { |b | b . nil? }
489
+ blocks . reject! { |b | b . nil? }
463
490
if blocks . any?
464
491
settings . push # create a new context to eval the follow
465
- instance_eval &block if block_given?
466
- blocks . each { |b | instance_eval & b }
467
- settings . pop # when finished, we pop the context
492
+ instance_eval ( &block ) if block_given?
493
+ blocks . each { |b | instance_eval ( & b ) }
494
+ settings . pop # when finished, we pop the context
468
495
reset_validations!
469
496
else
470
- instance_eval &block
497
+ instance_eval ( &block )
471
498
end
472
499
end
473
500
@@ -497,7 +524,7 @@ def initialize
497
524
def call ( env )
498
525
status , headers , body = @route_set . call ( env )
499
526
headers . delete ( 'X-Cascade' ) unless cascade?
500
- [ status , headers , body ]
527
+ [ status , headers , body ]
501
528
end
502
529
503
530
# Some requests may return a HTTP 404 error if grape cannot find a matching
@@ -509,8 +536,8 @@ def call(env)
509
536
# errors from reaching upstream. This is effectivelly done by unsetting
510
537
# X-Cascade. Default :cascade is true.
511
538
def cascade?
512
- return !! self . class . settings [ :cascade ] if self . class . settings . has_key? ( :cascade )
513
- return !! self . class . settings [ :version_options ] [ :cascade ] if self . class . settings [ :version_options ] && self . class . settings [ :version_options ] . has_key? ( :cascade )
539
+ return !!self . class . settings [ :cascade ] if self . class . settings . has_key? ( :cascade )
540
+ return !!self . class . settings [ :version_options ] [ :cascade ] if self . class . settings [ :version_options ] && self . class . settings [ :version_options ] . has_key? ( :cascade )
514
541
true
515
542
end
516
543
@@ -523,32 +550,34 @@ def cascade?
523
550
# will return an HTTP 405 response for any HTTP method that the resource
524
551
# cannot handle.
525
552
def add_head_not_allowed_methods
526
- allowed_methods = Hash . new { |h , k | h [ k ] = [ ] }
527
- resources = self . class . endpoints . map do |endpoint |
528
- endpoint . options [ :app ] && endpoint . options [ :app ] . respond_to? ( :endpoints ) ?
529
- endpoint . options [ :app ] . endpoints . map ( &:routes ) :
553
+ allowed_methods = Hash . new { |h , k | h [ k ] = [ ] }
554
+ resources = self . class . endpoints . map do |endpoint |
555
+ if endpoint . options [ :app ] && endpoint . options [ :app ] . respond_to? ( :endpoints )
556
+ endpoint . options [ :app ] . endpoints . map ( &:routes )
557
+ else
530
558
endpoint . routes
559
+ end
531
560
end
532
561
resources . flatten . each do |route |
533
562
allowed_methods [ route . route_compiled ] << route . route_method
534
563
end
535
564
allowed_methods . each do |path_info , methods |
536
- if methods . include? ( 'GET' ) && ! methods . include? ( "HEAD" ) && ! self . class . settings [ :do_not_route_head ]
537
- methods = methods | [ 'HEAD' ]
565
+ if methods . include? ( 'GET' ) && !methods . include? ( "HEAD" ) && !self . class . settings [ :do_not_route_head ]
566
+ methods = methods | [ 'HEAD' ]
538
567
end
539
568
allow_header = ( [ "OPTIONS" ] | methods ) . join ( ", " )
540
569
unless methods . include? ( "OPTIONS" ) || self . class . settings [ :do_not_route_options ]
541
- @route_set . add_route ( proc { [ 204 , { 'Allow' => allow_header } , [ ] ] } , {
542
- : path_info => path_info ,
543
- : request_method => "OPTIONS"
570
+ @route_set . add_route ( proc { [ 204 , { 'Allow' => allow_header } , [ ] ] } , {
571
+ path_info : path_info ,
572
+ request_method : "OPTIONS"
544
573
} )
545
574
end
546
575
not_allowed_methods = %w( GET PUT POST DELETE PATCH HEAD ) - methods
547
576
not_allowed_methods << "OPTIONS" if self . class . settings [ :do_not_route_options ]
548
577
not_allowed_methods . each do |bad_method |
549
- @route_set . add_route ( proc { [ 405 , { 'Allow' => allow_header , 'Content-Type' => 'text/plain' } , [ ] ] } , {
550
- : path_info => path_info ,
551
- : request_method => bad_method
578
+ @route_set . add_route ( proc { [ 405 , { 'Allow' => allow_header , 'Content-Type' => 'text/plain' } , [ ] ] } , {
579
+ path_info : path_info ,
580
+ request_method : bad_method
552
581
} )
553
582
end
554
583
end
0 commit comments