grape 2.0.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -1
  3. data/README.md +362 -316
  4. data/UPGRADING.md +197 -7
  5. data/grape.gemspec +5 -6
  6. data/lib/grape/api/instance.rb +13 -10
  7. data/lib/grape/api.rb +17 -8
  8. data/lib/grape/content_types.rb +0 -2
  9. data/lib/grape/cookies.rb +2 -1
  10. data/lib/grape/dry_types.rb +0 -2
  11. data/lib/grape/dsl/desc.rb +22 -20
  12. data/lib/grape/dsl/headers.rb +1 -1
  13. data/lib/grape/dsl/inside_route.rb +42 -13
  14. data/lib/grape/dsl/parameters.rb +4 -3
  15. data/lib/grape/dsl/routing.rb +20 -4
  16. data/lib/grape/dsl/validations.rb +13 -0
  17. data/lib/grape/endpoint.rb +12 -15
  18. data/lib/grape/{util/env.rb → env.rb} +0 -5
  19. data/lib/grape/error_formatter/txt.rb +11 -10
  20. data/lib/grape/exceptions/base.rb +3 -3
  21. data/lib/grape/exceptions/validation.rb +0 -2
  22. data/lib/grape/exceptions/validation_array_errors.rb +1 -0
  23. data/lib/grape/exceptions/validation_errors.rb +1 -3
  24. data/lib/grape/extensions/hash.rb +5 -1
  25. data/lib/grape/http/headers.rb +18 -34
  26. data/lib/grape/{util/json.rb → json.rb} +1 -3
  27. data/lib/grape/locale/en.yml +3 -0
  28. data/lib/grape/middleware/auth/base.rb +0 -2
  29. data/lib/grape/middleware/auth/dsl.rb +0 -2
  30. data/lib/grape/middleware/base.rb +0 -2
  31. data/lib/grape/middleware/error.rb +55 -50
  32. data/lib/grape/middleware/formatter.rb +16 -13
  33. data/lib/grape/middleware/globals.rb +1 -3
  34. data/lib/grape/middleware/stack.rb +2 -3
  35. data/lib/grape/middleware/versioner/accept_version_header.rb +0 -2
  36. data/lib/grape/middleware/versioner/header.rb +17 -163
  37. data/lib/grape/middleware/versioner/param.rb +2 -4
  38. data/lib/grape/middleware/versioner/path.rb +1 -3
  39. data/lib/grape/namespace.rb +3 -4
  40. data/lib/grape/path.rb +24 -29
  41. data/lib/grape/request.rb +4 -12
  42. data/lib/grape/router/base_route.rb +39 -0
  43. data/lib/grape/router/greedy_route.rb +20 -0
  44. data/lib/grape/router/pattern.rb +39 -30
  45. data/lib/grape/router/route.rb +22 -59
  46. data/lib/grape/router.rb +32 -37
  47. data/lib/grape/util/accept/header.rb +19 -0
  48. data/lib/grape/util/accept_header_handler.rb +105 -0
  49. data/lib/grape/util/base_inheritable.rb +4 -4
  50. data/lib/grape/util/cache.rb +0 -3
  51. data/lib/grape/util/endpoint_configuration.rb +1 -1
  52. data/lib/grape/util/header.rb +13 -0
  53. data/lib/grape/util/inheritable_values.rb +0 -2
  54. data/lib/grape/util/lazy/block.rb +29 -0
  55. data/lib/grape/util/lazy/object.rb +45 -0
  56. data/lib/grape/util/lazy/value.rb +38 -0
  57. data/lib/grape/util/lazy/value_array.rb +21 -0
  58. data/lib/grape/util/lazy/value_enumerable.rb +34 -0
  59. data/lib/grape/util/lazy/value_hash.rb +21 -0
  60. data/lib/grape/util/media_type.rb +70 -0
  61. data/lib/grape/util/reverse_stackable_values.rb +1 -6
  62. data/lib/grape/util/stackable_values.rb +1 -6
  63. data/lib/grape/util/strict_hash_configuration.rb +3 -3
  64. data/lib/grape/validations/attributes_doc.rb +38 -36
  65. data/lib/grape/validations/contract_scope.rb +71 -0
  66. data/lib/grape/validations/params_scope.rb +10 -9
  67. data/lib/grape/validations/types/array_coercer.rb +0 -2
  68. data/lib/grape/validations/types/build_coercer.rb +69 -71
  69. data/lib/grape/validations/types/dry_type_coercer.rb +1 -11
  70. data/lib/grape/validations/types/json.rb +0 -2
  71. data/lib/grape/validations/types/primitive_coercer.rb +0 -2
  72. data/lib/grape/validations/types/set_coercer.rb +0 -3
  73. data/lib/grape/validations/types.rb +0 -3
  74. data/lib/grape/validations/validators/base.rb +1 -0
  75. data/lib/grape/validations/validators/default_validator.rb +5 -1
  76. data/lib/grape/validations/validators/length_validator.rb +42 -0
  77. data/lib/grape/validations/validators/values_validator.rb +6 -1
  78. data/lib/grape/validations.rb +3 -7
  79. data/lib/grape/version.rb +1 -1
  80. data/lib/grape/{util/xml.rb → xml.rb} +1 -1
  81. data/lib/grape.rb +30 -274
  82. metadata +31 -37
  83. data/lib/grape/eager_load.rb +0 -20
  84. data/lib/grape/middleware/versioner/parse_media_type_patch.rb +0 -24
  85. data/lib/grape/router/attribute_translator.rb +0 -63
  86. data/lib/grape/util/lazy_block.rb +0 -27
  87. data/lib/grape/util/lazy_object.rb +0 -43
  88. data/lib/grape/util/lazy_value.rb +0 -91
data/README.md CHANGED
@@ -19,21 +19,18 @@
19
19
  - [Mounting](#mounting)
20
20
  - [All](#all)
21
21
  - [Rack](#rack)
22
- - [ActiveRecord without Rails](#activerecord-without-rails)
23
- - [Rails 4](#rails-4)
24
- - [Rails 5+](#rails-5)
25
22
  - [Alongside Sinatra (or other frameworks)](#alongside-sinatra-or-other-frameworks)
26
23
  - [Rails](#rails)
27
- - [Rails < 5.2](#rails--52)
28
- - [Rails 6.0](#rails-60)
24
+ - [Zeitwerk](#zeitwerk)
29
25
  - [Modules](#modules)
30
26
  - [Remounting](#remounting)
31
27
  - [Mount Configuration](#mount-configuration)
32
28
  - [Versioning](#versioning)
33
- - [Path](#path)
34
- - [Header](#header)
35
- - [Accept-Version Header](#accept-version-header)
36
- - [Param](#param)
29
+ - [Strategies](#strategies)
30
+ - [Path](#path)
31
+ - [Header](#header)
32
+ - [Accept-Version Header](#accept-version-header)
33
+ - [Param](#param)
37
34
  - [Describing Methods](#describing-methods)
38
35
  - [Configuration](#configuration)
39
36
  - [Parameters](#parameters)
@@ -42,6 +39,7 @@
42
39
  - [Include Parent Namespaces](#include-parent-namespaces)
43
40
  - [Include Missing](#include-missing)
44
41
  - [Evaluate Given](#evaluate-given)
42
+ - [Parameter Precedence](#parameter-precedence)
45
43
  - [Parameter Validation and Coercion](#parameter-validation-and-coercion)
46
44
  - [Supported Parameter Types](#supported-parameter-types)
47
45
  - [Integer/Fixnum and Coercions](#integerfixnum-and-coercions)
@@ -58,6 +56,7 @@
58
56
  - [values](#values)
59
57
  - [except_values](#except_values)
60
58
  - [same_as](#same_as)
59
+ - [length](#length)
61
60
  - [regexp](#regexp)
62
61
  - [mutually_exclusive](#mutually_exclusive)
63
62
  - [exactly_one_of](#exactly_one_of)
@@ -71,6 +70,7 @@
71
70
  - [Custom Validation messages](#custom-validation-messages)
72
71
  - [presence, allow_blank, values, regexp](#presence-allow_blank-values-regexp)
73
72
  - [same_as](#same_as-1)
73
+ - [length](#length-1)
74
74
  - [all_or_none_of](#all_or_none_of-1)
75
75
  - [mutually_exclusive](#mutually_exclusive-1)
76
76
  - [exactly_one_of](#exactly_one_of-1)
@@ -80,6 +80,7 @@
80
80
  - [Pass symbols for i18n translations](#pass-symbols-for-i18n-translations)
81
81
  - [Overriding Attribute Names](#overriding-attribute-names)
82
82
  - [With Default](#with-default)
83
+ - [Using dry-validation or dry-schema](#using-dry-validation-or-dry-schema)
83
84
  - [Headers](#headers)
84
85
  - [Request](#request)
85
86
  - [Header Case Handling](#header-case-handling)
@@ -100,7 +101,6 @@
100
101
  - [Rescuing exceptions inside namespaces](#rescuing-exceptions-inside-namespaces)
101
102
  - [Unrescuable Exceptions](#unrescuable-exceptions)
102
103
  - [Exceptions that should be rescued explicitly](#exceptions-that-should-be-rescued-explicitly)
103
- - [Rails 3.x](#rails-3x)
104
104
  - [Logging](#logging)
105
105
  - [API Formats](#api-formats)
106
106
  - [JSONP](#jsonp)
@@ -121,6 +121,7 @@
121
121
  - [Current Route and Endpoint](#current-route-and-endpoint)
122
122
  - [Before, After and Finally](#before-after-and-finally)
123
123
  - [Anchoring](#anchoring)
124
+ - [Instance Variables](#instance-variables)
124
125
  - [Using Custom Middleware](#using-custom-middleware)
125
126
  - [Grape Middleware](#grape-middleware)
126
127
  - [Rails Middleware](#rails-middleware)
@@ -152,18 +153,13 @@
152
153
 
153
154
  ## What is Grape?
154
155
 
155
- Grape is a REST-like API framework for Ruby. It's designed to run on Rack
156
- or complement existing web application frameworks such as Rails and Sinatra by
157
- providing a simple DSL to easily develop RESTful APIs. It has built-in support
158
- for common conventions, including multiple formats, subdomain/prefix restriction,
159
- content negotiation, versioning and much more.
156
+ Grape is a REST-like API framework for Ruby. It's designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily develop RESTful APIs. It has built-in support for common conventions, including multiple formats, subdomain/prefix restriction, content negotiation, versioning and much more.
160
157
 
161
158
  ## Stable Release
162
159
 
163
- You're reading the documentation for the stable release of Grape, **2.0.0**.
160
+ You're reading the documentation for the stable release of Grape, **2.1.1**.
164
161
  Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
165
162
 
166
-
167
163
  ## Project Resources
168
164
 
169
165
  * [Grape Website](http://www.ruby-grape.org)
@@ -179,7 +175,7 @@ The maintainers of Grape are working with Tidelift to deliver commercial support
179
175
 
180
176
  ## Installation
181
177
 
182
- Ruby 2.6 or newer is required.
178
+ Ruby 2.7 or newer is required.
183
179
 
184
180
  Grape is available as a gem, to install it run:
185
181
 
@@ -188,8 +184,7 @@ Grape is available as a gem, to install it run:
188
184
  ## Basic Usage
189
185
 
190
186
  Grape APIs are Rack applications that are created by subclassing `Grape::API`.
191
- Below is a simple example showing some of the more common features of Grape in
192
- the context of recreating parts of the Twitter API.
187
+ Below is a simple example showing some of the more common features of Grape in the context of recreating parts of the Twitter API.
193
188
 
194
189
  ```ruby
195
190
  module Twitter
@@ -287,8 +282,7 @@ This can be added to your `config.ru` (if using rackup), `application.rb` (if us
287
282
 
288
283
  ### Rack
289
284
 
290
- The above sample creates a Rack application that can be run from a rackup `config.ru` file
291
- with `rackup`:
285
+ The above sample creates a Rack application that can be run from a rackup `config.ru` file with `rackup`:
292
286
 
293
287
  ```ruby
294
288
  run Twitter::API
@@ -312,32 +306,9 @@ And would respond to the following routes:
312
306
 
313
307
  Grape will also automatically respond to HEAD and OPTIONS for all GET, and just OPTIONS for all other routes.
314
308
 
315
- ### ActiveRecord without Rails
316
-
317
- If you want to use ActiveRecord within Grape, you will need to make sure that ActiveRecord's connection pool
318
- is handled correctly.
319
-
320
- #### Rails 4
321
-
322
- The easiest way to achieve that is by using ActiveRecord's `ConnectionManagement` middleware in your
323
- `config.ru` before mounting Grape, e.g.:
324
-
325
- ```ruby
326
- use ActiveRecord::ConnectionAdapters::ConnectionManagement
327
- ```
328
-
329
- #### Rails 5+
330
-
331
- Use [otr-activerecord](https://github.com/jhollinger/otr-activerecord) as follows:
332
-
333
- ```ruby
334
- use OTR::ActiveRecord::ConnectionManagement
335
- ```
336
-
337
309
  ### Alongside Sinatra (or other frameworks)
338
310
 
339
- If you wish to mount Grape alongside another Rack framework such as Sinatra, you can do so easily using
340
- `Rack::Cascade`:
311
+ If you wish to mount Grape alongside another Rack framework such as Sinatra, you can do so easily using `Rack::Cascade`:
341
312
 
342
313
  ```ruby
343
314
  # Example config.ru
@@ -373,21 +344,8 @@ Modify `config/routes`:
373
344
  ```ruby
374
345
  mount Twitter::API => '/'
375
346
  ```
376
-
377
- #### Rails < 5.2
378
-
379
- Modify `application.rb`:
380
-
381
- ```ruby
382
- config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
383
- config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]
384
- ```
385
-
386
- See [below](#reloading-api-changes-in-development) for additional code that enables reloading of API changes in development.
387
-
388
- #### Rails 6.0
389
-
390
- For Rails versions greater than 6.0.0.beta2, `Zeitwerk` autoloader is the default for CRuby. By default `Zeitwerk` inflects `api` as `Api` instead of `API`. To make our example work, you need to uncomment the lines at the bottom of `config/initializers/inflections.rb`, and add `API` as an acronym:
347
+ #### Zeitwerk
348
+ Rails's default autoloader is `Zeitwerk`. By default, it inflects `api` as `Api` instead of `API`. To make our example work, you need to uncomment the lines at the bottom of `config/initializers/inflections.rb`, and add `API` as an acronym:
391
349
 
392
350
  ```ruby
393
351
  ActiveSupport::Inflector.inflections(:en) do |inflect|
@@ -397,8 +355,7 @@ end
397
355
 
398
356
  ### Modules
399
357
 
400
- You can mount multiple API implementations inside another one. These don't have to be
401
- different versions, but may be components of the same API.
358
+ You can mount multiple API implementations inside another one. These don't have to be different versions, but may be components of the same API.
402
359
 
403
360
  ```ruby
404
361
  class Twitter::API < Grape::API
@@ -415,7 +372,7 @@ class Twitter::API < Grape::API
415
372
  end
416
373
  ```
417
374
 
418
- Keep in mind such declarations as `before/after/rescue_from` must be placed before `mount` in a case where they should be inherited.
375
+ Declarations as `before/after/rescue_from` can be placed before or after `mount`. In any case they will be inherited.
419
376
 
420
377
  ```ruby
421
378
  class Twitter::API < Grape::API
@@ -423,8 +380,20 @@ class Twitter::API < Grape::API
423
380
  header 'X-Base-Header', 'will be defined for all APIs that are mounted below'
424
381
  end
425
382
 
383
+ rescue_from :all do
384
+ error!({ "error" => "Internal Server Error" }, 500)
385
+ end
386
+
426
387
  mount Twitter::Users
427
388
  mount Twitter::Search
389
+
390
+ after do
391
+ clean_cache!
392
+ end
393
+
394
+ rescue_from ZeroDivisionError do
395
+ error!({ "error" => "Not found" }, 404)
396
+ end
428
397
  end
429
398
  ```
430
399
 
@@ -555,10 +524,69 @@ end
555
524
 
556
525
  ## Versioning
557
526
 
558
- There are four strategies in which clients can reach your API's endpoints: `:path`,
559
- `:header`, `:accept_version_header` and `:param`. The default strategy is `:path`.
527
+ You have the option to provide various versions of your API by establishing a separate `Grape::API` class for each offered version and then integrating them into a primary `Grape::API` class. Ensure that newer versions are mounted before older ones. The default approach to versioning directs the request to the subsequent Rack middleware if a specific version is not found.
528
+
529
+ ```ruby
530
+ require 'v1'
531
+ require 'v2'
532
+ require 'v3'
533
+ class App < Grape::API
534
+ mount V3
535
+ mount V2
536
+ mount V1
537
+ end
538
+ ```
539
+
540
+ To maintain the same endpoints from earlier API versions without rewriting them, you can indicate multiple versions within the previous API versions.
541
+
542
+ ```ruby
543
+ class V1 < Grape::API
544
+ version 'v1', 'v2', 'v3'
545
+
546
+ get '/foo' do
547
+ # your code for GET /foo
548
+ end
549
+
550
+ get '/other' do
551
+ # your code for GET /other
552
+ end
553
+ end
554
+
555
+ class V2 < Grape::API
556
+ version 'v2', 'v3'
557
+
558
+ get '/var' do
559
+ # your code for GET /var
560
+ end
561
+ end
562
+
563
+ class V3 < Grape::API
564
+ version 'v3'
565
+
566
+ get '/foo' do
567
+ # your new code for GET /foo
568
+ end
569
+ end
570
+ ```
571
+
572
+ Using the example provided, the subsequent endpoints will be accessible across various versions:
560
573
 
561
- ### Path
574
+ ```shell
575
+ GET /v1/foo
576
+ GET /v1/other
577
+ GET /v2/foo # => Same behavior as v1
578
+ GET /v2/other # => Same behavior as v1
579
+ GET /v2/var # => New endpoint not available in v1
580
+ GET /v3/foo # => Different behavior to v1 and v2
581
+ GET /v3/other # => Same behavior as v1 and v2
582
+ GET /v3/var # => Same behavior as v2
583
+ ```
584
+
585
+ There are four strategies in which clients can reach your API's endpoints: `:path`, `:header`, `:accept_version_header` and `:param`. The default strategy is `:path`.
586
+
587
+ ### Strategies
588
+
589
+ #### Path
562
590
 
563
591
  ```ruby
564
592
  version 'v1', using: :path
@@ -568,7 +596,7 @@ Using this versioning strategy, clients should pass the desired version in the U
568
596
 
569
597
  curl http://localhost:9292/v1/statuses/public_timeline
570
598
 
571
- ### Header
599
+ #### Header
572
600
 
573
601
  ```ruby
574
602
  version 'v1', using: :header, vendor: 'twitter'
@@ -586,20 +614,15 @@ Using this versioning strategy, clients should pass the desired version in the H
586
614
 
587
615
  curl -H Accept:application/vnd.twitter-v1+json http://localhost:9292/statuses/public_timeline
588
616
 
589
- By default, the first matching version is used when no `Accept` header is
590
- supplied. This behavior is similar to routing in Rails. To circumvent this default behavior,
591
- one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error
592
- is returned when no correct `Accept` header is supplied.
617
+ By default, the first matching version is used when no `Accept` header is supplied. This behavior is similar to routing in Rails. To circumvent this default behavior, one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error is returned when no correct `Accept` header is supplied.
593
618
 
594
- When an invalid `Accept` header is supplied, a `406 Not Acceptable` error is returned if the `:cascade`
595
- option is set to `false`. Otherwise a `404 Not Found` error is returned by Rack if no other route
596
- matches.
619
+ When an invalid `Accept` header is supplied, a `406 Not Acceptable` error is returned if the `:cascade` option is set to `false`. Otherwise a `404 Not Found` error is returned by Rack if no other route matches.
597
620
 
598
621
  Grape will evaluate the relative quality preference included in Accept headers and default to a quality of 1.0 when omitted. In the following example a Grape API that supports XML and JSON in that order will return JSON:
599
622
 
600
623
  curl -H "Accept: text/xml;q=0.8, application/json;q=0.9" localhost:1234/resource
601
624
 
602
- ### Accept-Version Header
625
+ #### Accept-Version Header
603
626
 
604
627
  ```ruby
605
628
  version 'v1', using: :accept_version_header
@@ -609,20 +632,15 @@ Using this versioning strategy, clients should pass the desired version in the H
609
632
 
610
633
  curl -H "Accept-Version:v1" http://localhost:9292/statuses/public_timeline
611
634
 
612
- By default, the first matching version is used when no `Accept-Version` header is
613
- supplied. This behavior is similar to routing in Rails. To circumvent this default behavior,
614
- one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error
615
- is returned when no correct `Accept` header is supplied and the `:cascade` option is set to `false`.
616
- Otherwise a `404 Not Found` error is returned by Rack if no other route matches.
635
+ By default, the first matching version is used when no `Accept-Version` header is supplied. This behavior is similar to routing in Rails. To circumvent this default behavior, one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error is returned when no correct `Accept` header is supplied and the `:cascade` option is set to `false`. Otherwise a `404 Not Found` error is returned by Rack if no other route matches.
617
636
 
618
- ### Param
637
+ #### Param
619
638
 
620
639
  ```ruby
621
640
  version 'v1', using: :param
622
641
  ```
623
642
 
624
- Using this versioning strategy, clients should pass the desired version as a request parameter,
625
- either in the URL query string or in the request body.
643
+ Using this versioning strategy, clients should pass the desired version as a request parameter, either in the URL query string or in the request body.
626
644
 
627
645
  curl http://localhost:9292/statuses/public_timeline?apiver=v1
628
646
 
@@ -713,13 +731,11 @@ API.configure do |config|
713
731
  end
714
732
  ```
715
733
 
716
- This will be available inside the API with `configuration`, as if it were
717
- [mount configuration](#mount-configuration).
734
+ This will be available inside the API with `configuration`, as if it were [mount configuration](#mount-configuration).
718
735
 
719
736
  ## Parameters
720
737
 
721
- Request parameters are available through the `params` hash object. This includes `GET`, `POST`
722
- and `PUT` parameters, along with any named parameters you specify in your route strings.
738
+ Request parameters are available through the `params` hash object. This includes `GET`, `POST` and `PUT` parameters, along with any named parameters you specify in your route strings.
723
739
 
724
740
  ```ruby
725
741
  get :public_timeline do
@@ -727,8 +743,7 @@ get :public_timeline do
727
743
  end
728
744
  ```
729
745
 
730
- Parameters are automatically populated from the request body on `POST` and `PUT` for form input, JSON and
731
- XML content-types.
746
+ Parameters are automatically populated from the request body on `POST` and `PUT` for form input, JSON and XML content-types.
732
747
 
733
748
  The request:
734
749
 
@@ -1067,8 +1082,7 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d
1067
1082
  }
1068
1083
  ````
1069
1084
 
1070
- Note that an attribute with a `nil` value is not considered *missing* and will also be returned
1071
- when `include_missing` is set to `false`:
1085
+ Note that an attribute with a `nil` value is not considered *missing* and will also be returned when `include_missing` is set to `false`:
1072
1086
 
1073
1087
  **Request**
1074
1088
 
@@ -1186,6 +1200,35 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/child -d '{"chil
1186
1200
  }
1187
1201
  ````
1188
1202
 
1203
+ ### Parameter Precedence
1204
+
1205
+ Using `route_param` takes higher precedence over a regular parameter defined with same name:
1206
+
1207
+ ```ruby
1208
+ params do
1209
+ requires :foo, type: String
1210
+ end
1211
+ route_param :foo do
1212
+ get do
1213
+ { value: params[:foo] }
1214
+ end
1215
+ end
1216
+ ```
1217
+
1218
+ **Request**
1219
+
1220
+ ```bash
1221
+ curl -X POST -H "Content-Type: application/json" localhost:9292/bar -d '{"foo": "baz"}'
1222
+ ```
1223
+
1224
+ **Response**
1225
+
1226
+ ```json
1227
+ {
1228
+ "value": "bar"
1229
+ }
1230
+ ```
1231
+
1189
1232
  ## Parameter Validation and Coercion
1190
1233
 
1191
1234
  You can define validations and coercion options for your parameters using a `params` block.
@@ -1207,8 +1250,7 @@ put ':id' do
1207
1250
  end
1208
1251
  ```
1209
1252
 
1210
- When a type is specified an implicit validation is done after the coercion to ensure
1211
- the output type is the one declared.
1253
+ When a type is specified an implicit validation is done after the coercion to ensure the output type is the one declared.
1212
1254
 
1213
1255
  Optional parameters can have a default value.
1214
1256
 
@@ -1220,9 +1262,7 @@ params do
1220
1262
  end
1221
1263
  ```
1222
1264
 
1223
- Default values are eagerly evaluated. Above `:non_random_number` will evaluate to the same
1224
- number for each call to the endpoint of this `params` block. To have the default evaluate
1225
- lazily with each request use a lambda, like `:random_number` above.
1265
+ Default values are eagerly evaluated. Above `:non_random_number` will evaluate to the same number for each call to the endpoint of this `params` block. To have the default evaluate lazily with each request use a lambda, like `:random_number` above.
1226
1266
 
1227
1267
  Note that default values will be passed through to any validation options specified.
1228
1268
  The following example will always fail if `:color` is not explicitly provided.
@@ -1241,6 +1281,15 @@ params do
1241
1281
  end
1242
1282
  ```
1243
1283
 
1284
+ You can use the value of one parameter as the default value of some other parameter. In this case, if the `primary_color` parameter is not provided, it will have the same value as the `color` one. If both of them not provided, both of them will have `blue` value.
1285
+
1286
+ ```ruby
1287
+ params do
1288
+ optional :color, type: String, default: 'blue'
1289
+ optional :primary_color, type: String, default: -> (params) { params[:color] }
1290
+ end
1291
+ ```
1292
+
1244
1293
  ### Supported Parameter Types
1245
1294
 
1246
1295
  The following are all valid types, supported out of the box by Grape:
@@ -1282,12 +1331,7 @@ get '/int' integers: { int: '45' }
1282
1331
 
1283
1332
  ### Custom Types and Coercions
1284
1333
 
1285
- Aside from the default set of supported types listed above, any class can be
1286
- used as a type as long as an explicit coercion method is supplied. If the type
1287
- implements a class-level `parse` method, Grape will use it automatically.
1288
- This method must take one string argument and return an instance of the correct
1289
- type, or return an instance of `Grape::Types::InvalidValue` which optionally
1290
- accepts a message to be returned in the response.
1334
+ Aside from the default set of supported types listed above, any class can be used as a type as long as an explicit coercion method is supplied. If the type implements a class-level `parse` method, Grape will use it automatically. This method must take one string argument and return an instance of the correct type, or return an instance of `Grape::Types::InvalidValue` which optionally accepts a message to be returned in the response.
1291
1335
 
1292
1336
  ```ruby
1293
1337
  class Color
@@ -1297,7 +1341,7 @@ class Color
1297
1341
  end
1298
1342
 
1299
1343
  def self.parse(value)
1300
- return new(value) if %w[blue red green]).include?(value)
1344
+ return new(value) if %w[blue red green].include?(value)
1301
1345
 
1302
1346
  Grape::Types::InvalidValue.new('Unsupported color')
1303
1347
  end
@@ -1315,10 +1359,7 @@ get '/stuff' do
1315
1359
  end
1316
1360
  ```
1317
1361
 
1318
- Alternatively, a custom coercion method may be supplied for any type of parameter
1319
- using `coerce_with`. Any class or object may be given that implements a `parse` or
1320
- `call` method, in that order of precedence. The method must accept a single string
1321
- parameter, and the return value must match the given `type`.
1362
+ Alternatively, a custom coercion method may be supplied for any type of parameter using `coerce_with`. Any class or object may be given that implements a `parse` or `call` method, in that order of precedence. The method must accept a single string parameter, and the return value must match the given `type`.
1322
1363
 
1323
1364
  ```ruby
1324
1365
  params do
@@ -1342,9 +1383,7 @@ params do
1342
1383
  end
1343
1384
  ```
1344
1385
 
1345
- Grape will assert that coerced values match the given `type`, and will reject the request
1346
- if they do not. To override this behaviour, custom types may implement a `parsed?` method
1347
- that should accept a single argument and return `true` if the value passes type validation.
1386
+ Grape will assert that coerced values match the given `type`, and will reject the request if they do not. To override this behaviour, custom types may implement a `parsed?` method that should accept a single argument and return `true` if the value passes type validation.
1348
1387
 
1349
1388
  ```ruby
1350
1389
  class SecureUri
@@ -1379,9 +1418,7 @@ end
1379
1418
 
1380
1419
  ### First-Class `JSON` Types
1381
1420
 
1382
- Grape supports complex parameters given as JSON-formatted strings using the special `type: JSON`
1383
- declaration. JSON objects and arrays of objects are accepted equally, with nested validation
1384
- rules applied to all objects in either case:
1421
+ Grape supports complex parameters given as JSON-formatted strings using the special `type: JSON` declaration. JSON objects and arrays of objects are accepted equally, with nested validation rules applied to all objects in either case:
1385
1422
 
1386
1423
  ```ruby
1387
1424
  params do
@@ -1400,8 +1437,7 @@ client.get('/', json: '{"int":4}') # => HTTP 400
1400
1437
  client.get('/', json: '[{"int":4}]') # => HTTP 400
1401
1438
  ```
1402
1439
 
1403
- Additionally `type: Array[JSON]` may be used, which explicitly marks the parameter as an array
1404
- of objects. If a single object is supplied it will be wrapped.
1440
+ Additionally `type: Array[JSON]` may be used, which explicitly marks the parameter as an array of objects. If a single object is supplied it will be wrapped.
1405
1441
 
1406
1442
  ```ruby
1407
1443
  params do
@@ -1413,8 +1449,7 @@ get '/' do
1413
1449
  params[:json].each { |obj| ... } # always works
1414
1450
  end
1415
1451
  ```
1416
- For stricter control over the type of JSON structure which may be supplied,
1417
- use `type: Array, coerce_with: JSON` or `type: Hash, coerce_with: JSON`.
1452
+ For stricter control over the type of JSON structure which may be supplied, use `type: Array, coerce_with: JSON` or `type: Hash, coerce_with: JSON`.
1418
1453
 
1419
1454
  ### Multiple Allowed Types
1420
1455
 
@@ -1433,8 +1468,7 @@ client.get('/', status_code: 300) # => 300
1433
1468
  client.get('/', status_code: %w(404 NOT FOUND)) # => [404, "NOT", "FOUND"]
1434
1469
  ```
1435
1470
 
1436
- As a special case, variant-member-type collections may also be declared, by
1437
- passing a `Set` or `Array` with more than one member to `type`:
1471
+ As a special case, variant-member-type collections may also be declared, by passing a `Set` or `Array` with more than one member to `type`:
1438
1472
 
1439
1473
  ```ruby
1440
1474
  params do
@@ -1450,11 +1484,8 @@ client.get('/', status_codes: %w(1 two)) # => [1, "two"]
1450
1484
  ### Validation of Nested Parameters
1451
1485
 
1452
1486
  Parameters can be nested using `group` or by calling `requires` or `optional` with a block.
1453
- In the [above example](#parameter-validation-and-coercion), this means `params[:media][:url]` is required along with `params[:id]`,
1454
- and `params[:audio][:format]` is required only if `params[:audio]` is present.
1455
- With a block, `group`, `requires` and `optional` accept an additional option `type` which can
1456
- be either `Array` or `Hash`, and defaults to `Array`. Depending on the value, the nested
1457
- parameters will be treated either as values of a hash or as values of hashes in an array.
1487
+ In the [above example](#parameter-validation-and-coercion), this means `params[:media][:url]` is required along with `params[:id]`, and `params[:audio][:format]` is required only if `params[:audio]` is present.
1488
+ With a block, `group`, `requires` and `optional` accept an additional option `type` which can be either `Array` or `Hash`, and defaults to `Array`. Depending on the value, the nested parameters will be treated either as values of a hash or as values of hashes in an array.
1458
1489
 
1459
1490
  ```ruby
1460
1491
  params do
@@ -1472,9 +1503,7 @@ end
1472
1503
 
1473
1504
  ### Dependent Parameters
1474
1505
 
1475
- Suppose some of your parameters are only relevant if another parameter is given;
1476
- Grape allows you to express this relationship through the `given` method in your
1477
- parameters block, like so:
1506
+ Suppose some of your parameters are only relevant if another parameter is given; Grape allows you to express this relationship through the `given` method in your parameters block, like so:
1478
1507
 
1479
1508
  ```ruby
1480
1509
  params do
@@ -1513,31 +1542,45 @@ Note: param in `given` should be the renamed one. In the example, it should be `
1513
1542
 
1514
1543
  ### Group Options
1515
1544
 
1516
- Parameters options can be grouped. It can be useful if you want to extract
1517
- common validation or types for several parameters. The example below presents a
1518
- typical case when parameters share common options.
1545
+ Parameters options can be grouped. It can be useful if you want to extract common validation or types for several parameters.
1546
+ Within these groups, individual parameters can extend or selectively override the common settings, allowing you to maintain the defaults at the group level while still applying parameter-specific rules where necessary.
1547
+
1548
+ The example below presents a typical case when parameters share common options.
1519
1549
 
1520
1550
  ```ruby
1521
1551
  params do
1522
- requires :first_name, type: String, regexp: /w+/, desc: 'First name'
1523
- requires :middle_name, type: String, regexp: /w+/, desc: 'Middle name'
1524
- requires :last_name, type: String, regexp: /w+/, desc: 'Last name'
1552
+ requires :first_name, type: String, regexp: /w+/, desc: 'First name', documentation: { in: 'body' }
1553
+ optional :middle_name, type: String, regexp: /w+/, desc: 'Middle name', documentation: { in: 'body', x: { nullable: true } }
1554
+ requires :last_name, type: String, regexp: /w+/, desc: 'Last name', documentation: { in: 'body' }
1525
1555
  end
1526
1556
  ```
1527
1557
 
1528
- Grape allows you to present the same logic through the `with` method in your
1529
- parameters block, like so:
1558
+ Grape allows you to present the same logic through the `with` method in your parameters block, like so:
1530
1559
 
1531
1560
  ```ruby
1532
1561
  params do
1533
- with(type: String, regexp: /w+/) do
1562
+ with(type: String, regexp: /w+/, documentation: { in: 'body' }) do
1534
1563
  requires :first_name, desc: 'First name'
1535
- requires :middle_name, desc: 'Middle name'
1564
+ optional :middle_name, desc: 'Middle name', documentation: { x: { nullable: true } }
1536
1565
  requires :last_name, desc: 'Last name'
1537
1566
  end
1538
1567
  end
1539
1568
  ```
1540
1569
 
1570
+ You can organize settings into layers using nested `with' blocks. Each layer can use, add to, or change the settings of the layer above it. This helps to keep complex parameters organized and consistent, while still allowing for specific customizations to be made.
1571
+
1572
+ ```ruby
1573
+ params do
1574
+ with(documentation: { in: 'body' }) do # Applies documentation to all nested parameters
1575
+ with(type: String, regexp: /\w+/) do # Applies type and validation to names
1576
+ requires :first_name, desc: 'First name'
1577
+ requires :last_name, desc: 'Last name'
1578
+ end
1579
+ optional :age, type: Integer, desc: 'Age', documentation: { x: { nullable: true } } # Specific settings for 'age'
1580
+ end
1581
+ end
1582
+ ```
1583
+
1541
1584
  ### Renaming
1542
1585
 
1543
1586
  You can rename parameters using `as`, which can be useful when refactoring existing APIs:
@@ -1560,13 +1603,9 @@ The value passed to `as` will be the key when calling `declared(params)`.
1560
1603
 
1561
1604
  #### `allow_blank`
1562
1605
 
1563
- Parameters can be defined as `allow_blank`, ensuring that they contain a value. By default, `requires`
1564
- only validates that a parameter was sent in the request, regardless its value. With `allow_blank: false`,
1565
- empty values or whitespace only values are invalid.
1606
+ Parameters can be defined as `allow_blank`, ensuring that they contain a value. By default, `requires` only validates that a parameter was sent in the request, regardless its value. With `allow_blank: false`, empty values or whitespace only values are invalid.
1566
1607
 
1567
- `allow_blank` can be combined with both `requires` and `optional`. If the parameter is required, it has to contain
1568
- a value. If it's optional, it's possible to not send it in the request, but if it's being sent, it has to have
1569
- some value, and not an empty string/only whitespaces.
1608
+ `allow_blank` can be combined with both `requires` and `optional`. If the parameter is required, it has to contain a value. If it's optional, it's possible to not send it in the request, but if it's being sent, it has to have some value, and not an empty string/only whitespaces.
1570
1609
 
1571
1610
 
1572
1611
  ```ruby
@@ -1617,11 +1656,9 @@ end
1617
1656
  ```
1618
1657
 
1619
1658
  The `:values` option can also be supplied with a `Proc`, evaluated lazily with each request.
1620
- If the Proc has arity zero (i.e. it takes no arguments) it is expected to return either a list
1621
- or a range which will then be used to validate the parameter.
1659
+ If the Proc has arity zero (i.e. it takes no arguments) it is expected to return either a list or a range which will then be used to validate the parameter.
1622
1660
 
1623
- For example, given a status model you may want to restrict by hashtags that you have
1624
- previously defined in the `HashTag` model.
1661
+ For example, given a status model you may want to restrict by hashtags that you have previously defined in the `HashTag` model.
1625
1662
 
1626
1663
  ```ruby
1627
1664
  params do
@@ -1629,10 +1666,7 @@ params do
1629
1666
  end
1630
1667
  ```
1631
1668
 
1632
- Alternatively, a Proc with arity one (i.e. taking one argument) can be used to explicitly validate
1633
- each parameter value. In that case, the Proc is expected to return a truthy value if the parameter
1634
- value is valid. The parameter will be considered invalid if the Proc returns a falsy value or if it
1635
- raises a StandardError.
1669
+ Alternatively, a Proc with arity one (i.e. taking one argument) can be used to explicitly validate each parameter value. In that case, the Proc is expected to return a truthy value if the parameter value is valid. The parameter will be considered invalid if the Proc returns a falsy value or if it raises a StandardError.
1636
1670
 
1637
1671
  ```ruby
1638
1672
  params do
@@ -1654,9 +1688,7 @@ end
1654
1688
 
1655
1689
  Parameters can be restricted from having a specific set of values with the `:except_values` option.
1656
1690
 
1657
- The `except_values` validator behaves similarly to the `values` validator in that it accepts either
1658
- an Array, a Range, or a Proc. Unlike the `values` validator, however, `except_values` only accepts
1659
- Procs with arity zero.
1691
+ The `except_values` validator behaves similarly to the `values` validator in that it accepts either an Array, a Range, or a Proc. Unlike the `values` validator, however, `except_values` only accepts Procs with arity zero.
1660
1692
 
1661
1693
  ```ruby
1662
1694
  params do
@@ -1677,11 +1709,23 @@ params do
1677
1709
  end
1678
1710
  ```
1679
1711
 
1712
+ #### `length`
1713
+
1714
+ Parameters with types that support `#length` method can be restricted to have a specific length with the `:length` option.
1715
+
1716
+ The validator accepts `:min` or `:max` or both options to validate that the value of the parameter is within the given limits.
1717
+
1718
+ ```ruby
1719
+ params do
1720
+ requires :str, type: String, length: { min: 3 }
1721
+ requires :list, type: [Integer], length: { min: 3, max: 5 }
1722
+ requires :hash, type: Hash, length: { max: 5 }
1723
+ end
1724
+ ```
1725
+
1680
1726
  #### `regexp`
1681
1727
 
1682
- Parameters can be restricted to match a specific regular expression with the `:regexp` option. If the value
1683
- does not match the regular expression an error will be returned. Note that this is true for both `requires`
1684
- and `optional` parameters.
1728
+ Parameters can be restricted to match a specific regular expression with the `:regexp` option. If the value does not match the regular expression an error will be returned. Note that this is true for both `requires` and `optional` parameters.
1685
1729
 
1686
1730
  ```ruby
1687
1731
  params do
@@ -1816,8 +1860,7 @@ namespace :statuses do
1816
1860
  end
1817
1861
  ```
1818
1862
 
1819
- The `namespace` method has a number of aliases, including: `group`, `resource`,
1820
- `resources`, and `segment`. Use whichever reads the best for your API.
1863
+ The `namespace` method has a number of aliases, including: `group`, `resource`, `resources`, and `segment`. Use whichever reads the best for your API.
1821
1864
 
1822
1865
  You can conveniently define a route parameter as a namespace using `route_param`.
1823
1866
 
@@ -1972,8 +2015,7 @@ end
1972
2015
 
1973
2016
  ### I18n
1974
2017
 
1975
- Grape supports I18n for parameter-related error messages, but will fallback to English if
1976
- translations for the default locale have not been provided. See [en.yml](lib/grape/locale/en.yml) for message keys.
2018
+ Grape supports I18n for parameter-related error messages, but will fallback to English if translations for the default locale have not been provided. See [en.yml](lib/grape/locale/en.yml) for message keys.
1977
2019
 
1978
2020
  In case your app enforces available locales only and :en is not included in your available locales, Grape cannot fall back to English and will return the translation key for the error message. To avoid this behaviour, either provide a translation for your default locale or add :en to your available locales.
1979
2021
 
@@ -1998,6 +2040,15 @@ params do
1998
2040
  end
1999
2041
  ```
2000
2042
 
2043
+ #### `length`
2044
+
2045
+ ```ruby
2046
+ params do
2047
+ requires :str, type: String, length: { min: 5, message: 'str is expected to be atleast 5 characters long' }
2048
+ requires :list, type: [Integer], length: { min: 2, max: 3, message: 'list is expected to have between 2 and 3 elements' }
2049
+ end
2050
+ ```
2051
+
2001
2052
  #### `all_or_none_of`
2002
2053
 
2003
2054
  ```ruby
@@ -2106,6 +2157,40 @@ params do
2106
2157
  end
2107
2158
  ```
2108
2159
 
2160
+ ### Using `dry-validation` or `dry-schema`
2161
+
2162
+ As an alternative to the `params` DSL described above, you can use a schema or `dry-validation` contract to describe an endpoint's parameters. This can be especially useful if you use the above already in some other parts of your application. If not, you'll need to add `dry-validation` or `dry-schema` to your `Gemfile`.
2163
+
2164
+ Then call `contract` with a contract or schema defined previously:
2165
+
2166
+ ```rb
2167
+ CreateOrdersSchema = Dry::Schema.Params do
2168
+ required(:orders).array(:hash) do
2169
+ required(:name).filled(:string)
2170
+ optional(:volume).maybe(:integer, lt?: 9)
2171
+ end
2172
+ end
2173
+
2174
+ # ...
2175
+
2176
+ contract CreateOrdersSchema
2177
+ ```
2178
+
2179
+ or with a block, using the [schema definition syntax](https://dry-rb.org/gems/dry-schema/1.13/#quick-start):
2180
+
2181
+ ```rb
2182
+ contract do
2183
+ required(:orders).array(:hash) do
2184
+ required(:name).filled(:string)
2185
+ optional(:volume).maybe(:integer, lt?: 9)
2186
+ end
2187
+ end
2188
+ ```
2189
+
2190
+ The latter will define a coercing schema (`Dry::Schema.Params`). When using the former approach, it's up to you to decide whether the input will need coercing.
2191
+
2192
+ The `params` and `contract` declarations can also be used together in the same API, e.g. to describe different parts of a nested namespace for an endpoint.
2193
+
2109
2194
  ## Headers
2110
2195
 
2111
2196
  ### Request
@@ -2205,8 +2290,7 @@ namespace ':id' do
2205
2290
  end
2206
2291
  ```
2207
2292
 
2208
- Optionally, you can define requirements for your named route parameters using regular
2209
- expressions on namespace or endpoint. The route will match only if all requirements are met.
2293
+ Optionally, you can define requirements for your named route parameters using regular expressions on namespace or endpoint. The route will match only if all requirements are met.
2210
2294
 
2211
2295
  ```ruby
2212
2296
  get ':id', requirements: { id: /[0-9]*/ } do
@@ -2224,8 +2308,7 @@ end
2224
2308
 
2225
2309
  ## Helpers
2226
2310
 
2227
- You can define helper methods that your endpoints can use with the `helpers`
2228
- macro by either giving a block or an array of modules.
2311
+ You can define helper methods that your endpoints can use with the `helpers` macro by either giving a block or an array of modules.
2229
2312
 
2230
2313
  ```ruby
2231
2314
  module StatusHelpers
@@ -2464,11 +2547,36 @@ end
2464
2547
  API.recognize_path '/statuses'
2465
2548
  ```
2466
2549
 
2550
+ Since version `2.1.0`, the `recognize_path` method takes into account the parameters type to determine which endpoint should match with given path.
2551
+
2552
+ ```ruby
2553
+ class Books < Grape::API
2554
+ resource :books do
2555
+ route_param :id, type: Integer do
2556
+ # GET /books/:id
2557
+ get do
2558
+ #...
2559
+ end
2560
+ end
2561
+
2562
+ resource :share do
2563
+ # POST /books/share
2564
+ post do
2565
+ # ....
2566
+ end
2567
+ end
2568
+ end
2569
+ end
2570
+
2571
+ API.recognize_path '/books/1' # => /books/:id
2572
+ API.recognize_path '/books/share' # => /books/share
2573
+ API.recognize_path '/books/other' # => nil
2574
+ ```
2575
+
2576
+
2467
2577
  ## Allowed Methods
2468
2578
 
2469
- When you add a `GET` route for a resource, a route for the `HEAD`
2470
- method will also be added automatically. You can disable this
2471
- behavior with `do_not_route_head!`.
2579
+ When you add a `GET` route for a resource, a route for the `HEAD` method will also be added automatically. You can disable this behavior with `do_not_route_head!`.
2472
2580
 
2473
2581
  ``` ruby
2474
2582
  class API < Grape::API
@@ -2480,11 +2588,7 @@ class API < Grape::API
2480
2588
  end
2481
2589
  ```
2482
2590
 
2483
- When you add a route for a resource, a route for the `OPTIONS`
2484
- method will also be added. The response to an OPTIONS request will
2485
- include an "Allow" header listing the supported methods. If the resource
2486
- has `before` and `after` callbacks they will be executed, but no other callbacks will
2487
- run.
2591
+ When you add a route for a resource, a route for the `OPTIONS` method will also be added. The response to an OPTIONS request will include an "Allow" header listing the supported methods. If the resource has `before` and `after` callbacks they will be executed, but no other callbacks will run.
2488
2592
 
2489
2593
  ```ruby
2490
2594
  class API < Grape::API
@@ -2513,10 +2617,7 @@ curl -v -X OPTIONS http://localhost:3000/rt_count
2513
2617
 
2514
2618
  You can disable this behavior with `do_not_route_options!`.
2515
2619
 
2516
- If a request for a resource is made with an unsupported HTTP method, an
2517
- HTTP 405 (Method Not Allowed) response will be returned. If the resource
2518
- has `before` callbacks they will be executed, but no other callbacks will
2519
- run.
2620
+ If a request for a resource is made with an unsupported HTTP method, an HTTP 405 (Method Not Allowed) response will be returned. If the resource has `before` callbacks they will be executed, but no other callbacks will run.
2520
2621
 
2521
2622
  ``` shell
2522
2623
  curl -X DELETE -v http://localhost:3000/rt_count/
@@ -2542,8 +2643,7 @@ Anything that responds to `#to_s` can be given as a first argument to `error!`.
2542
2643
  error! :not_found, 404
2543
2644
  ```
2544
2645
 
2545
- You can also return JSON formatted objects by raising error! and passing a hash
2546
- instead of a message.
2646
+ You can also return JSON formatted objects by raising error! and passing a hash instead of a message.
2547
2647
 
2548
2648
  ```ruby
2549
2649
  error!({ error: 'unexpected error', detail: 'missing widget' }, 500)
@@ -2608,8 +2708,7 @@ route :any, '*path' do
2608
2708
  end
2609
2709
  ```
2610
2710
 
2611
- It is very crucial to __define this endpoint at the very end of your API__, as it
2612
- literally accepts every request.
2711
+ It is very crucial to __define this endpoint at the very end of your API__, as it literally accepts every request.
2613
2712
 
2614
2713
  ## Exception Handling
2615
2714
 
@@ -2851,33 +2950,11 @@ Any exception that is not subclass of `StandardError` should be rescued explicit
2851
2950
  Usually it is not a case for an application logic as such errors point to problems in Ruby runtime.
2852
2951
  This is following [standard recommendations for exceptions handling](https://ruby-doc.org/core/Exception.html).
2853
2952
 
2854
- ### Rails 3.x
2855
-
2856
- When mounted inside containers, such as Rails 3.x, errors such as "404 Not Found" or
2857
- "406 Not Acceptable" will likely be handled and rendered by Rails handlers. For instance,
2858
- accessing a nonexistent route "/api/foo" raises a 404, which inside rails will ultimately
2859
- be translated to an `ActionController::RoutingError`, which most likely will get rendered
2860
- to a HTML error page.
2861
-
2862
- Most APIs will enjoy preventing downstream handlers from handling errors. You may set the
2863
- `:cascade` option to `false` for the entire API or separately on specific `version` definitions,
2864
- which will remove the `X-Cascade: true` header from API responses.
2865
-
2866
- ```ruby
2867
- cascade false
2868
- ```
2869
-
2870
- ```ruby
2871
- version 'v1', using: :header, vendor: 'twitter', cascade: false
2872
- ```
2873
-
2874
2953
  ## Logging
2875
2954
 
2876
- `Grape::API` provides a `logger` method which by default will return an instance of the `Logger`
2877
- class from Ruby's standard library.
2955
+ `Grape::API` provides a `logger` method which by default will return an instance of the `Logger` class from Ruby's standard library.
2878
2956
 
2879
- To log messages from within an endpoint, you need to define a helper to make the logger
2880
- available in the endpoint context.
2957
+ To log messages from within an endpoint, you need to define a helper to make the logger available in the endpoint context.
2881
2958
 
2882
2959
  ```ruby
2883
2960
  class API < Grape::API
@@ -2926,9 +3003,7 @@ For similar to Rails request logging try the [grape_logging](https://github.com/
2926
3003
 
2927
3004
  ## API Formats
2928
3005
 
2929
- Your API can declare which content-types to support by using `content_type`. If you do not specify any, Grape will support
2930
- _XML_, _JSON_, _BINARY_, and _TXT_ content-types. The default format is `:txt`; you can change this with `default_format`.
2931
- Essentially, the two APIs below are equivalent.
3006
+ Your API can declare which content-types to support by using `content_type`. If you do not specify any, Grape will support _XML_, _JSON_, _BINARY_, and _TXT_ content-types. The default format is `:txt`; you can change this with `default_format`. Essentially, the two APIs below are equivalent.
2932
3007
 
2933
3008
  ```ruby
2934
3009
  class Twitter::API < Grape::API
@@ -2947,9 +3022,7 @@ class Twitter::API < Grape::API
2947
3022
  end
2948
3023
  ```
2949
3024
 
2950
- If you declare any `content_type` whatsoever, the Grape defaults will be overridden. For example, the following API will only
2951
- support the `:xml` and `:rss` content-types, but not `:txt`, `:json`, or `:binary`. Importantly, this means the `:txt`
2952
- default format is not supported! So, make sure to set a new `default_format`.
3025
+ If you declare any `content_type` whatsoever, the Grape defaults will be overridden. For example, the following API will only support the `:xml` and `:rss` content-types, but not `:txt`, `:json`, or `:binary`. Importantly, this means the `:txt` default format is not supported! So, make sure to set a new `default_format`.
2953
3026
 
2954
3027
  ```ruby
2955
3028
  class Twitter::API < Grape::API
@@ -2960,8 +3033,7 @@ class Twitter::API < Grape::API
2960
3033
  end
2961
3034
  ```
2962
3035
 
2963
- Serialization takes place automatically. For example, you do not have to call `to_json` in each JSON API endpoint
2964
- implementation. The response format (and thus the automatic serialization) is determined in the following order:
3036
+ Serialization takes place automatically. For example, you do not have to call `to_json` in each JSON API endpoint implementation. The response format (and thus the automatic serialization) is determined in the following order:
2965
3037
  * Use the file extension, if specified. If the file is .json, choose the JSON format.
2966
3038
  * Use the value of the `format` parameter in the query string, if specified.
2967
3039
  * Use the format set by the `format` option, if specified.
@@ -2984,18 +3056,13 @@ class MultipleFormatAPI < Grape::API
2984
3056
  end
2985
3057
  ```
2986
3058
 
2987
- * `GET /hello` (with an `Accept: */*` header) does not have an extension or a `format` parameter, so it will respond with
2988
- JSON (the default format).
3059
+ * `GET /hello` (with an `Accept: */*` header) does not have an extension or a `format` parameter, so it will respond with JSON (the default format).
2989
3060
  * `GET /hello.xml` has a recognized extension, so it will respond with XML.
2990
3061
  * `GET /hello?format=xml` has a recognized `format` parameter, so it will respond with XML.
2991
- * `GET /hello.xml?format=json` has a recognized extension (which takes precedence over the `format` parameter), so it will
2992
- respond with XML.
2993
- * `GET /hello.xls` (with an `Accept: */*` header) has an extension, but that extension is not recognized, so it will respond
2994
- with JSON (the default format).
2995
- * `GET /hello.xls` with an `Accept: application/xml` header has an unrecognized extension, but the `Accept` header
2996
- corresponds to a recognized format, so it will respond with XML.
2997
- * `GET /hello.xls` with an `Accept: text/plain` header has an unrecognized extension *and* an unrecognized `Accept` header,
2998
- so it will respond with JSON (the default format).
3062
+ * `GET /hello.xml?format=json` has a recognized extension (which takes precedence over the `format` parameter), so it will respond with XML.
3063
+ * `GET /hello.xls` (with an `Accept: */*` header) has an extension, but that extension is not recognized, so it will respond with JSON (the default format).
3064
+ * `GET /hello.xls` with an `Accept: application/xml` header has an unrecognized extension, but the `Accept` header corresponds to a recognized format, so it will respond with XML.
3065
+ * `GET /hello.xls` with an `Accept: text/plain` header has an unrecognized extension *and* an unrecognized `Accept` header, so it will respond with JSON (the default format).
2999
3066
 
3000
3067
  You can override this process explicitly by specifying `env['api.format']` in the API itself.
3001
3068
  For example, the following API will let you upload arbitrary files and return their contents as an attachment with the correct MIME type.
@@ -3012,8 +3079,7 @@ class Twitter::API < Grape::API
3012
3079
  end
3013
3080
  ```
3014
3081
 
3015
- You can have your API only respond to a single format with `format`. If you use this, the API will **not** respond to file
3016
- extensions other than specified in `format`. For example, consider the following API.
3082
+ You can have your API only respond to a single format with `format`. If you use this, the API will **not** respond to file extensions other than specified in `format`. For example, consider the following API.
3017
3083
 
3018
3084
  ```ruby
3019
3085
  class SingleFormatAPI < Grape::API
@@ -3028,14 +3094,10 @@ end
3028
3094
  * `GET /hello` will respond with JSON.
3029
3095
  * `GET /hello.json` will respond with JSON.
3030
3096
  * `GET /hello.xml`, `GET /hello.foobar`, or *any* other extension will respond with an HTTP 404 error code.
3031
- * `GET /hello?format=xml` will respond with an HTTP 406 error code, because the XML format specified by the request parameter
3032
- is not supported.
3033
- * `GET /hello` with an `Accept: application/xml` header will still respond with JSON, since it could not negotiate a
3034
- recognized content-type from the headers and JSON is the effective default.
3097
+ * `GET /hello?format=xml` will respond with an HTTP 406 error code, because the XML format specified by the request parameter is not supported.
3098
+ * `GET /hello` with an `Accept: application/xml` header will still respond with JSON, since it could not negotiate a recognized content-type from the headers and JSON is the effective default.
3035
3099
 
3036
- The formats apply to parsing, too. The following API will only respond to the JSON content-type and will not parse any other
3037
- input than `application/json`, `application/x-www-form-urlencoded`, `multipart/form-data`, `multipart/related` and
3038
- `multipart/mixed`. All other requests will fail with an HTTP 406 error code.
3100
+ The formats apply to parsing, too. The following API will only respond to the JSON content-type and will not parse any other input than `application/json`, `application/x-www-form-urlencoded`, `multipart/form-data`, `multipart/related` and `multipart/mixed`. All other requests will fail with an HTTP 406 error code.
3039
3101
 
3040
3102
  ```ruby
3041
3103
  class Twitter::API < Grape::API
@@ -3091,23 +3153,18 @@ end
3091
3153
  Built-in formatters are the following.
3092
3154
 
3093
3155
  * `:json`: use object's `to_json` when available, otherwise call `MultiJson.dump`
3094
- * `:xml`: use object's `to_xml` when available, usually via `MultiXml`, otherwise call `to_s`
3156
+ * `:xml`: use object's `to_xml` when available, usually via `MultiXml`
3095
3157
  * `:txt`: use object's `to_txt` when available, otherwise `to_s`
3096
3158
  * `:serializable_hash`: use object's `serializable_hash` when available, otherwise fallback to `:json`
3097
3159
  * `:binary`: data will be returned "as is"
3098
3160
 
3099
- If a body is present in a request to an API, with a Content-Type header value that is of an unsupported type a
3100
- "415 Unsupported Media Type" error code will be returned by Grape.
3161
+ If a body is present in a request to an API, with a Content-Type header value that is of an unsupported type a "415 Unsupported Media Type" error code will be returned by Grape.
3101
3162
 
3102
- Response statuses that indicate no content as defined by [Rack](https://github.com/rack)
3103
- [here](https://github.com/rack/rack/blob/master/lib/rack/utils.rb#L567)
3104
- will bypass serialization and the body entity - though there should be none -
3105
- will not be modified.
3163
+ Response statuses that indicate no content as defined by [Rack](https://github.com/rack) [here](https://github.com/rack/rack/blob/master/lib/rack/utils.rb#L567) will bypass serialization and the body entity - though there should be none - will not be modified.
3106
3164
 
3107
3165
  ### JSONP
3108
3166
 
3109
- Grape supports JSONP via [Rack::JSONP](https://github.com/rack/rack-contrib), part of the
3110
- [rack-contrib](https://github.com/rack/rack-contrib) gem. Add `rack-contrib` to your `Gemfile`.
3167
+ Grape supports JSONP via [Rack::JSONP](https://github.com/rack/rack-contrib), part of the [rack-contrib](https://github.com/rack/rack-contrib) gem. Add `rack-contrib` to your `Gemfile`.
3111
3168
 
3112
3169
  ```ruby
3113
3170
  require 'rack/contrib'
@@ -3123,9 +3180,7 @@ end
3123
3180
 
3124
3181
  ### CORS
3125
3182
 
3126
- Grape supports CORS via [Rack::CORS](https://github.com/cyu/rack-cors), part of the
3127
- [rack-cors](https://github.com/cyu/rack-cors) gem. Add `rack-cors` to your `Gemfile`,
3128
- then use the middleware in your config.ru file.
3183
+ Grape supports CORS via [Rack::CORS](https://github.com/cyu/rack-cors), part of the [rack-cors](https://github.com/cyu/rack-cors) gem. Add `rack-cors` to your `Gemfile`, then use the middleware in your config.ru file.
3129
3184
 
3130
3185
  ```ruby
3131
3186
  require 'rack/cors'
@@ -3143,8 +3198,7 @@ run Twitter::API
3143
3198
 
3144
3199
  ## Content-type
3145
3200
 
3146
- Content-type is set by the formatter. You can override the content-type of the response at runtime
3147
- by setting the `Content-Type` header.
3201
+ Content-type is set by the formatter. You can override the content-type of the response at runtime by setting the `Content-Type` header.
3148
3202
 
3149
3203
  ```ruby
3150
3204
  class API < Grape::API
@@ -3157,16 +3211,12 @@ end
3157
3211
 
3158
3212
  ## API Data Formats
3159
3213
 
3160
- Grape accepts and parses input data sent with the POST and PUT methods as described in the Parameters
3161
- section above. It also supports custom data formats. You must declare additional content-types via
3162
- `content_type` and optionally supply a parser via `parser` unless a parser is already available within
3163
- Grape to enable a custom format. Such a parser can be a function or a class.
3214
+ Grape accepts and parses input data sent with the POST and PUT methods as described in the Parameters section above. It also supports custom data formats. You must declare additional content-types via `content_type` and optionally supply a parser via `parser` unless a parser is already available within Grape to enable a custom format. Such a parser can be a function or a class.
3164
3215
 
3165
3216
  With a parser, parsed data is available "as-is" in `env['api.request.body']`.
3166
3217
  Without a parser, data is available "as-is" and in `env['api.request.input']`.
3167
3218
 
3168
- The following example is a trivial parser that will assign any input with the "text/custom" content-type
3169
- to `:value`. The parameter will be available via `params[:value]` inside the API call.
3219
+ The following example is a trivial parser that will assign any input with the "text/custom" content-type to `:value`. The parameter will be available via `params[:value]` inside the API call.
3170
3220
 
3171
3221
  ```ruby
3172
3222
  module CustomParser
@@ -3200,9 +3250,7 @@ Grape uses `JSON` and `ActiveSupport::XmlMini` for JSON and XML parsing by defau
3200
3250
 
3201
3251
  ## RESTful Model Representations
3202
3252
 
3203
- Grape supports a range of ways to present your data with some help from a generic `present` method,
3204
- which accepts two arguments: the object to be presented and the options associated with it. The options
3205
- hash may include `:with`, which defines the entity to expose.
3253
+ Grape supports a range of ways to present your data with some help from a generic `present` method, which accepts two arguments: the object to be presented and the options associated with it. The options hash may include `:with`, which defines the entity to expose.
3206
3254
 
3207
3255
  ### Grape Entities
3208
3256
 
@@ -3281,8 +3329,7 @@ The response will be
3281
3329
  }
3282
3330
  ```
3283
3331
 
3284
- In addition to separately organizing entities, it may be useful to put them as namespaced
3285
- classes underneath the model they represent.
3332
+ In addition to separately organizing entities, it may be useful to put them as namespaced classes underneath the model they represent.
3286
3333
 
3287
3334
  ```ruby
3288
3335
  class Status
@@ -3296,11 +3343,7 @@ class Status
3296
3343
  end
3297
3344
  ```
3298
3345
 
3299
- If you organize your entities this way, Grape will automatically detect the `Entity` class and
3300
- use it to present your models. In this example, if you added `present Status.new` to your endpoint,
3301
- Grape will automatically detect that there is a `Status::Entity` class and use that as the
3302
- representative entity. This can still be overridden by using the `:with` option or an explicit
3303
- `represents` call.
3346
+ If you organize your entities this way, Grape will automatically detect the `Entity` class and use it to present your models. In this example, if you added `present Status.new` to your endpoint, Grape will automatically detect that there is a `Status::Entity` class and use that as the representative entity. This can still be overridden by using the `:with` option or an explicit `represents` call.
3304
3347
 
3305
3348
  You can present `hash` with `Grape::Presenters::Presenter` to keep things consistent.
3306
3349
 
@@ -3333,15 +3376,11 @@ You can use [Roar](https://github.com/apotonick/roar) to render HAL or Collectio
3333
3376
 
3334
3377
  ### Rabl
3335
3378
 
3336
- You can use [Rabl](https://github.com/nesquena/rabl) templates with the help of the
3337
- [grape-rabl](https://github.com/ruby-grape/grape-rabl) gem, which defines a custom Grape Rabl
3338
- formatter.
3379
+ You can use [Rabl](https://github.com/nesquena/rabl) templates with the help of the [grape-rabl](https://github.com/ruby-grape/grape-rabl) gem, which defines a custom Grape Rabl formatter.
3339
3380
 
3340
3381
  ### Active Model Serializers
3341
3382
 
3342
- You can use [Active Model Serializers](https://github.com/rails-api/active_model_serializers) serializers with the help of the
3343
- [grape-active_model_serializers](https://github.com/jrhe/grape-active_model_serializers) gem, which defines a custom Grape AMS
3344
- formatter.
3383
+ You can use [Active Model Serializers](https://github.com/rails-api/active_model_serializers) serializers with the help of the [grape-active_model_serializers](https://github.com/jrhe/grape-active_model_serializers) gem, which defines a custom Grape AMS formatter.
3345
3384
 
3346
3385
  ## Sending Raw or No Data
3347
3386
 
@@ -3381,9 +3420,7 @@ class API < Grape::API
3381
3420
  end
3382
3421
  ```
3383
3422
 
3384
- You can also set the response to a file with `sendfile`. This works with the
3385
- [Rack::Sendfile](https://www.rubydoc.info/gems/rack/Rack/Sendfile) middleware to optimally send
3386
- the file through your web server software.
3423
+ You can also set the response to a file with `sendfile`. This works with the [Rack::Sendfile](https://www.rubydoc.info/gems/rack/Rack/Sendfile) middleware to optimally send the file through your web server software.
3387
3424
 
3388
3425
  ```ruby
3389
3426
  class API < Grape::API
@@ -3427,9 +3464,7 @@ end
3427
3464
 
3428
3465
  ### Basic Auth
3429
3466
 
3430
- Grape has built-in Basic authentication (the given `block`
3431
- is executed in the context of the current `Endpoint`). Authentication
3432
- applies to the current namespace and any children, but not parents.
3467
+ Grape has built-in Basic authentication (the given `block` is executed in the context of the current `Endpoint`). Authentication applies to the current namespace and any children, but not parents.
3433
3468
 
3434
3469
  ```ruby
3435
3470
  http_basic do |username, password|
@@ -3440,16 +3475,13 @@ end
3440
3475
 
3441
3476
  ### Register custom middleware for authentication
3442
3477
 
3443
- Grape can use custom Middleware for authentication. How to implement these
3444
- Middleware have a look at `Rack::Auth::Basic` or similar implementations.
3445
-
3478
+ Grape can use custom Middleware for authentication. How to implement these Middleware have a look at `Rack::Auth::Basic` or similar implementations.
3446
3479
 
3447
3480
  For registering a Middleware you need the following options:
3448
3481
 
3449
3482
  * `label` - the name for your authenticator to use it later
3450
3483
  * `MiddlewareClass` - the MiddlewareClass to use for authentication
3451
- * `option_lookup_proc` - A Proc with one Argument to lookup the options at
3452
- runtime (return value is an `Array` as Parameter for the Middleware).
3484
+ * `option_lookup_proc` - A Proc with one Argument to lookup the options at runtime (return value is an `Array` as Parameter for the Middleware).
3453
3485
 
3454
3486
  Example:
3455
3487
 
@@ -3473,7 +3505,7 @@ You can access the controller params, headers, and helpers through the context w
3473
3505
 
3474
3506
  Grape routes can be reflected at runtime. This can notably be useful for generating documentation.
3475
3507
 
3476
- Grape exposes arrays of API versions and compiled routes. Each route contains a `route_prefix`, `route_version`, `route_namespace`, `route_method`, `route_path` and `route_params`. You can add custom route settings to the route metadata with `route_setting`.
3508
+ Grape exposes arrays of API versions and compiled routes. Each route contains a `prefix`, `version`, `namespace`, `method` and `params`. You can add custom route settings to the route metadata with `route_setting`.
3477
3509
 
3478
3510
  ```ruby
3479
3511
  class TwitterAPI < Grape::API
@@ -3496,7 +3528,7 @@ TwitterAPI::routes[0].description # => 'Includes custom settings.'
3496
3528
  TwitterAPI::routes[0].settings[:custom] # => { key: 'value' }
3497
3529
  ```
3498
3530
 
3499
- Note that `Route#route_xyz` methods have been deprecated since 0.15.0.
3531
+ Note that `Route#route_xyz` methods have been deprecated since 0.15.0 and removed since 2.0.1.
3500
3532
 
3501
3533
  Please use `Route#xyz` instead.
3502
3534
 
@@ -3516,15 +3548,12 @@ class MyAPI < Grape::API
3516
3548
  requires :id, type: Integer, desc: 'Identity.'
3517
3549
  end
3518
3550
  get 'params/:id' do
3519
- route.route_params[params[:id]] # yields the parameter description
3551
+ route.params[params[:id]] # yields the parameter description
3520
3552
  end
3521
3553
  end
3522
3554
  ```
3523
3555
 
3524
- The current endpoint responding to the request is `self` within the API block
3525
- or `env['api.endpoint']` elsewhere. The endpoint has some interesting properties,
3526
- such as `source` which gives you access to the original code block of the API
3527
- implementation. This can be particularly useful for building a logger middleware.
3556
+ The current endpoint responding to the request is `self` within the API block or `env['api.endpoint']` elsewhere. The endpoint has some interesting properties, such as `source` which gives you access to the original code block of the API implementation. This can be particularly useful for building a logger middleware.
3528
3557
 
3529
3558
  ```ruby
3530
3559
  class ApiLogger < Grape::Middleware::Base
@@ -3538,10 +3567,8 @@ end
3538
3567
 
3539
3568
  ## Before, After and Finally
3540
3569
 
3541
- Blocks can be executed before or after every API call, using `before`, `after`,
3542
- `before_validation` and `after_validation`.
3543
- If the API fails the `after` call will not be triggered, if you need code to execute for sure
3544
- use the `finally`.
3570
+ Blocks can be executed before or after every API call, using `before`, `after`, `before_validation` and `after_validation`.
3571
+ If the API fails the `after` call will not be triggered, if you need code to execute for sure use the `finally`.
3545
3572
 
3546
3573
  Before and after callbacks execute in the following order:
3547
3574
 
@@ -3555,13 +3582,9 @@ Before and after callbacks execute in the following order:
3555
3582
 
3556
3583
  Steps 4, 5 and 6 only happen if validation succeeds.
3557
3584
 
3558
- If a request for a resource is made with an unsupported HTTP method (returning
3559
- HTTP 405) only `before` callbacks will be executed. The remaining callbacks will
3560
- be bypassed.
3585
+ If a request for a resource is made with an unsupported HTTP method (returning HTTP 405) only `before` callbacks will be executed. The remaining callbacks will be bypassed.
3561
3586
 
3562
- If a request for a resource is made that triggers the built-in `OPTIONS` handler,
3563
- only `before` and `after` callbacks will be executed. The remaining callbacks will
3564
- be bypassed.
3587
+ If a request for a resource is made that triggers the built-in `OPTIONS` handler, only `before` and `after` callbacks will be executed. The remaining callbacks will be bypassed.
3565
3588
 
3566
3589
  For example, using a simple `before` block to set a header.
3567
3590
 
@@ -3706,11 +3729,7 @@ Instead of altering a response, you can also terminate and rewrite it from any c
3706
3729
 
3707
3730
  ## Anchoring
3708
3731
 
3709
- Grape by default anchors all request paths, which means that the request URL
3710
- should match from start to end to match, otherwise a `404 Not Found` is
3711
- returned. However, this is sometimes not what you want, because it is not always
3712
- known upfront what can be expected from the call. This is because Rack-mount by
3713
- default anchors requests to match from the start to the end, or not at all.
3732
+ Grape by default anchors all request paths, which means that the request URL should match from start to end to match, otherwise a `404 Not Found` is returned. However, this is sometimes not what you want, because it is not always known upfront what can be expected from the call. This is because Rack-mount by default anchors requests to match from the start to the end, or not at all.
3714
3733
  Rails solves this problem by using a `anchor: false` option in your routes.
3715
3734
  In Grape this option can be used as well when a method is defined.
3716
3735
 
@@ -3726,12 +3745,44 @@ class TwitterAPI < Grape::API
3726
3745
  end
3727
3746
  ```
3728
3747
 
3729
- This will match all paths starting with '/statuses/'. There is one caveat though:
3730
- the `params[:status]` parameter only holds the first part of the request url.
3731
- Luckily this can be circumvented by using the described above syntax for path
3732
- specification and using the `PATH_INFO` Rack environment variable, using
3733
- `env['PATH_INFO']`. This will hold everything that comes after the '/statuses/'
3734
- part.
3748
+ This will match all paths starting with '/statuses/'. There is one caveat though: the `params[:status]` parameter only holds the first part of the request url.
3749
+ Luckily this can be circumvented by using the described above syntax for path specification and using the `PATH_INFO` Rack environment variable, using `env['PATH_INFO']`. This will hold everything that comes after the '/statuses/' part.
3750
+
3751
+ ## Instance Variables
3752
+
3753
+ You can use instance variables to pass information across the various stages of a request. An instance variable set within a `before` validator is accessible within the endpoint's code and can also be utilized within the `rescue_from` handler.
3754
+
3755
+ ```ruby
3756
+ class TwitterAPI < Grape::API
3757
+ before do
3758
+ @var = 1
3759
+ end
3760
+
3761
+ get '/' do
3762
+ puts @var # => 1
3763
+ raise
3764
+ end
3765
+
3766
+ rescue_from :all do
3767
+ puts @var # => 1
3768
+ end
3769
+ end
3770
+ ```
3771
+
3772
+ The values of instance variables cannot be shared among various endpoints within the same API. This limitation arises due to Grape generating a new instance for each request made. Consequently, instance variables set within an endpoint during one request differ from those set during a subsequent request, as they exist within separate instances.
3773
+
3774
+ ```ruby
3775
+ class TwitterAPI < Grape::API
3776
+ get '/first' do
3777
+ @var = 1
3778
+ puts @var # => 1
3779
+ end
3780
+
3781
+ get '/second' do
3782
+ puts @var # => nil
3783
+ end
3784
+ end
3785
+ ```
3735
3786
 
3736
3787
  ## Using Custom Middleware
3737
3788
 
@@ -3940,8 +3991,7 @@ describe Twitter::API do
3940
3991
  end
3941
3992
  ```
3942
3993
 
3943
- In Rails, HTTP request tests would go into the `spec/requests` group. You may want your API code to go into
3944
- `app/api` - you can match that layout under `spec` by adding the following in `spec/rails_helper.rb`.
3994
+ In Rails, HTTP request tests would go into the `spec/requests` group. You may want your API code to go into `app/api` - you can match that layout under `spec` by adding the following in `spec/rails_helper.rb`.
3945
3995
 
3946
3996
  ```ruby
3947
3997
  RSpec.configure do |config|
@@ -3975,10 +4025,7 @@ end
3975
4025
 
3976
4026
  ### Stubbing Helpers
3977
4027
 
3978
- Because helpers are mixed in based on the context when an endpoint is defined, it can
3979
- be difficult to stub or mock them for testing. The `Grape::Endpoint.before_each` method
3980
- can help by allowing you to define behavior on the endpoint that will run before every
3981
- request.
4028
+ Because helpers are mixed in based on the context when an endpoint is defined, it can be difficult to stub or mock them for testing. The `Grape::Endpoint.before_each` method can help by allowing you to define behavior on the endpoint that will run before every request.
3982
4029
 
3983
4030
  ```ruby
3984
4031
  describe 'an endpoint that needs helpers stubbed' do
@@ -4104,8 +4151,7 @@ Grape integrates with following third-party tools:
4104
4151
 
4105
4152
  ## Contributing to Grape
4106
4153
 
4107
- Grape is work of hundreds of contributors. You're encouraged to submit pull requests, propose
4108
- features and discuss issues.
4154
+ Grape is work of hundreds of contributors. You're encouraged to submit pull requests, propose features and discuss issues.
4109
4155
 
4110
4156
  See [CONTRIBUTING](CONTRIBUTING.md).
4111
4157