@@ -41,7 +41,6 @@ the context of recreating parts of the Twitter API.
41
41
``` ruby
42
42
module Twitter
43
43
class API < Grape ::API
44
-
45
44
version ' v1' , using: :header , vendor: ' twitter'
46
45
format :json
47
46
@@ -56,7 +55,6 @@ module Twitter
56
55
end
57
56
58
57
resource :statuses do
59
-
60
58
desc " Return a public timeline."
61
59
get :public_timeline do
62
60
Status .limit(20 )
@@ -111,7 +109,6 @@ module Twitter
111
109
authenticate!
112
110
current_user.statuses.find(params[:id ]).destroy
113
111
end
114
-
115
112
end
116
113
end
117
114
end
@@ -152,7 +149,7 @@ require 'grape'
152
149
153
150
class API < Grape ::API
154
151
get :hello do
155
- {hello: " world" }
152
+ { hello: " world" }
156
153
end
157
154
end
158
155
@@ -303,7 +300,7 @@ The Grape endpoint:
303
300
304
301
``` ruby
305
302
post ' /statuses' do
306
- Status .create!({ text: params[:text ] } )
303
+ Status .create!(text: params[:text ])
307
304
end
308
305
```
309
306
@@ -465,10 +462,10 @@ You can rescue a `Grape::Exceptions::ValidationErrors` and respond with a custom
465
462
466
463
``` ruby
467
464
rescue_from Grape ::Exceptions ::ValidationErrors do |e |
468
- Rack ::Response .new ({
469
- ' status' => e.status,
470
- ' message' => e.message,
471
- ' errors' => e.errors
465
+ Rack ::Response .new (
466
+ status: e.status,
467
+ message: e.message,
468
+ errors: e.errors
472
469
}.to_json, e.status)
473
470
end
474
471
```
@@ -480,7 +477,6 @@ The validation errors are grouped by parameter name and can be accessed via ``Gr
480
477
Grape supports I18n for parameter-related error messages, but will fallback to English if
481
478
translations for the default locale have not been provided. See [ en.yml] ( lib/grape/locale/en.yml ) for message keys.
482
479
483
-
484
480
## Headers
485
481
486
482
Request headers are available through the ` headers ` helper or from ` env ` in their original form.
500
496
You can set a response header with ` header ` inside an API.
501
497
502
498
``` ruby
503
- header " X-Robots-Tag" , " noindex"
499
+ header ' X-Robots-Tag' , ' noindex'
500
+ ```
501
+
502
+ When raising ` error! ` , pass additional headers as arguments.
503
+
504
+ ``` ruby
505
+ error! ' Unauthorized' , 401 , ' X-Error-Detail' => ' Invalid token.'
504
506
```
505
507
506
508
## Routes
@@ -558,7 +560,6 @@ You can set, get and delete your cookies very simply using `cookies` method.
558
560
559
561
``` ruby
560
562
class API < Grape ::API
561
-
562
563
get ' status_count' do
563
564
cookies[:status_count ] ||= 0
564
565
cookies[:status_count ] += 1
@@ -568,18 +569,17 @@ class API < Grape::API
568
569
delete ' status_count' do
569
570
{ status_count: cookies.delete(:status_count ) }
570
571
end
571
-
572
572
end
573
573
```
574
574
575
575
Use a hash-based syntax to set more than one value.
576
576
577
577
``` ruby
578
578
cookies[:status_count ] = {
579
- value: 0 ,
580
- expires: Time .tomorrow,
581
- domain: ' .twitter.com' ,
582
- path: ' /'
579
+ value: 0 ,
580
+ expires: Time .tomorrow,
581
+ domain: ' .twitter.com' ,
582
+ path: ' /'
583
583
}
584
584
585
585
cookies[:status_count ][:value ] += 1
@@ -602,11 +602,11 @@ cookies.delete :status_count, path: '/'
602
602
You can redirect to a new url temporarily (302) or permanently (301).
603
603
604
604
``` ruby
605
- redirect " /statuses"
605
+ redirect ' /statuses'
606
606
```
607
607
608
608
``` ruby
609
- redirect " /statuses" , permanent: true
609
+ redirect ' /statuses' , permanent: true
610
610
```
611
611
612
612
## Allowed Methods
@@ -617,13 +617,11 @@ behavior with `do_not_route_head!`.
617
617
618
618
``` ruby
619
619
class API < Grape ::API
620
-
621
620
do_not_route_head!
622
621
623
622
get ' /example' do
624
623
# only responds to GET
625
624
end
626
-
627
625
end
628
626
```
629
627
@@ -633,7 +631,6 @@ include an "Allow" header listing the supported methods.
633
631
634
632
``` ruby
635
633
class API < Grape ::API
636
-
637
634
get ' /rt_count' do
638
635
{ rt_count: current_user.rt_count }
639
636
end
@@ -645,7 +642,6 @@ class API < Grape::API
645
642
current_user.rt_count += params[:value ].to_i
646
643
{ rt_count: current_user.rt_count }
647
644
end
648
-
649
645
end
650
646
```
651
647
@@ -678,14 +674,14 @@ curl -X DELETE -v http://localhost:3000/rt_count/
678
674
You can abort the execution of an API method by raising errors with ` error! ` .
679
675
680
676
``` ruby
681
- error! " Access Denied" , 401
677
+ error! ' Access Denied' , 401
682
678
```
683
679
684
680
You can also return JSON formatted objects by raising error! and passing a hash
685
681
instead of a message.
686
682
687
683
``` ruby
688
- error!({ " error" => " unexpected error" , " detail" => " missing widget" }, 500 )
684
+ error!({ error: " unexpected error" , detail: " missing widget" }, 500 )
689
685
```
690
686
691
687
### Default Error HTTP Status Code
@@ -1365,7 +1361,6 @@ Create `config/initializers/reload_api.rb`.
1365
1361
1366
1362
``` ruby
1367
1363
if Rails .env.development?
1368
-
1369
1364
ActiveSupport ::Dependencies .explicitly_unloadable_constants << " Twitter::API"
1370
1365
1371
1366
api_files = Dir [" #{ Rails .root} /app/api/**/*.rb" ]
@@ -1375,13 +1370,11 @@ if Rails.env.development?
1375
1370
ActionDispatch ::Callbacks .to_prepare do
1376
1371
api_reloader.execute_if_updated
1377
1372
end
1378
-
1379
1373
end
1380
1374
```
1381
1375
1382
1376
See [ StackOverflow #3282655 ] ( http://stackoverflow.com/questions/3282655/ruby-on-rails-3-reload-lib-directory-for-each-request/4368838#4368838 ) for more information.
1383
1377
1384
-
1385
1378
## Performance Monitoring
1386
1379
1387
1380
Grape integrates with NewRelic via the
0 commit comments