grape 1.3.0 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +119 -1
  3. data/LICENSE +1 -1
  4. data/README.md +123 -29
  5. data/UPGRADING.md +265 -39
  6. data/lib/grape/api/instance.rb +32 -31
  7. data/lib/grape/api.rb +5 -5
  8. data/lib/grape/content_types.rb +34 -0
  9. data/lib/grape/dsl/callbacks.rb +1 -1
  10. data/lib/grape/dsl/helpers.rb +2 -1
  11. data/lib/grape/dsl/inside_route.rb +77 -43
  12. data/lib/grape/dsl/parameters.rb +12 -8
  13. data/lib/grape/dsl/routing.rb +12 -11
  14. data/lib/grape/dsl/validations.rb +18 -1
  15. data/lib/grape/eager_load.rb +1 -1
  16. data/lib/grape/endpoint.rb +8 -6
  17. data/lib/grape/exceptions/base.rb +0 -4
  18. data/lib/grape/exceptions/validation.rb +1 -1
  19. data/lib/grape/exceptions/validation_errors.rb +12 -13
  20. data/lib/grape/http/headers.rb +26 -0
  21. data/lib/grape/middleware/auth/base.rb +3 -3
  22. data/lib/grape/middleware/base.rb +4 -5
  23. data/lib/grape/middleware/error.rb +11 -13
  24. data/lib/grape/middleware/formatter.rb +3 -3
  25. data/lib/grape/middleware/stack.rb +10 -2
  26. data/lib/grape/middleware/versioner/header.rb +4 -4
  27. data/lib/grape/middleware/versioner/parse_media_type_patch.rb +2 -1
  28. data/lib/grape/middleware/versioner/path.rb +1 -1
  29. data/lib/grape/namespace.rb +12 -2
  30. data/lib/grape/path.rb +13 -3
  31. data/lib/grape/request.rb +13 -8
  32. data/lib/grape/router/attribute_translator.rb +26 -5
  33. data/lib/grape/router/pattern.rb +17 -16
  34. data/lib/grape/router/route.rb +5 -24
  35. data/lib/grape/router.rb +26 -30
  36. data/lib/grape/{serve_file → serve_stream}/file_body.rb +1 -1
  37. data/lib/grape/{serve_file → serve_stream}/sendfile_response.rb +1 -1
  38. data/lib/grape/{serve_file/file_response.rb → serve_stream/stream_response.rb} +8 -8
  39. data/lib/grape/util/base_inheritable.rb +15 -8
  40. data/lib/grape/util/cache.rb +20 -0
  41. data/lib/grape/util/lazy_object.rb +43 -0
  42. data/lib/grape/util/lazy_value.rb +1 -0
  43. data/lib/grape/util/reverse_stackable_values.rb +2 -0
  44. data/lib/grape/util/stackable_values.rb +7 -20
  45. data/lib/grape/validations/attributes_iterator.rb +8 -0
  46. data/lib/grape/validations/multiple_attributes_iterator.rb +1 -1
  47. data/lib/grape/validations/params_scope.rb +10 -8
  48. data/lib/grape/validations/single_attribute_iterator.rb +1 -1
  49. data/lib/grape/validations/types/array_coercer.rb +14 -5
  50. data/lib/grape/validations/types/build_coercer.rb +5 -8
  51. data/lib/grape/validations/types/custom_type_coercer.rb +16 -2
  52. data/lib/grape/validations/types/dry_type_coercer.rb +36 -1
  53. data/lib/grape/validations/types/file.rb +15 -12
  54. data/lib/grape/validations/types/invalid_value.rb +24 -0
  55. data/lib/grape/validations/types/json.rb +40 -36
  56. data/lib/grape/validations/types/primitive_coercer.rb +15 -6
  57. data/lib/grape/validations/types/set_coercer.rb +6 -4
  58. data/lib/grape/validations/types/variant_collection_coercer.rb +1 -1
  59. data/lib/grape/validations/types.rb +7 -9
  60. data/lib/grape/validations/validator_factory.rb +1 -1
  61. data/lib/grape/validations/validators/as.rb +1 -1
  62. data/lib/grape/validations/validators/base.rb +8 -8
  63. data/lib/grape/validations/validators/coerce.rb +11 -15
  64. data/lib/grape/validations/validators/default.rb +3 -5
  65. data/lib/grape/validations/validators/exactly_one_of.rb +4 -2
  66. data/lib/grape/validations/validators/except_values.rb +1 -1
  67. data/lib/grape/validations/validators/multiple_params_base.rb +2 -1
  68. data/lib/grape/validations/validators/regexp.rb +1 -1
  69. data/lib/grape/validations/validators/values.rb +1 -1
  70. data/lib/grape/version.rb +1 -1
  71. data/lib/grape.rb +5 -5
  72. data/spec/grape/api/instance_spec.rb +50 -0
  73. data/spec/grape/api_remount_spec.rb +9 -4
  74. data/spec/grape/api_spec.rb +82 -6
  75. data/spec/grape/dsl/inside_route_spec.rb +182 -33
  76. data/spec/grape/endpoint/declared_spec.rb +601 -0
  77. data/spec/grape/endpoint_spec.rb +0 -521
  78. data/spec/grape/entity_spec.rb +7 -1
  79. data/spec/grape/exceptions/validation_errors_spec.rb +2 -2
  80. data/spec/grape/integration/rack_sendfile_spec.rb +12 -8
  81. data/spec/grape/middleware/auth/strategies_spec.rb +1 -1
  82. data/spec/grape/middleware/error_spec.rb +1 -1
  83. data/spec/grape/middleware/formatter_spec.rb +3 -3
  84. data/spec/grape/middleware/stack_spec.rb +10 -0
  85. data/spec/grape/path_spec.rb +4 -4
  86. data/spec/grape/request_spec.rb +1 -1
  87. data/spec/grape/validations/instance_behaivour_spec.rb +1 -1
  88. data/spec/grape/validations/multiple_attributes_iterator_spec.rb +13 -3
  89. data/spec/grape/validations/params_scope_spec.rb +26 -0
  90. data/spec/grape/validations/single_attribute_iterator_spec.rb +17 -6
  91. data/spec/grape/validations/types/array_coercer_spec.rb +35 -0
  92. data/spec/grape/validations/types/primitive_coercer_spec.rb +135 -0
  93. data/spec/grape/validations/types/set_coercer_spec.rb +34 -0
  94. data/spec/grape/validations/types_spec.rb +1 -1
  95. data/spec/grape/validations/validators/coerce_spec.rb +366 -86
  96. data/spec/grape/validations/validators/default_spec.rb +170 -0
  97. data/spec/grape/validations/validators/exactly_one_of_spec.rb +12 -12
  98. data/spec/grape/validations/validators/except_values_spec.rb +1 -0
  99. data/spec/grape/validations/validators/values_spec.rb +1 -1
  100. data/spec/grape/validations_spec.rb +298 -30
  101. data/spec/integration/eager_load/eager_load_spec.rb +15 -0
  102. data/spec/shared/versioning_examples.rb +20 -20
  103. data/spec/spec_helper.rb +3 -10
  104. data/spec/support/chunks.rb +14 -0
  105. data/spec/support/eager_load.rb +19 -0
  106. data/spec/support/versioned_helpers.rb +4 -6
  107. metadata +27 -10
  108. data/lib/grape/util/content_types.rb +0 -28
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 826e1365751c85c3f1f7d433e842a76c8a7f5dc6c4a1b13128679e4d56b2ccf5
4
- data.tar.gz: af8aef528c40764a53a3e397c306835745943a1d376ab3738bf7aa2547ef75d1
3
+ metadata.gz: ac9fdc749f4dcad72fa8baacf0b07fa0fa499552521c789dd8e84439554ce275
4
+ data.tar.gz: 075b2bc7e75e7b0240973086cdae37cd2adf10ef4a4c419dc2cccd7a2741a3cd
5
5
  SHA512:
6
- metadata.gz: 0af9a855214f1fbacd92403111aa1f4761b7257adf6ca9dbd6ab4e1cb889469b76a489971d058a58e98d3bf6906dacb67eb8a5eae11aad16782f318b3453988b
7
- data.tar.gz: 25004acd2431ab7cd174466e82ca5240b888ca045036227909ed9c8c886cd8706f1d3b696f4b1ede8eaa69c68c156fe064557afea8cdf53c9d39b136db7ae4b1
6
+ metadata.gz: b845801e8c95ceee643853fd306c1513ce0611ebd535f3e2f058e95b4c491da08f58bcbd33b8d42d2cd8f1432d619dda203f8243fcc72e2b808ec4d0e15bc81c
7
+ data.tar.gz: ccdf098f65e4331fc35eaab564fbaf8d7411d1ae6f394f4cf4e0fdfa4f4871092e60b23a8a3a60ffc95680e3287446cac343932b0ef2af6b0745b91422d1d0a4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,121 @@
1
+ ### 1.5.2 (2021/02/06)
2
+
3
+ #### Features
4
+
5
+ * [#2157](https://github.com/ruby-grape/grape/pull/2157): Custom types can set a message to be used in the response when invalid - [@dnesteryuk](https://github.com/dnesteryuk).
6
+ * [#2145](https://github.com/ruby-grape/grape/pull/2145): Ruby 3.0 compatibility - [@ericproulx](https://github.com/ericproulx).
7
+ * [#2143](https://github.com/ruby-grape/grape/pull/2143): Enable GitHub Actions with updated RuboCop and Danger - [@anakinj](https://github.com/anakinj).
8
+
9
+ #### Fixes
10
+
11
+ * [#2144](https://github.com/ruby-grape/grape/pull/2144): Fix compatibility issue with activesupport 6.1 and XML serialization of arrays - [@anakinj](https://github.com/anakinj).
12
+ * [#2137](https://github.com/ruby-grape/grape/pull/2137): Fix typos - [@johnny-miyake](https://github.com/johnny-miyake).
13
+ * [#2131](https://github.com/ruby-grape/grape/pull/2131): Fix Ruby 2.7 keyword deprecation warning in validators/coerce - [@K0H205](https://github.com/K0H205).
14
+ * [#2132](https://github.com/ruby-grape/grape/pull/2132): Use #ruby2_keywords for correct delegation on Ruby <= 2.6, 2.7 and 3 - [@eregon](https://github.com/eregon).
15
+ * [#2152](https://github.com/ruby-grape/grape/pull/2152): Fix configuration method inside namespaced params - [@fsainz](https://github.com/fsainz).
16
+
17
+ ### 1.5.1 (2020/11/15)
18
+
19
+ #### Fixes
20
+
21
+ * [#2129](https://github.com/ruby-grape/grape/pull/2129): Fix validation error when Required Array nested inside an optional array, for Multiparam validators - [@dwhenry](https://github.com/dwhenry).
22
+ * [#2128](https://github.com/ruby-grape/grape/pull/2128): Fix validation error when Required Array nested inside an optional array - [@dwhenry](https://github.com/dwhenry).
23
+ * [#2127](https://github.com/ruby-grape/grape/pull/2127): Fix a performance issue with dependent params - [@dnesteryuk](https://github.com/dnesteryuk).
24
+ * [#2126](https://github.com/ruby-grape/grape/pull/2126): Fix warnings about redefined attribute accessors in `AttributeTranslator` - [@samsonjs](https://github.com/samsonjs).
25
+ * [#2121](https://github.com/ruby-grape/grape/pull/2121): Fix 2.7 deprecation warning in validator_factory - [@Legogris](https://github.com/Legogris).
26
+ * [#2115](https://github.com/ruby-grape/grape/pull/2115): Fix declared_params regression with multiple allowed types - [@stanhu](https://github.com/stanhu).
27
+ * [#2123](https://github.com/ruby-grape/grape/pull/2123): Fix 2.7 deprecation warning in middleware/stack - [@Legogris](https://github.com/Legogris).
28
+
29
+ ### 1.5.0 (2020/10/05)
30
+
31
+ #### Fixes
32
+
33
+ * [#2104](https://github.com/ruby-grape/grape/pull/2104): Fix Ruby 2.7 keyword deprecation warning - [@stanhu](https://github.com/stanhu).
34
+ * [#2103](https://github.com/ruby-grape/grape/pull/2103): Ensure complete declared params structure is present - [@tlconnor](https://github.com/tlconnor).
35
+ * [#2099](https://github.com/ruby-grape/grape/pull/2099): Added truffleruby to Travis-CI - [@gogainda](https://github.com/gogainda).
36
+ * [#2089](https://github.com/ruby-grape/grape/pull/2089): Specify order of mounting Grape with Rack::Cascade in README - [@jonmchan](https://github.com/jonmchan).
37
+ * [#2088](https://github.com/ruby-grape/grape/pull/2088): Set `Cache-Control` header only for streamed responses - [@stanhu](https://github.com/stanhu).
38
+ * [#2092](https://github.com/ruby-grape/grape/pull/2092): Correct an example params in Include Missing doc - [@huyvohcmc](https://github.com/huyvohcmc).
39
+ * [#2091](https://github.com/ruby-grape/grape/pull/2091): Fix ruby 2.7 keyword deprecations - [@dim](https://github.com/dim).
40
+ * [#2097](https://github.com/ruby-grape/grape/pull/2097): Skip to set default value unless `meets_dependency?` - [@wanabe](https://github.com/wanabe).
41
+ * [#2096](https://github.com/ruby-grape/grape/pull/2096): Fix redundant dependency check - [@braktar](https://github.com/braktar).
42
+ * [#2096](https://github.com/ruby-grape/grape/pull/2098): Fix nested coercion - [@braktar](https://github.com/braktar).
43
+ * [#2102](https://github.com/ruby-grape/grape/pull/2102): Fix retaining setup blocks when remounting APIs - [@jylamont](https://github.com/jylamont).
44
+
45
+ ### 1.4.0 (2020/07/10)
46
+
47
+ #### Features
48
+
49
+ * [#1520](https://github.com/ruby-grape/grape/pull/1520): Un-deprecate stream-like objects - [@urkle](https://github.com/urkle).
50
+ * [#2060](https://github.com/ruby-grape/grape/pull/2060): Drop support for Ruby 2.4 - [@dblock](https://github.com/dblock).
51
+ * [#2060](https://github.com/ruby-grape/grape/pull/2060): Upgraded Rubocop to 0.84.0 - [@dblock](https://github.com/dblock).
52
+ * [#2077](https://github.com/ruby-grape/grape/pull/2077): Simplify logic for defining declared params - [@dnesteryuk](https://github.com/dnesteryuk).
53
+ * [#2076](https://github.com/ruby-grape/grape/pull/2076): Make route information available for hooks when the automatically generated endpoints are invoked - [@anakinj](https://github.com/anakinj).
54
+
55
+ #### Fixes
56
+
57
+ * [#2067](https://github.com/ruby-grape/grape/pull/2067): Coerce empty String to `nil` for all primitive types except `String` - [@petekinnecom](https://github.com/petekinnecom).
58
+ * [#2064](https://github.com/ruby-grape/grape/pull/2064): Fix Ruby 2.7 deprecation warning in `Grape::Middleware::Base#initialize` - [@skarger](https://github.com/skarger).
59
+ * [#2072](https://github.com/ruby-grape/grape/pull/2072): Fix `Grape.eager_load!` and `compile!` - [@stanhu](https://github.com/stanhu).
60
+ * [#2084](https://github.com/ruby-grape/grape/pull/2084): Fix memory leak in path normalization - [@fcheung](https://github.com/fcheung).
61
+
62
+ ### 1.3.3 (2020/05/23)
63
+
64
+ #### Features
65
+
66
+ * [#2048](https://github.com/ruby-grape/grape/issues/2034): Grape Enterprise support is now available [via TideLift](https://tidelift.com/subscription/request-a-demo?utm_source=rubygems-grape&utm_medium=referral&utm_campaign=enterprise) - [@dblock](https://github.com/dblock).
67
+ * [#2039](https://github.com/ruby-grape/grape/pull/2039): Travis - update rails versions - [@ericproulx](https://github.com/ericproulx).
68
+ * [#2038](https://github.com/ruby-grape/grape/pull/2038): Travis - update ruby versions - [@ericproulx](https://github.com/ericproulx).
69
+ * [#2050](https://github.com/ruby-grape/grape/pull/2050): Refactor route public_send to AttributeTranslator - [@ericproulx](https://github.com/ericproulx).
70
+
71
+ #### Fixes
72
+
73
+ * [#2049](https://github.com/ruby-grape/grape/pull/2049): Coerce an empty string to nil in case of the bool type - [@dnesteryuk](https://github.com/dnesteryuk).
74
+ * [#2043](https://github.com/ruby-grape/grape/pull/2043): Modify declared for nested array and hash - [@kadotami](https://github.com/kadotami).
75
+ * [#2040](https://github.com/ruby-grape/grape/pull/2040): Fix a regression with Array of type nil - [@ericproulx](https://github.com/ericproulx).
76
+ * [#2054](https://github.com/ruby-grape/grape/pull/2054): Coercing of nested arrays - [@dnesteryuk](https://github.com/dnesteryuk).
77
+ * [#2050](https://github.com/ruby-grape/grape/pull/2053): Fix broken multiple mounts - [@Jack12816](https://github.com/Jack12816).
78
+
79
+ ### 1.3.2 (2020/04/12)
80
+
81
+ #### Features
82
+
83
+ * [#2020](https://github.com/ruby-grape/grape/pull/2020): Reduce array allocation - [@ericproulx](https://github.com/ericproulx).
84
+ * [#2015](https://github.com/ruby-grape/grape/pull/2014): Reduce MatchData allocation - [@ericproulx](https://github.com/ericproulx).
85
+ * [#2014](https://github.com/ruby-grape/grape/pull/2014): Reduce total allocated arrays - [@ericproulx](https://github.com/ericproulx).
86
+ * [#2011](https://github.com/ruby-grape/grape/pull/2011): Reduce total retained regexes - [@ericproulx](https://github.com/ericproulx).
87
+
88
+ #### Fixes
89
+
90
+ * [#2033](https://github.com/ruby-grape/grape/pull/2033): Ensure `Float` params are correctly coerced to `BigDecimal` - [@tlconnor](https://github.com/tlconnor).
91
+ * [#2031](https://github.com/ruby-grape/grape/pull/2031): Fix a regression with an array of a custom type - [@dnesteryuk](https://github.com/dnesteryuk).
92
+ * [#2026](https://github.com/ruby-grape/grape/pull/2026): Fix a regression in `coerce_with` when coercion returns `nil` - [@misdoro](https://github.com/misdoro).
93
+ * [#2025](https://github.com/ruby-grape/grape/pull/2025): Fix Decimal type category - [@kdoya](https://github.com/kdoya).
94
+ * [#2019](https://github.com/ruby-grape/grape/pull/2019): Avoid coercing parameter with multiple types to an empty Array - [@stanhu](https://github.com/stanhu).
95
+
96
+ ### 1.3.1 (2020/03/11)
97
+
98
+ #### Features
99
+
100
+ * [#2005](https://github.com/ruby-grape/grape/pull/2005): Content types registrable - [@ericproulx](https://github.com/ericproulx).
101
+ * [#2003](https://github.com/ruby-grape/grape/pull/2003): Upgraded Rubocop to 0.80.1 - [@ericproulx](https://github.com/ericproulx).
102
+ * [#2002](https://github.com/ruby-grape/grape/pull/2002): Objects allocation optimization (lazy_lookup) - [@ericproulx](https://github.com/ericproulx).
103
+
104
+ #### Fixes
105
+
106
+ * [#2006](https://github.com/ruby-grape/grape/pull/2006): Fix explicit rescue StandardError - [@ericproulx](https://github.com/ericproulx).
107
+ * [#2004](https://github.com/ruby-grape/grape/pull/2004): Rubocop fixes - [@ericproulx](https://github.com/ericproulx).
108
+ * [#1995](https://github.com/ruby-grape/grape/pull/1995): Fix: "undefined instance variables" and "method redefined" warnings - [@nbeyer](https://github.com/nbeyer).
109
+ * [#1994](https://github.com/ruby-grape/grape/pull/1993): Fix typos in README - [@bellmyer](https://github.com/bellmyer).
110
+ * [#1993](https://github.com/ruby-grape/grape/pull/1993): Lazy join allow header - [@ericproulx](https://github.com/ericproulx).
111
+ * [#1987](https://github.com/ruby-grape/grape/pull/1987): Re-add exactly_one_of mutually exclusive error message - [@ZeroInputCtrl](https://github.com/ZeroInputCtrl).
112
+ * [#1977](https://github.com/ruby-grape/grape/pull/1977): Skip validation for a file if it is optional and nil - [@dnesteryuk](https://github.com/dnesteryuk).
113
+ * [#1976](https://github.com/ruby-grape/grape/pull/1976): Ensure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk).
114
+ * [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
115
+ * [#1968](https://github.com/ruby-grape/grape/pull/1968): Fix args forwarding in Grape::Middleware::Stack#merge_with for ruby 2.7.0 - [@dm1try](https://github.com/dm1try).
116
+ * [#1988](https://github.com/ruby-grape/grape/pull/1988): Refactor the full_messages method and stop overriding full_message - [@hosseintoussi](https://github.com/hosseintoussi).
117
+ * [#1956](https://github.com/ruby-grape/grape/pull/1956): Comply with Rack spec, fix `undefined method [] for nil:NilClass` error when upgrading Rack - [@ioquatix](https://github.com/ioquatix).
118
+
1
119
  ### 1.3.0 (2020/01/11)
2
120
 
3
121
  #### Features
@@ -519,7 +637,7 @@
519
637
  * [#492](https://github.com/ruby-grape/grape/pull/492): Don't allow to have nil value when a param is required and has a list of allowed values - [@Antti](https://github.com/Antti).
520
638
  * [#495](https://github.com/ruby-grape/grape/pull/495): Fixed `ParamsScope#params` for parameters nested inside arrays - [@asross](https://github.com/asross).
521
639
  * [#498](https://github.com/ruby-grape/grape/pull/498): Dry'ed up options and headers logic, allow headers to be passed to OPTIONS requests - [@karlfreeman](https://github.com/karlfreeman).
522
- * [#500](https://github.com/ruby-grape/grape/pull/500): Skip entity auto-detection when explicitely passed - [@yaneq](https://github.com/yaneq).
640
+ * [#500](https://github.com/ruby-grape/grape/pull/500): Skip entity auto-detection when explicitly passed - [@yaneq](https://github.com/yaneq).
523
641
  * [#503](https://github.com/ruby-grape/grape/pull/503): Calling declared(params) from child namespace fails to include parent namespace defined params - [@myitcv](https://github.com/myitcv).
524
642
  * [#512](https://github.com/ruby-grape/grape/pull/512): Don't create `Grape::Request` multiple times - [@dblock](https://github.com/dblock).
525
643
  * [#538](https://github.com/ruby-grape/grape/pull/538): Fixed default values for grouped params - [@dm1try](https://github.com/dm1try).
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010-2019 Michael Bleigh, Intridea Inc. and Contributors.
1
+ Copyright (c) 2010-2020 Michael Bleigh, Intridea Inc. and Contributors.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ![grape logo](grape.png)
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/grape.svg)](http://badge.fury.io/rb/grape)
4
- [![Build Status](https://travis-ci.org/ruby-grape/grape.svg?branch=master)](https://travis-ci.org/ruby-grape/grape)
4
+ [![Build Status](https://github.com/ruby-grape/grape/workflows/test/badge.svg?branch=master)](https://github.com/ruby-grape/grape/actions)
5
5
  [![Code Climate](https://codeclimate.com/github/ruby-grape/grape.svg)](https://codeclimate.com/github/ruby-grape/grape)
6
6
  [![Coverage Status](https://coveralls.io/repos/github/ruby-grape/grape/badge.svg?branch=master)](https://coveralls.io/github/ruby-grape/grape?branch=master)
7
7
  [![Inline docs](https://inch-ci.org/github/ruby-grape/grape.svg)](https://inch-ci.org/github/ruby-grape/grape)
@@ -12,12 +12,15 @@
12
12
  - [What is Grape?](#what-is-grape)
13
13
  - [Stable Release](#stable-release)
14
14
  - [Project Resources](#project-resources)
15
+ - [Grape for Enterprise](#grape-for-enterprise)
15
16
  - [Installation](#installation)
16
17
  - [Basic Usage](#basic-usage)
17
18
  - [Mounting](#mounting)
18
19
  - [All](#all)
19
20
  - [Rack](#rack)
20
21
  - [ActiveRecord without Rails](#activerecord-without-rails)
22
+ - [Rails 4](#rails-4)
23
+ - [Rails 5+](#rails-5)
21
24
  - [Alongside Sinatra (or other frameworks)](#alongside-sinatra-or-other-frameworks)
22
25
  - [Rails](#rails)
23
26
  - [Rails < 5.2](#rails--52)
@@ -141,6 +144,7 @@
141
144
  - [format_response.grape](#format_responsegrape)
142
145
  - [Monitoring Products](#monitoring-products)
143
146
  - [Contributing to Grape](#contributing-to-grape)
147
+ - [Security](#security)
144
148
  - [License](#license)
145
149
  - [Copyright](#copyright)
146
150
 
@@ -154,8 +158,7 @@ content negotiation, versioning and much more.
154
158
 
155
159
  ## Stable Release
156
160
 
157
- You're reading the documentation for the stable release of Grape, **1.3.0**.
158
- Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
161
+ You're reading the documentation for the stable release of Grape, 1.5.2.
159
162
 
160
163
  ## Project Resources
161
164
 
@@ -164,6 +167,14 @@ Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
164
167
  * Need help? Try [Grape Google Group](http://groups.google.com/group/ruby-grape) or [Gitter](https://gitter.im/ruby-grape/grape)
165
168
  * [Follow us on Twitter](https://twitter.com/grapeframework)
166
169
 
170
+ ## Grape for Enterprise
171
+
172
+ Available as part of the Tidelift Subscription.
173
+
174
+ The maintainers of Grape are working with Tidelift to deliver commercial support and maintenance. Save time, reduce risk, and improve code health, while paying the maintainers of Grape. Click [here](https://tidelift.com/subscription/request-a-demo?utm_source=rubygems-grape&utm_medium=referral&utm_campaign=enterprise) for more details.
175
+
176
+ In 2020, we plan to use the money towards gathering Grape contributors for dinner in New York City.
177
+
167
178
  ## Installation
168
179
 
169
180
  Ruby 2.4 or newer is required.
@@ -306,13 +317,21 @@ Grape will also automatically respond to HEAD and OPTIONS for all GET, and just
306
317
  If you want to use ActiveRecord within Grape, you will need to make sure that ActiveRecord's connection pool
307
318
  is handled correctly.
308
319
 
320
+ #### Rails 4
321
+
309
322
  The easiest way to achieve that is by using ActiveRecord's `ConnectionManagement` middleware in your
310
323
  `config.ru` before mounting Grape, e.g.:
311
324
 
312
325
  ```ruby
313
326
  use ActiveRecord::ConnectionAdapters::ConnectionManagement
327
+ ```
314
328
 
315
- run Twitter::API
329
+ #### Rails 5+
330
+
331
+ Use [otr-activerecord](https://github.com/jhollinger/otr-activerecord) as follows:
332
+
333
+ ```ruby
334
+ use OTR::ActiveRecord::ConnectionManagement
316
335
  ```
317
336
 
318
337
  ### Alongside Sinatra (or other frameworks)
@@ -339,9 +358,12 @@ class Web < Sinatra::Base
339
358
  end
340
359
 
341
360
  use Rack::Session::Cookie
342
- run Rack::Cascade.new [API, Web]
361
+ run Rack::Cascade.new [Web, API]
343
362
  ```
344
363
 
364
+ Note that order of loading apps using `Rack::Cascade` matters. The grape application must be last if you want to raise custom 404 errors from grape (such as `error!('Not Found',404)`). If the grape application is not last and returns 404 or 405 response, [cascade utilizes that as a signal to try the next app](https://www.rubydoc.info/gems/rack/Rack/Cascade). This may lead to undesirable behavior showing the [wrong 404 page from the wrong app](https://github.com/ruby-grape/grape/issues/1515).
365
+
366
+
345
367
  ### Rails
346
368
 
347
369
  Place API files into `app/api`. Rails expects a subdirectory that matches the name of the Ruby module and a file name that matches the name of the class. In our example, the file name location and directory for `Twitter::API` should be `app/api/twitter/api.rb`.
@@ -773,7 +795,12 @@ Available parameter builders are `Grape::Extensions::Hash::ParamBuilder`, `Grape
773
795
 
774
796
  ### Declared
775
797
 
776
- Grape allows you to access only the parameters that have been declared by your `params` block. It filters out the params that have been passed, but are not allowed. Consider the following API endpoint:
798
+ Grape allows you to access only the parameters that have been declared by your `params` block. It will:
799
+
800
+ * Filter out the params that have been passed, but are not allowed.
801
+ * Include any optional params that are declared but not passed.
802
+
803
+ Consider the following API endpoint:
777
804
 
778
805
  ````ruby
779
806
  format :json
@@ -806,9 +833,9 @@ Once we add parameters requirements, grape will start returning only the declare
806
833
  format :json
807
834
 
808
835
  params do
809
- requires :user, type: Hash do
810
- requires :first_name, type: String
811
- requires :last_name, type: String
836
+ optional :user, type: Hash do
837
+ optional :first_name, type: String
838
+ optional :last_name, type: String
812
839
  end
813
840
  end
814
841
 
@@ -836,6 +863,44 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d
836
863
  }
837
864
  ````
838
865
 
866
+ Missing params that are declared as type `Hash` or `Array` will be included.
867
+
868
+ ````ruby
869
+ format :json
870
+
871
+ params do
872
+ optional :user, type: Hash do
873
+ optional :first_name, type: String
874
+ optional :last_name, type: String
875
+ end
876
+ optional :widgets, type: Array
877
+ end
878
+
879
+ post 'users/signup' do
880
+ { 'declared_params' => declared(params) }
881
+ end
882
+ ````
883
+
884
+ **Request**
885
+
886
+ ````bash
887
+ curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d '{}'
888
+ ````
889
+
890
+ **Response**
891
+
892
+ ````json
893
+ {
894
+ "declared_params": {
895
+ "user": {
896
+ "first_name": null,
897
+ "last_name": null
898
+ },
899
+ "widgets": []
900
+ }
901
+ }
902
+ ````
903
+
839
904
  The returned hash is an `ActiveSupport::HashWithIndifferentAccess`.
840
905
 
841
906
  The `#declared` method is not available to `before` filters, as those are evaluated prior to parameter coercion.
@@ -894,8 +959,10 @@ By default `declared(params)` includes parameters that have `nil` values. If you
894
959
  format :json
895
960
 
896
961
  params do
897
- requires :first_name, type: String
898
- optional :last_name, type: String
962
+ requires :user, type: Hash do
963
+ requires :first_name, type: String
964
+ optional :last_name, type: String
965
+ end
899
966
  end
900
967
 
901
968
  post 'users/signup' do
@@ -1048,13 +1115,13 @@ params do
1048
1115
  end
1049
1116
  ```
1050
1117
 
1051
- Note that default values will be passed through to any validation options specified.
1052
- The following example will always fail if `:color` is not explicitly provided.
1053
-
1054
1118
  Default values are eagerly evaluated. Above `:non_random_number` will evaluate to the same
1055
1119
  number for each call to the endpoint of this `params` block. To have the default evaluate
1056
1120
  lazily with each request use a lambda, like `:random_number` above.
1057
1121
 
1122
+ Note that default values will be passed through to any validation options specified.
1123
+ The following example will always fail if `:color` is not explicitly provided.
1124
+
1058
1125
  ```ruby
1059
1126
  params do
1060
1127
  optional :color, type: String, default: 'blue', values: ['red', 'green']
@@ -1114,7 +1181,8 @@ Aside from the default set of supported types listed above, any class can be
1114
1181
  used as a type as long as an explicit coercion method is supplied. If the type
1115
1182
  implements a class-level `parse` method, Grape will use it automatically.
1116
1183
  This method must take one string argument and return an instance of the correct
1117
- type, or raise an exception to indicate the value was invalid. E.g.,
1184
+ type, or return an instance of `Grape::Types::InvalidValue` which optionally
1185
+ accepts a message to be returned in the response.
1118
1186
 
1119
1187
  ```ruby
1120
1188
  class Color
@@ -1124,8 +1192,9 @@ class Color
1124
1192
  end
1125
1193
 
1126
1194
  def self.parse(value)
1127
- fail 'Invalid color' unless %w(blue red green).include?(value)
1128
- new(value)
1195
+ return new(value) if %w[blue red green]).include?(value)
1196
+
1197
+ Grape::Types::InvalidValue.new('Unsupported color')
1129
1198
  end
1130
1199
  end
1131
1200
 
@@ -1722,7 +1791,7 @@ params do
1722
1791
  end
1723
1792
  ```
1724
1793
 
1725
- Every validation will have it's own instance of the validator, which means that the validator can have a state.
1794
+ Every validation will have its own instance of the validator, which means that the validator can have a state.
1726
1795
 
1727
1796
  ### Validation Errors
1728
1797
 
@@ -1981,10 +2050,10 @@ end
1981
2050
 
1982
2051
  # is NOT the same as
1983
2052
 
1984
- get ':status' do # this makes param[:status] available
2053
+ get ':status' do # this makes params[:status] available
1985
2054
  end
1986
2055
 
1987
- # This will make both param[:status_id] and param[:id] available
2056
+ # This will make both params[:status_id] and params[:id] available
1988
2057
 
1989
2058
  get 'statuses/:status_id/reviews/:id' do
1990
2059
  end
@@ -3157,17 +3226,19 @@ end
3157
3226
 
3158
3227
  Use `body false` to return `204 No Content` without any data or content-type.
3159
3228
 
3160
- You can also set the response to a file with `file`.
3229
+ You can also set the response to a file with `sendfile`. This works with the
3230
+ [Rack::Sendfile](https://www.rubydoc.info/gems/rack/Rack/Sendfile) middleware to optimally send
3231
+ the file through your web server software.
3161
3232
 
3162
3233
  ```ruby
3163
3234
  class API < Grape::API
3164
3235
  get '/' do
3165
- file '/path/to/file'
3236
+ sendfile '/path/to/file'
3166
3237
  end
3167
3238
  end
3168
3239
  ```
3169
3240
 
3170
- If you want a file to be streamed using Rack::Chunked, use `stream`.
3241
+ To stream a file in chunks use `stream`
3171
3242
 
3172
3243
  ```ruby
3173
3244
  class API < Grape::API
@@ -3177,6 +3248,26 @@ class API < Grape::API
3177
3248
  end
3178
3249
  ```
3179
3250
 
3251
+ If you want to stream non-file data use the `stream` method and a `Stream` object.
3252
+ This is an object that responds to `each` and yields for each chunk to send to the client.
3253
+ Each chunk will be sent as it is yielded instead of waiting for all of the content to be available.
3254
+
3255
+ ```ruby
3256
+ class MyStream
3257
+ def each
3258
+ yield 'part 1'
3259
+ yield 'part 2'
3260
+ yield 'part 3'
3261
+ end
3262
+ end
3263
+
3264
+ class API < Grape::API
3265
+ get '/' do
3266
+ stream MyStream.new
3267
+ end
3268
+ end
3269
+ ```
3270
+
3180
3271
  ## Authentication
3181
3272
 
3182
3273
  ### Basic and Digest Auth
@@ -3188,14 +3279,13 @@ applies to the current namespace and any children, but not parents.
3188
3279
  ```ruby
3189
3280
  http_basic do |username, password|
3190
3281
  # verify user's password here
3191
- { 'test' => 'password1' }[username] == password
3282
+ # IMPORTANT: make sure you use a comparison method which isn't prone to a timing attack
3192
3283
  end
3193
3284
  ```
3194
3285
 
3195
3286
  ```ruby
3196
3287
  http_digest({ realm: 'Test Api', opaque: 'app secret' }) do |username|
3197
3288
  # lookup the user's password here
3198
- { 'user1' => 'password1' }[username]
3199
3289
  end
3200
3290
  ```
3201
3291
 
@@ -3301,7 +3391,7 @@ end
3301
3391
 
3302
3392
  Blocks can be executed before or after every API call, using `before`, `after`,
3303
3393
  `before_validation` and `after_validation`.
3304
- If the API fails the `after` call will not be trigered, if you need code to execute for sure
3394
+ If the API fails the `after` call will not be triggered, if you need code to execute for sure
3305
3395
  use the `finally`.
3306
3396
 
3307
3397
  Before and after callbacks execute in the following order:
@@ -3852,7 +3942,7 @@ Grape integrates with following third-party tools:
3852
3942
  * **Librato Metrics** - [grape-librato](https://github.com/seanmoon/grape-librato) gem
3853
3943
  * **[Skylight](https://www.skylight.io/)** - [skylight](https://github.com/skylightio/skylight-ruby) gem, [documentation](https://docs.skylight.io/grape/)
3854
3944
  * **[AppSignal](https://www.appsignal.com)** - [appsignal-ruby](https://github.com/appsignal/appsignal-ruby) gem, [documentation](http://docs.appsignal.com/getting-started/supported-frameworks.html#grape)
3855
- * **[ElasticAPM](https://www.elastic.co/products/apm) - [elastic-apm](https://github.com/elastic/apm-agent-ruby) gem, [documentation](https://www.elastic.co/guide/en/apm/agent/ruby/3.x/getting-started-rack.html#getting-started-grape)
3945
+ * **[ElasticAPM](https://www.elastic.co/products/apm)** - [elastic-apm](https://github.com/elastic/apm-agent-ruby) gem, [documentation](https://www.elastic.co/guide/en/apm/agent/ruby/3.x/getting-started-rack.html#getting-started-grape)
3856
3946
 
3857
3947
  ## Contributing to Grape
3858
3948
 
@@ -3861,10 +3951,14 @@ features and discuss issues.
3861
3951
 
3862
3952
  See [CONTRIBUTING](CONTRIBUTING.md).
3863
3953
 
3954
+ ## Security
3955
+
3956
+ See [SECURITY](SECURITY.md) for details.
3957
+
3864
3958
  ## License
3865
3959
 
3866
- MIT License. See LICENSE for details.
3960
+ MIT License. See [LICENSE](LICENSE) for details.
3867
3961
 
3868
3962
  ## Copyright
3869
3963
 
3870
- Copyright (c) 2010-2019 Michael Bleigh, Intridea Inc. and Contributors.
3964
+ Copyright (c) 2010-2020 Michael Bleigh, Intridea Inc. and Contributors.