grape 1.8.0 → 3.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +391 -1
- data/CONTRIBUTING.md +3 -11
- data/README.md +648 -552
- data/UPGRADING.md +803 -7
- data/grape.gemspec +11 -14
- data/lib/grape/api/instance.rb +68 -158
- data/lib/grape/api.rb +71 -107
- data/lib/grape/content_types.rb +23 -11
- data/lib/grape/cookies.rb +28 -25
- data/lib/grape/declared_params_handler.rb +116 -0
- data/lib/grape/dry_types.rb +48 -6
- data/lib/grape/dsl/callbacks.rb +14 -58
- data/lib/grape/dsl/declared.rb +35 -0
- data/lib/grape/dsl/desc.rb +15 -63
- data/lib/grape/dsl/entity.rb +88 -0
- data/lib/grape/dsl/headers.rb +2 -2
- data/lib/grape/dsl/helpers.rb +83 -64
- data/lib/grape/dsl/inside_route.rb +56 -295
- data/lib/grape/dsl/logger.rb +4 -9
- data/lib/grape/dsl/middleware.rb +22 -40
- data/lib/grape/dsl/parameters.rb +50 -82
- data/lib/grape/dsl/request_response.rb +144 -146
- data/lib/grape/dsl/rescue_options.rb +24 -0
- data/lib/grape/dsl/routing.rb +261 -186
- data/lib/grape/dsl/settings.rb +29 -135
- data/lib/grape/dsl/validations.rb +39 -32
- data/lib/grape/dsl/version_options.rb +23 -0
- data/lib/grape/endpoint/options.rb +19 -0
- data/lib/grape/endpoint.rb +277 -250
- data/lib/grape/{util/env.rb → env.rb} +1 -8
- data/lib/grape/error_formatter/base.rb +56 -21
- data/lib/grape/error_formatter/json.rb +13 -15
- data/lib/grape/error_formatter/serializable_hash.rb +7 -0
- data/lib/grape/error_formatter/txt.rb +12 -18
- data/lib/grape/error_formatter/xml.rb +3 -13
- data/lib/grape/error_formatter.rb +5 -25
- data/lib/grape/exceptions/base.rb +22 -58
- data/lib/grape/exceptions/error_response.rb +45 -0
- data/lib/grape/exceptions/incompatible_option_values.rb +1 -1
- data/lib/grape/exceptions/internal_server_error.rb +16 -0
- data/lib/grape/exceptions/invalid_accept_header.rb +1 -1
- data/lib/grape/exceptions/invalid_formatter.rb +1 -1
- data/lib/grape/exceptions/invalid_message_body.rb +1 -1
- data/lib/grape/exceptions/invalid_version_header.rb +1 -1
- data/lib/grape/exceptions/invalid_versioner_option.rb +1 -1
- data/lib/grape/exceptions/method_not_allowed.rb +1 -1
- data/lib/grape/exceptions/missing_group_type.rb +0 -2
- data/lib/grape/exceptions/missing_mime_type.rb +1 -1
- data/lib/grape/exceptions/request_error.rb +11 -0
- data/lib/grape/exceptions/unknown_auth_strategy.rb +11 -0
- data/lib/grape/exceptions/unknown_parameter.rb +1 -1
- data/lib/grape/exceptions/unknown_params_builder.rb +11 -0
- data/lib/grape/exceptions/unknown_validator.rb +1 -1
- data/lib/grape/exceptions/unsupported_group_type.rb +0 -2
- data/lib/grape/exceptions/validation.rb +25 -9
- data/lib/grape/exceptions/validation_array_errors.rb +5 -0
- data/lib/grape/exceptions/validation_errors.rb +22 -26
- data/lib/grape/formatter/base.rb +16 -0
- data/lib/grape/formatter/json.rb +4 -6
- data/lib/grape/formatter/serializable_hash.rb +6 -10
- data/lib/grape/formatter/txt.rb +3 -5
- data/lib/grape/formatter/xml.rb +4 -6
- data/lib/grape/formatter.rb +7 -25
- data/lib/grape/json.rb +46 -0
- data/lib/grape/locale/en.yml +43 -42
- data/lib/grape/middleware/auth/base.rb +11 -33
- data/lib/grape/middleware/auth/dsl.rb +37 -31
- data/lib/grape/middleware/auth/strategies.rb +1 -2
- data/lib/grape/middleware/base.rb +56 -33
- data/lib/grape/middleware/deprecated_options_hash_access.rb +19 -0
- data/lib/grape/middleware/error.rb +183 -91
- data/lib/grape/middleware/formatter.rb +94 -105
- data/lib/grape/middleware/precomputed_content_types.rb +46 -0
- data/lib/grape/middleware/stack.rb +27 -38
- data/lib/grape/middleware/versioner/accept_version_header.rb +6 -33
- data/lib/grape/middleware/versioner/base.rb +72 -0
- data/lib/grape/middleware/versioner/header.rb +44 -129
- data/lib/grape/middleware/versioner/param.rb +4 -25
- data/lib/grape/middleware/versioner/path.rb +13 -32
- data/lib/grape/middleware/versioner.rb +7 -14
- data/lib/grape/mountable.rb +22 -0
- data/lib/grape/namespace.rb +18 -12
- data/lib/grape/params_builder/base.rb +20 -0
- data/lib/grape/params_builder/hash.rb +11 -0
- data/lib/grape/params_builder/hash_with_indifferent_access.rb +11 -0
- data/lib/grape/params_builder/hashie_mash.rb +11 -0
- data/lib/grape/params_builder.rb +15 -0
- data/lib/grape/parser/base.rb +16 -0
- data/lib/grape/parser/json.rb +6 -8
- data/lib/grape/parser/xml.rb +6 -8
- data/lib/grape/parser.rb +5 -23
- data/lib/grape/path.rb +38 -63
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +168 -26
- data/lib/grape/router/base_route.rb +48 -0
- data/lib/grape/router/greedy_route.rb +26 -0
- data/lib/grape/router/mustermann_pattern.rb +44 -0
- data/lib/grape/router/pattern.rb +30 -34
- data/lib/grape/router/route.rb +28 -61
- data/lib/grape/router.rb +71 -86
- data/lib/grape/serve_stream/file_body.rb +1 -0
- data/lib/grape/serve_stream/sendfile_response.rb +3 -5
- data/lib/grape/serve_stream/stream_response.rb +1 -0
- data/lib/grape/testing.rb +33 -0
- data/lib/grape/util/api_description.rb +58 -0
- data/lib/grape/util/base_inheritable.rb +14 -14
- data/lib/grape/util/cache.rb +1 -3
- data/lib/grape/util/deep_freeze.rb +35 -0
- data/lib/grape/util/endpoint_configuration.rb +1 -1
- data/lib/grape/util/header.rb +13 -0
- data/lib/grape/util/inheritable_setting.rb +51 -27
- data/lib/grape/util/inheritable_values.rb +7 -5
- data/lib/grape/util/lazy/base.rb +16 -0
- data/lib/grape/util/lazy/block.rb +22 -0
- data/lib/grape/util/lazy/value.rb +31 -0
- data/lib/grape/util/lazy/value_array.rb +21 -0
- data/lib/grape/util/lazy/value_enumerable.rb +31 -0
- data/lib/grape/util/lazy/value_hash.rb +21 -0
- data/lib/grape/util/media_type.rb +67 -0
- data/lib/grape/util/path_normalizer.rb +34 -0
- data/lib/grape/util/registry.rb +27 -0
- data/lib/grape/util/reverse_stackable_values.rb +1 -6
- data/lib/grape/util/stackable_values.rb +12 -14
- data/lib/grape/util/translation.rb +42 -0
- data/lib/grape/validations/attributes_iterator.rb +43 -27
- data/lib/grape/validations/coerce_options.rb +21 -0
- data/lib/grape/validations/contract_scope.rb +28 -0
- data/lib/grape/validations/multiple_attributes_iterator.rb +1 -1
- data/lib/grape/validations/oneof_collector.rb +39 -0
- data/lib/grape/validations/param_scope_tracker.rb +62 -0
- data/lib/grape/validations/params_documentation.rb +52 -0
- data/lib/grape/validations/params_scope.rb +180 -303
- data/lib/grape/validations/shared_options.rb +19 -0
- data/lib/grape/validations/single_attribute_iterator.rb +2 -2
- data/lib/grape/validations/types/array_coercer.rb +4 -7
- data/lib/grape/validations/types/custom_type_coercer.rb +41 -85
- data/lib/grape/validations/types/custom_type_collection_coercer.rb +1 -1
- data/lib/grape/validations/types/dry_type_coercer.rb +15 -28
- data/lib/grape/validations/types/json.rb +0 -2
- data/lib/grape/validations/types/primitive_coercer.rb +11 -35
- data/lib/grape/validations/types/set_coercer.rb +1 -4
- data/lib/grape/validations/types/variant_collection_coercer.rb +11 -0
- data/lib/grape/validations/types.rb +29 -54
- data/lib/grape/validations/validations_spec.rb +164 -0
- data/lib/grape/validations/validators/all_or_none_of_validator.rb +6 -3
- data/lib/grape/validations/validators/allow_blank_validator.rb +10 -5
- data/lib/grape/validations/validators/at_least_one_of_validator.rb +5 -2
- data/lib/grape/validations/validators/base.rb +120 -36
- data/lib/grape/validations/validators/coerce_validator.rb +18 -36
- data/lib/grape/validations/validators/contract_scope_validator.rb +45 -0
- data/lib/grape/validations/validators/default_validator.rb +13 -16
- data/lib/grape/validations/validators/exactly_one_of_validator.rb +10 -3
- data/lib/grape/validations/validators/except_values_validator.rb +15 -5
- data/lib/grape/validations/validators/length_validator.rb +48 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +12 -9
- data/lib/grape/validations/validators/{mutual_exclusion_validator.rb → mutually_exclusive_validator.rb} +4 -2
- data/lib/grape/validations/validators/oneof_validator.rb +49 -0
- data/lib/grape/validations/validators/presence_validator.rb +4 -2
- data/lib/grape/validations/validators/regexp_validator.rb +11 -3
- data/lib/grape/validations/validators/same_as_validator.rb +6 -15
- data/lib/grape/validations/validators/values_validator.rb +36 -65
- data/lib/grape/validations.rb +8 -21
- data/lib/grape/version.rb +1 -2
- data/lib/grape/xml.rb +17 -0
- data/lib/grape.rb +75 -288
- metadata +79 -291
- data/lib/grape/api/helpers.rb +0 -9
- data/lib/grape/dsl/api.rb +0 -19
- data/lib/grape/dsl/configuration.rb +0 -15
- data/lib/grape/eager_load.rb +0 -20
- data/lib/grape/exceptions/empty_message_body.rb +0 -11
- data/lib/grape/exceptions/missing_option.rb +0 -11
- data/lib/grape/exceptions/too_many_multipart_files.rb +0 -11
- data/lib/grape/exceptions/unknown_options.rb +0 -11
- data/lib/grape/extensions/active_support/hash_with_indifferent_access.rb +0 -27
- data/lib/grape/extensions/hash.rb +0 -22
- data/lib/grape/extensions/hashie/mash.rb +0 -26
- data/lib/grape/http/headers.rb +0 -61
- data/lib/grape/middleware/globals.rb +0 -16
- data/lib/grape/middleware/helpers.rb +0 -12
- data/lib/grape/middleware/versioner/parse_media_type_patch.rb +0 -24
- data/lib/grape/router/attribute_translator.rb +0 -63
- data/lib/grape/types/invalid_value.rb +0 -8
- data/lib/grape/util/json.rb +0 -12
- data/lib/grape/util/lazy_block.rb +0 -27
- data/lib/grape/util/lazy_object.rb +0 -43
- data/lib/grape/util/lazy_value.rb +0 -91
- data/lib/grape/util/registrable.rb +0 -15
- data/lib/grape/util/strict_hash_configuration.rb +0 -108
- data/lib/grape/util/xml.rb +0 -10
- data/lib/grape/validations/attributes_doc.rb +0 -58
- data/lib/grape/validations/types/build_coercer.rb +0 -94
- data/lib/grape/validations/validator_factory.rb +0 -15
- data/spec/grape/api/custom_validations_spec.rb +0 -213
- data/spec/grape/api/deeply_included_options_spec.rb +0 -56
- data/spec/grape/api/defines_boolean_in_params_spec.rb +0 -38
- data/spec/grape/api/documentation_spec.rb +0 -59
- data/spec/grape/api/inherited_helpers_spec.rb +0 -114
- data/spec/grape/api/instance_spec.rb +0 -103
- data/spec/grape/api/invalid_format_spec.rb +0 -45
- data/spec/grape/api/namespace_parameters_in_route_spec.rb +0 -38
- data/spec/grape/api/nested_helpers_spec.rb +0 -50
- data/spec/grape/api/optional_parameters_in_route_spec.rb +0 -43
- data/spec/grape/api/parameters_modification_spec.rb +0 -41
- data/spec/grape/api/patch_method_helpers_spec.rb +0 -79
- data/spec/grape/api/recognize_path_spec.rb +0 -21
- data/spec/grape/api/required_parameters_in_route_spec.rb +0 -37
- data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +0 -26
- data/spec/grape/api/routes_with_requirements_spec.rb +0 -59
- data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +0 -41
- data/spec/grape/api/shared_helpers_spec.rb +0 -36
- data/spec/grape/api_remount_spec.rb +0 -509
- data/spec/grape/api_spec.rb +0 -4356
- data/spec/grape/dsl/callbacks_spec.rb +0 -45
- data/spec/grape/dsl/desc_spec.rb +0 -98
- data/spec/grape/dsl/headers_spec.rb +0 -62
- data/spec/grape/dsl/helpers_spec.rb +0 -100
- data/spec/grape/dsl/inside_route_spec.rb +0 -531
- data/spec/grape/dsl/logger_spec.rb +0 -24
- data/spec/grape/dsl/middleware_spec.rb +0 -60
- data/spec/grape/dsl/parameters_spec.rb +0 -180
- data/spec/grape/dsl/request_response_spec.rb +0 -225
- data/spec/grape/dsl/routing_spec.rb +0 -275
- data/spec/grape/dsl/settings_spec.rb +0 -261
- data/spec/grape/dsl/validations_spec.rb +0 -55
- data/spec/grape/endpoint/declared_spec.rb +0 -846
- data/spec/grape/endpoint_spec.rb +0 -1085
- data/spec/grape/entity_spec.rb +0 -336
- data/spec/grape/exceptions/base_spec.rb +0 -81
- data/spec/grape/exceptions/body_parse_errors_spec.rb +0 -185
- data/spec/grape/exceptions/invalid_accept_header_spec.rb +0 -358
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -15
- data/spec/grape/exceptions/invalid_response_spec.rb +0 -11
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -15
- data/spec/grape/exceptions/missing_group_type_spec.rb +0 -17
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -17
- data/spec/grape/exceptions/missing_option_spec.rb +0 -15
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -15
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -15
- data/spec/grape/exceptions/unsupported_group_type_spec.rb +0 -19
- data/spec/grape/exceptions/validation_errors_spec.rb +0 -92
- data/spec/grape/exceptions/validation_spec.rb +0 -19
- data/spec/grape/extensions/param_builders/hash_spec.rb +0 -83
- data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +0 -105
- data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +0 -79
- data/spec/grape/grape_spec.rb +0 -9
- data/spec/grape/integration/global_namespace_function_spec.rb +0 -29
- data/spec/grape/integration/rack_sendfile_spec.rb +0 -48
- data/spec/grape/integration/rack_spec.rb +0 -51
- data/spec/grape/loading_spec.rb +0 -44
- data/spec/grape/middleware/auth/base_spec.rb +0 -31
- data/spec/grape/middleware/auth/dsl_spec.rb +0 -60
- data/spec/grape/middleware/auth/strategies_spec.rb +0 -120
- data/spec/grape/middleware/base_spec.rb +0 -221
- data/spec/grape/middleware/error_spec.rb +0 -85
- data/spec/grape/middleware/exception_spec.rb +0 -294
- data/spec/grape/middleware/formatter_spec.rb +0 -461
- data/spec/grape/middleware/globals_spec.rb +0 -30
- data/spec/grape/middleware/stack_spec.rb +0 -155
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -122
- data/spec/grape/middleware/versioner/header_spec.rb +0 -345
- data/spec/grape/middleware/versioner/param_spec.rb +0 -171
- data/spec/grape/middleware/versioner/path_spec.rb +0 -62
- data/spec/grape/middleware/versioner_spec.rb +0 -21
- data/spec/grape/named_api_spec.rb +0 -19
- data/spec/grape/parser_spec.rb +0 -86
- data/spec/grape/path_spec.rb +0 -252
- data/spec/grape/presenters/presenter_spec.rb +0 -71
- data/spec/grape/request_spec.rb +0 -126
- data/spec/grape/util/inheritable_setting_spec.rb +0 -242
- data/spec/grape/util/inheritable_values_spec.rb +0 -79
- data/spec/grape/util/reverse_stackable_values_spec.rb +0 -134
- data/spec/grape/util/stackable_values_spec.rb +0 -128
- data/spec/grape/util/strict_hash_configuration_spec.rb +0 -38
- data/spec/grape/validations/attributes_doc_spec.rb +0 -153
- data/spec/grape/validations/instance_behaivour_spec.rb +0 -43
- data/spec/grape/validations/multiple_attributes_iterator_spec.rb +0 -38
- data/spec/grape/validations/params_scope_spec.rb +0 -1420
- data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -56
- data/spec/grape/validations/types/array_coercer_spec.rb +0 -33
- data/spec/grape/validations/types/primitive_coercer_spec.rb +0 -150
- data/spec/grape/validations/types/set_coercer_spec.rb +0 -32
- data/spec/grape/validations/types_spec.rb +0 -111
- data/spec/grape/validations/validators/all_or_none_spec.rb +0 -162
- data/spec/grape/validations/validators/allow_blank_spec.rb +0 -575
- data/spec/grape/validations/validators/at_least_one_of_spec.rb +0 -205
- data/spec/grape/validations/validators/base_spec.rb +0 -38
- data/spec/grape/validations/validators/coerce_spec.rb +0 -1261
- data/spec/grape/validations/validators/default_spec.rb +0 -463
- data/spec/grape/validations/validators/exactly_one_of_spec.rb +0 -233
- data/spec/grape/validations/validators/except_values_spec.rb +0 -192
- data/spec/grape/validations/validators/mutual_exclusion_spec.rb +0 -214
- data/spec/grape/validations/validators/presence_spec.rb +0 -315
- data/spec/grape/validations/validators/regexp_spec.rb +0 -161
- data/spec/grape/validations/validators/same_as_spec.rb +0 -57
- data/spec/grape/validations/validators/values_spec.rb +0 -733
- data/spec/grape/validations/validators/zh-CN.yml +0 -10
- data/spec/grape/validations_spec.rb +0 -2030
- data/spec/integration/eager_load/eager_load_spec.rb +0 -15
- data/spec/integration/multi_json/json_spec.rb +0 -7
- data/spec/integration/multi_xml/xml_spec.rb +0 -7
- data/spec/shared/deprecated_class_examples.rb +0 -16
- data/spec/shared/versioning_examples.rb +0 -215
- data/spec/spec_helper.rb +0 -52
- data/spec/support/basic_auth_encode_helpers.rb +0 -11
- data/spec/support/chunks.rb +0 -14
- data/spec/support/content_type_helpers.rb +0 -15
- data/spec/support/endpoint_faker.rb +0 -25
- data/spec/support/file_streamer.rb +0 -13
- data/spec/support/integer_helpers.rb +0 -13
- data/spec/support/versioned_helpers.rb +0 -55
data/README.md
CHANGED
|
@@ -1,172 +1,22 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
3
|
[](http://badge.fury.io/rb/grape)
|
|
4
|
-
[](https://codeclimate.com/github/ruby-grape/grape)
|
|
4
|
+
[](https://github.com/ruby-grape/grape/actions/workflows/test.yml)
|
|
6
5
|
[](https://coveralls.io/github/ruby-grape/grape?branch=master)
|
|
7
|
-
[](https://inch-ci.org/github/ruby-grape/grape)
|
|
8
|
-
[](https://gitter.im/ruby-grape/grape?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
9
|
-
|
|
10
|
-
## Table of Contents
|
|
11
|
-
|
|
12
|
-
- [What is Grape?](#what-is-grape)
|
|
13
|
-
- [Stable Release](#stable-release)
|
|
14
|
-
- [Project Resources](#project-resources)
|
|
15
|
-
- [Grape for Enterprise](#grape-for-enterprise)
|
|
16
|
-
- [Installation](#installation)
|
|
17
|
-
- [Basic Usage](#basic-usage)
|
|
18
|
-
- [Mounting](#mounting)
|
|
19
|
-
- [All](#all)
|
|
20
|
-
- [Rack](#rack)
|
|
21
|
-
- [ActiveRecord without Rails](#activerecord-without-rails)
|
|
22
|
-
- [Rails 4](#rails-4)
|
|
23
|
-
- [Rails 5+](#rails-5)
|
|
24
|
-
- [Alongside Sinatra (or other frameworks)](#alongside-sinatra-or-other-frameworks)
|
|
25
|
-
- [Rails](#rails)
|
|
26
|
-
- [Rails < 5.2](#rails--52)
|
|
27
|
-
- [Rails 6.0](#rails-60)
|
|
28
|
-
- [Modules](#modules)
|
|
29
|
-
- [Remounting](#remounting)
|
|
30
|
-
- [Mount Configuration](#mount-configuration)
|
|
31
|
-
- [Versioning](#versioning)
|
|
32
|
-
- [Path](#path)
|
|
33
|
-
- [Header](#header)
|
|
34
|
-
- [Accept-Version Header](#accept-version-header)
|
|
35
|
-
- [Param](#param)
|
|
36
|
-
- [Describing Methods](#describing-methods)
|
|
37
|
-
- [Configuration](#configuration)
|
|
38
|
-
- [Parameters](#parameters)
|
|
39
|
-
- [Params Class](#params-class)
|
|
40
|
-
- [Declared](#declared)
|
|
41
|
-
- [Include Parent Namespaces](#include-parent-namespaces)
|
|
42
|
-
- [Include Missing](#include-missing)
|
|
43
|
-
- [Evaluate Given](#evaluate-given)
|
|
44
|
-
- [Parameter Validation and Coercion](#parameter-validation-and-coercion)
|
|
45
|
-
- [Supported Parameter Types](#supported-parameter-types)
|
|
46
|
-
- [Integer/Fixnum and Coercions](#integerfixnum-and-coercions)
|
|
47
|
-
- [Custom Types and Coercions](#custom-types-and-coercions)
|
|
48
|
-
- [Multipart File Parameters](#multipart-file-parameters)
|
|
49
|
-
- [First-Class JSON Types](#first-class-json-types)
|
|
50
|
-
- [Multiple Allowed Types](#multiple-allowed-types)
|
|
51
|
-
- [Validation of Nested Parameters](#validation-of-nested-parameters)
|
|
52
|
-
- [Dependent Parameters](#dependent-parameters)
|
|
53
|
-
- [Group Options](#group-options)
|
|
54
|
-
- [Renaming](#renaming)
|
|
55
|
-
- [Built-in Validators](#built-in-validators)
|
|
56
|
-
- [allow_blank](#allow_blank)
|
|
57
|
-
- [values](#values)
|
|
58
|
-
- [except_values](#except_values)
|
|
59
|
-
- [same_as](#same_as)
|
|
60
|
-
- [regexp](#regexp)
|
|
61
|
-
- [mutually_exclusive](#mutually_exclusive)
|
|
62
|
-
- [exactly_one_of](#exactly_one_of)
|
|
63
|
-
- [at_least_one_of](#at_least_one_of)
|
|
64
|
-
- [all_or_none_of](#all_or_none_of)
|
|
65
|
-
- [Nested mutually_exclusive, exactly_one_of, at_least_one_of, all_or_none_of](#nested-mutually_exclusive-exactly_one_of-at_least_one_of-all_or_none_of)
|
|
66
|
-
- [Namespace Validation and Coercion](#namespace-validation-and-coercion)
|
|
67
|
-
- [Custom Validators](#custom-validators)
|
|
68
|
-
- [Validation Errors](#validation-errors)
|
|
69
|
-
- [I18n](#i18n)
|
|
70
|
-
- [Custom Validation messages](#custom-validation-messages)
|
|
71
|
-
- [presence, allow_blank, values, regexp](#presence-allow_blank-values-regexp)
|
|
72
|
-
- [same_as](#same_as-1)
|
|
73
|
-
- [all_or_none_of](#all_or_none_of-1)
|
|
74
|
-
- [mutually_exclusive](#mutually_exclusive-1)
|
|
75
|
-
- [exactly_one_of](#exactly_one_of-1)
|
|
76
|
-
- [at_least_one_of](#at_least_one_of-1)
|
|
77
|
-
- [Coerce](#coerce)
|
|
78
|
-
- [With Lambdas](#with-lambdas)
|
|
79
|
-
- [Pass symbols for i18n translations](#pass-symbols-for-i18n-translations)
|
|
80
|
-
- [Overriding Attribute Names](#overriding-attribute-names)
|
|
81
|
-
- [With Default](#with-default)
|
|
82
|
-
- [Headers](#headers)
|
|
83
|
-
- [Request](#request)
|
|
84
|
-
- [Header Case Handling](#header-case-handling)
|
|
85
|
-
- [Response](#response)
|
|
86
|
-
- [Routes](#routes)
|
|
87
|
-
- [Helpers](#helpers)
|
|
88
|
-
- [Path Helpers](#path-helpers)
|
|
89
|
-
- [Parameter Documentation](#parameter-documentation)
|
|
90
|
-
- [Cookies](#cookies)
|
|
91
|
-
- [HTTP Status Code](#http-status-code)
|
|
92
|
-
- [Redirecting](#redirecting)
|
|
93
|
-
- [Recognizing Path](#recognizing-path)
|
|
94
|
-
- [Allowed Methods](#allowed-methods)
|
|
95
|
-
- [Raising Exceptions](#raising-exceptions)
|
|
96
|
-
- [Default Error HTTP Status Code](#default-error-http-status-code)
|
|
97
|
-
- [Handling 404](#handling-404)
|
|
98
|
-
- [Exception Handling](#exception-handling)
|
|
99
|
-
- [Rescuing exceptions inside namespaces](#rescuing-exceptions-inside-namespaces)
|
|
100
|
-
- [Unrescuable Exceptions](#unrescuable-exceptions)
|
|
101
|
-
- [Exceptions that should be rescued explicitly](#exceptions-that-should-be-rescued-explicitly)
|
|
102
|
-
- [Rails 3.x](#rails-3x)
|
|
103
|
-
- [Logging](#logging)
|
|
104
|
-
- [API Formats](#api-formats)
|
|
105
|
-
- [JSONP](#jsonp)
|
|
106
|
-
- [CORS](#cors)
|
|
107
|
-
- [Content-type](#content-type)
|
|
108
|
-
- [API Data Formats](#api-data-formats)
|
|
109
|
-
- [JSON and XML Processors](#json-and-xml-processors)
|
|
110
|
-
- [RESTful Model Representations](#restful-model-representations)
|
|
111
|
-
- [Grape Entities](#grape-entities)
|
|
112
|
-
- [Hypermedia and Roar](#hypermedia-and-roar)
|
|
113
|
-
- [Rabl](#rabl)
|
|
114
|
-
- [Active Model Serializers](#active-model-serializers)
|
|
115
|
-
- [Sending Raw or No Data](#sending-raw-or-no-data)
|
|
116
|
-
- [Authentication](#authentication)
|
|
117
|
-
- [Basic and Digest Auth](#basic-and-digest-auth)
|
|
118
|
-
- [Register custom middleware for authentication](#register-custom-middleware-for-authentication)
|
|
119
|
-
- [Describing and Inspecting an API](#describing-and-inspecting-an-api)
|
|
120
|
-
- [Current Route and Endpoint](#current-route-and-endpoint)
|
|
121
|
-
- [Before, After and Finally](#before-after-and-finally)
|
|
122
|
-
- [Anchoring](#anchoring)
|
|
123
|
-
- [Using Custom Middleware](#using-custom-middleware)
|
|
124
|
-
- [Grape Middleware](#grape-middleware)
|
|
125
|
-
- [Rails Middleware](#rails-middleware)
|
|
126
|
-
- [Remote IP](#remote-ip)
|
|
127
|
-
- [Writing Tests](#writing-tests)
|
|
128
|
-
- [Writing Tests with Rack](#writing-tests-with-rack)
|
|
129
|
-
- [RSpec](#rspec)
|
|
130
|
-
- [Airborne](#airborne)
|
|
131
|
-
- [MiniTest](#minitest)
|
|
132
|
-
- [Writing Tests with Rails](#writing-tests-with-rails)
|
|
133
|
-
- [RSpec](#rspec-1)
|
|
134
|
-
- [MiniTest](#minitest-1)
|
|
135
|
-
- [Stubbing Helpers](#stubbing-helpers)
|
|
136
|
-
- [Reloading API Changes in Development](#reloading-api-changes-in-development)
|
|
137
|
-
- [Reloading in Rack Applications](#reloading-in-rack-applications)
|
|
138
|
-
- [Reloading in Rails Applications](#reloading-in-rails-applications)
|
|
139
|
-
- [Performance Monitoring](#performance-monitoring)
|
|
140
|
-
- [Active Support Instrumentation](#active-support-instrumentation)
|
|
141
|
-
- [endpoint_run.grape](#endpoint_rungrape)
|
|
142
|
-
- [endpoint_render.grape](#endpoint_rendergrape)
|
|
143
|
-
- [endpoint_run_filters.grape](#endpoint_run_filtersgrape)
|
|
144
|
-
- [endpoint_run_validators.grape](#endpoint_run_validatorsgrape)
|
|
145
|
-
- [format_response.grape](#format_responsegrape)
|
|
146
|
-
- [Monitoring Products](#monitoring-products)
|
|
147
|
-
- [Contributing to Grape](#contributing-to-grape)
|
|
148
|
-
- [Security](#security)
|
|
149
|
-
- [License](#license)
|
|
150
|
-
- [Copyright](#copyright)
|
|
151
6
|
|
|
152
7
|
## What is Grape?
|
|
153
8
|
|
|
154
|
-
Grape is a REST-like API framework for Ruby. It's designed to run on Rack
|
|
155
|
-
or complement existing web application frameworks such as Rails and Sinatra by
|
|
156
|
-
providing a simple DSL to easily develop RESTful APIs. It has built-in support
|
|
157
|
-
for common conventions, including multiple formats, subdomain/prefix restriction,
|
|
158
|
-
content negotiation, versioning and much more.
|
|
9
|
+
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.
|
|
159
10
|
|
|
160
11
|
## Stable Release
|
|
161
12
|
|
|
162
|
-
You're reading the documentation for the stable release of Grape,
|
|
163
|
-
Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version.
|
|
13
|
+
You're reading the documentation for the stable release of Grape, 3.3.5.
|
|
164
14
|
|
|
165
15
|
## Project Resources
|
|
166
16
|
|
|
167
17
|
* [Grape Website](http://www.ruby-grape.org)
|
|
168
18
|
* [Documentation](http://www.rubydoc.info/gems/grape)
|
|
169
|
-
* Need help?
|
|
19
|
+
* Need help? [Open an Issue](https://github.com/ruby-grape/grape/issues)
|
|
170
20
|
* [Follow us on Twitter](https://twitter.com/grapeframework)
|
|
171
21
|
|
|
172
22
|
## Grape for Enterprise
|
|
@@ -177,7 +27,7 @@ The maintainers of Grape are working with Tidelift to deliver commercial support
|
|
|
177
27
|
|
|
178
28
|
## Installation
|
|
179
29
|
|
|
180
|
-
Ruby
|
|
30
|
+
Ruby 3.3 or newer is required.
|
|
181
31
|
|
|
182
32
|
Grape is available as a gem, to install it run:
|
|
183
33
|
|
|
@@ -186,8 +36,7 @@ Grape is available as a gem, to install it run:
|
|
|
186
36
|
## Basic Usage
|
|
187
37
|
|
|
188
38
|
Grape APIs are Rack applications that are created by subclassing `Grape::API`.
|
|
189
|
-
Below is a simple example showing some of the more common features of Grape in
|
|
190
|
-
the context of recreating parts of the Twitter API.
|
|
39
|
+
Below is a simple example showing some of the more common features of Grape in the context of recreating parts of the Twitter API.
|
|
191
40
|
|
|
192
41
|
```ruby
|
|
193
42
|
module Twitter
|
|
@@ -266,12 +115,16 @@ module Twitter
|
|
|
266
115
|
end
|
|
267
116
|
```
|
|
268
117
|
|
|
118
|
+
## Rails 7.1
|
|
119
|
+
|
|
120
|
+
Grape's [deprecator](https://api.rubyonrails.org/v7.1.0/classes/ActiveSupport/Deprecation.html) will be added to your application's deprecators [automatically](lib/grape/railtie.rb) as `:grape`, so that your application's configuration can be applied to it.
|
|
121
|
+
|
|
269
122
|
## Mounting
|
|
270
123
|
|
|
271
124
|
### All
|
|
272
125
|
|
|
273
126
|
|
|
274
|
-
By default Grape will compile the routes on the first route, it is possible to pre-load routes using the `compile!` method.
|
|
127
|
+
By default Grape will compile the routes on the first route, but it is possible to pre-load routes using the `compile!` method.
|
|
275
128
|
|
|
276
129
|
```ruby
|
|
277
130
|
Twitter::API.compile!
|
|
@@ -281,8 +134,7 @@ This can be added to your `config.ru` (if using rackup), `application.rb` (if us
|
|
|
281
134
|
|
|
282
135
|
### Rack
|
|
283
136
|
|
|
284
|
-
The above sample creates a Rack application that can be run from a rackup `config.ru` file
|
|
285
|
-
with `rackup`:
|
|
137
|
+
The above sample creates a Rack application that can be run from a rackup `config.ru` file with `rackup`:
|
|
286
138
|
|
|
287
139
|
```ruby
|
|
288
140
|
run Twitter::API
|
|
@@ -306,32 +158,9 @@ And would respond to the following routes:
|
|
|
306
158
|
|
|
307
159
|
Grape will also automatically respond to HEAD and OPTIONS for all GET, and just OPTIONS for all other routes.
|
|
308
160
|
|
|
309
|
-
### ActiveRecord without Rails
|
|
310
|
-
|
|
311
|
-
If you want to use ActiveRecord within Grape, you will need to make sure that ActiveRecord's connection pool
|
|
312
|
-
is handled correctly.
|
|
313
|
-
|
|
314
|
-
#### Rails 4
|
|
315
|
-
|
|
316
|
-
The easiest way to achieve that is by using ActiveRecord's `ConnectionManagement` middleware in your
|
|
317
|
-
`config.ru` before mounting Grape, e.g.:
|
|
318
|
-
|
|
319
|
-
```ruby
|
|
320
|
-
use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
#### Rails 5+
|
|
324
|
-
|
|
325
|
-
Use [otr-activerecord](https://github.com/jhollinger/otr-activerecord) as follows:
|
|
326
|
-
|
|
327
|
-
```ruby
|
|
328
|
-
use OTR::ActiveRecord::ConnectionManagement
|
|
329
|
-
```
|
|
330
|
-
|
|
331
161
|
### Alongside Sinatra (or other frameworks)
|
|
332
162
|
|
|
333
|
-
If you wish to mount Grape alongside another Rack framework such as Sinatra, you can do so easily using
|
|
334
|
-
`Rack::Cascade`:
|
|
163
|
+
If you wish to mount Grape alongside another Rack framework such as Sinatra, you can do so easily using `Rack::Cascade`:
|
|
335
164
|
|
|
336
165
|
```ruby
|
|
337
166
|
# Example config.ru
|
|
@@ -367,21 +196,8 @@ Modify `config/routes`:
|
|
|
367
196
|
```ruby
|
|
368
197
|
mount Twitter::API => '/'
|
|
369
198
|
```
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
Modify `application.rb`:
|
|
374
|
-
|
|
375
|
-
```ruby
|
|
376
|
-
config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
|
|
377
|
-
config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
See [below](#reloading-api-changes-in-development) for additional code that enables reloading of API changes in development.
|
|
381
|
-
|
|
382
|
-
#### Rails 6.0
|
|
383
|
-
|
|
384
|
-
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:
|
|
199
|
+
#### Zeitwerk
|
|
200
|
+
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:
|
|
385
201
|
|
|
386
202
|
```ruby
|
|
387
203
|
ActiveSupport::Inflector.inflections(:en) do |inflect|
|
|
@@ -391,8 +207,7 @@ end
|
|
|
391
207
|
|
|
392
208
|
### Modules
|
|
393
209
|
|
|
394
|
-
You can mount multiple API implementations inside another one. These don't have to be
|
|
395
|
-
different versions, but may be components of the same API.
|
|
210
|
+
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.
|
|
396
211
|
|
|
397
212
|
```ruby
|
|
398
213
|
class Twitter::API < Grape::API
|
|
@@ -409,7 +224,7 @@ class Twitter::API < Grape::API
|
|
|
409
224
|
end
|
|
410
225
|
```
|
|
411
226
|
|
|
412
|
-
|
|
227
|
+
Declarations as `before/after/rescue_from` can be placed before or after `mount`. In any case they will be inherited.
|
|
413
228
|
|
|
414
229
|
```ruby
|
|
415
230
|
class Twitter::API < Grape::API
|
|
@@ -417,8 +232,20 @@ class Twitter::API < Grape::API
|
|
|
417
232
|
header 'X-Base-Header', 'will be defined for all APIs that are mounted below'
|
|
418
233
|
end
|
|
419
234
|
|
|
235
|
+
rescue_from :all do
|
|
236
|
+
error!({ "error" => "Internal Server Error" }, 500)
|
|
237
|
+
end
|
|
238
|
+
|
|
420
239
|
mount Twitter::Users
|
|
421
240
|
mount Twitter::Search
|
|
241
|
+
|
|
242
|
+
after do
|
|
243
|
+
clean_cache!
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
rescue_from ZeroDivisionError do
|
|
247
|
+
error!({ "error" => "Not found" }, 404)
|
|
248
|
+
end
|
|
422
249
|
end
|
|
423
250
|
```
|
|
424
251
|
|
|
@@ -484,7 +311,7 @@ mount ::Some::Api => '/some/api', with: { condition: true }
|
|
|
484
311
|
|
|
485
312
|
You can access `configuration` on the class (to use as dynamic attributes), inside blocks (like namespace)
|
|
486
313
|
|
|
487
|
-
If you want logic happening
|
|
314
|
+
If you want logic happening based on a `configuration`, you can use the helper `given`.
|
|
488
315
|
|
|
489
316
|
```ruby
|
|
490
317
|
class ConditionalEndpoint::API < Grape::API
|
|
@@ -549,10 +376,69 @@ end
|
|
|
549
376
|
|
|
550
377
|
## Versioning
|
|
551
378
|
|
|
552
|
-
|
|
553
|
-
`:header`, `:accept_version_header` and `:param`. The default strategy is `:path`.
|
|
379
|
+
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.
|
|
554
380
|
|
|
555
|
-
|
|
381
|
+
```ruby
|
|
382
|
+
require 'v1'
|
|
383
|
+
require 'v2'
|
|
384
|
+
require 'v3'
|
|
385
|
+
class App < Grape::API
|
|
386
|
+
mount V3
|
|
387
|
+
mount V2
|
|
388
|
+
mount V1
|
|
389
|
+
end
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
To maintain the same endpoints from earlier API versions without rewriting them, you can indicate multiple versions within the previous API versions.
|
|
393
|
+
|
|
394
|
+
```ruby
|
|
395
|
+
class V1 < Grape::API
|
|
396
|
+
version 'v1', 'v2', 'v3'
|
|
397
|
+
|
|
398
|
+
get '/foo' do
|
|
399
|
+
# your code for GET /foo
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
get '/other' do
|
|
403
|
+
# your code for GET /other
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
class V2 < Grape::API
|
|
408
|
+
version 'v2', 'v3'
|
|
409
|
+
|
|
410
|
+
get '/var' do
|
|
411
|
+
# your code for GET /var
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
class V3 < Grape::API
|
|
416
|
+
version 'v3'
|
|
417
|
+
|
|
418
|
+
get '/foo' do
|
|
419
|
+
# your new code for GET /foo
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
Using the example provided, the subsequent endpoints will be accessible across various versions:
|
|
425
|
+
|
|
426
|
+
```shell
|
|
427
|
+
GET /v1/foo
|
|
428
|
+
GET /v1/other
|
|
429
|
+
GET /v2/foo # => Same behavior as v1
|
|
430
|
+
GET /v2/other # => Same behavior as v1
|
|
431
|
+
GET /v2/var # => New endpoint not available in v1
|
|
432
|
+
GET /v3/foo # => Different behavior to v1 and v2
|
|
433
|
+
GET /v3/other # => Same behavior as v1 and v2
|
|
434
|
+
GET /v3/var # => Same behavior as v2
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
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`.
|
|
438
|
+
|
|
439
|
+
### Strategies
|
|
440
|
+
|
|
441
|
+
#### Path
|
|
556
442
|
|
|
557
443
|
```ruby
|
|
558
444
|
version 'v1', using: :path
|
|
@@ -562,7 +448,7 @@ Using this versioning strategy, clients should pass the desired version in the U
|
|
|
562
448
|
|
|
563
449
|
curl http://localhost:9292/v1/statuses/public_timeline
|
|
564
450
|
|
|
565
|
-
|
|
451
|
+
#### Header
|
|
566
452
|
|
|
567
453
|
```ruby
|
|
568
454
|
version 'v1', using: :header, vendor: 'twitter'
|
|
@@ -576,20 +462,19 @@ vnd.vendor-and-or-resource-v1234+format
|
|
|
576
462
|
|
|
577
463
|
Basically all tokens between the final `-` and the `+` will be interpreted as the version.
|
|
578
464
|
|
|
579
|
-
Using this versioning strategy, clients should pass the desired version in the HTTP `Accept`
|
|
465
|
+
Using this versioning strategy, clients should pass the desired version in the HTTP `Accept` header.
|
|
580
466
|
|
|
581
467
|
curl -H Accept:application/vnd.twitter-v1+json http://localhost:9292/statuses/public_timeline
|
|
582
468
|
|
|
583
|
-
By default, the first matching version is used when no `Accept` header is
|
|
584
|
-
supplied. This behavior is similar to routing in Rails. To circumvent this default behavior,
|
|
585
|
-
one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error
|
|
586
|
-
is returned when no correct `Accept` header is supplied.
|
|
469
|
+
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.
|
|
587
470
|
|
|
588
|
-
When an invalid `Accept` header is supplied, a `406 Not Acceptable` error is returned if the `:cascade`
|
|
589
|
-
option is set to `false`. Otherwise a `404 Not Found` error is returned by Rack if no other route
|
|
590
|
-
matches.
|
|
471
|
+
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.
|
|
591
472
|
|
|
592
|
-
|
|
473
|
+
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:
|
|
474
|
+
|
|
475
|
+
curl -H "Accept: text/xml;q=0.8, application/json;q=0.9" localhost:1234/resource
|
|
476
|
+
|
|
477
|
+
#### Accept-Version Header
|
|
593
478
|
|
|
594
479
|
```ruby
|
|
595
480
|
version 'v1', using: :accept_version_header
|
|
@@ -599,20 +484,15 @@ Using this versioning strategy, clients should pass the desired version in the H
|
|
|
599
484
|
|
|
600
485
|
curl -H "Accept-Version:v1" http://localhost:9292/statuses/public_timeline
|
|
601
486
|
|
|
602
|
-
By default, the first matching version is used when no `Accept-Version` header is
|
|
603
|
-
supplied. This behavior is similar to routing in Rails. To circumvent this default behavior,
|
|
604
|
-
one could use the `:strict` option. When this option is set to `true`, a `406 Not Acceptable` error
|
|
605
|
-
is returned when no correct `Accept` header is supplied and the `:cascade` option is set to `false`.
|
|
606
|
-
Otherwise a `404 Not Found` error is returned by Rack if no other route matches.
|
|
487
|
+
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-Version` 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.
|
|
607
488
|
|
|
608
|
-
|
|
489
|
+
#### Param
|
|
609
490
|
|
|
610
491
|
```ruby
|
|
611
492
|
version 'v1', using: :param
|
|
612
493
|
```
|
|
613
494
|
|
|
614
|
-
Using this versioning strategy, clients should pass the desired version as a request parameter,
|
|
615
|
-
either in the URL query string or in the request body.
|
|
495
|
+
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.
|
|
616
496
|
|
|
617
497
|
curl http://localhost:9292/statuses/public_timeline?apiver=v1
|
|
618
498
|
|
|
@@ -625,11 +505,32 @@ version 'v1', using: :param, parameter: 'v'
|
|
|
625
505
|
curl http://localhost:9292/statuses/public_timeline?v=v1
|
|
626
506
|
|
|
627
507
|
|
|
508
|
+
## Linting
|
|
509
|
+
|
|
510
|
+
You can check whether your API is in conformance with the [Rack's specification](https://github.com/rack/rack/blob/main/SPEC.rdoc) by calling `lint!` at the API level or through [configuration](#configuration).
|
|
511
|
+
|
|
512
|
+
```ruby
|
|
513
|
+
class Api < Grape::API
|
|
514
|
+
lint!
|
|
515
|
+
end
|
|
516
|
+
```
|
|
517
|
+
```ruby
|
|
518
|
+
Grape.configure do |config|
|
|
519
|
+
config.lint = true
|
|
520
|
+
end
|
|
521
|
+
```
|
|
522
|
+
```ruby
|
|
523
|
+
Grape.config.lint = true
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
### Bug in Rack::ETag under Rack 3.X
|
|
527
|
+
If you're using Rack 3.X and the `Rack::ETag` middleware (used by [Rails](https://guides.rubyonrails.org/rails_on_rack.html#inspecting-middleware-stack)), a [bug](https://github.com/rack/rack/pull/2324) related to linting has been fixed in [3.1.13](https://github.com/rack/rack/blob/v3.1.13/CHANGELOG.md#3113---2025-04-13) and [3.0.15](https://github.com/rack/rack/blob/v3.1.13/CHANGELOG.md#3015---2025-04-13) respectively.
|
|
528
|
+
|
|
628
529
|
## Describing Methods
|
|
629
530
|
|
|
630
531
|
You can add a description to API methods and namespaces. The description would be used by [grape-swagger][grape-swagger] to generate swagger compliant documentation.
|
|
631
532
|
|
|
632
|
-
Note: Description block is only for documentation and won't
|
|
533
|
+
Note: Description block is only for documentation and won't affect API behavior.
|
|
633
534
|
|
|
634
535
|
```ruby
|
|
635
536
|
desc 'Returns your public timeline.' do
|
|
@@ -691,10 +592,13 @@ For example, for the `param_builder`, the following code could run in an initial
|
|
|
691
592
|
|
|
692
593
|
```ruby
|
|
693
594
|
Grape.configure do |config|
|
|
694
|
-
config.param_builder =
|
|
595
|
+
config.param_builder = :hashie_mash
|
|
695
596
|
end
|
|
696
597
|
```
|
|
697
598
|
|
|
599
|
+
Available parameter builders are `:hash`, `:hash_with_indifferent_access`, and `:hashie_mash`.
|
|
600
|
+
See [params_builder](lib/grape/params_builder).
|
|
601
|
+
|
|
698
602
|
You can also configure a single API:
|
|
699
603
|
|
|
700
604
|
```ruby
|
|
@@ -703,13 +607,11 @@ API.configure do |config|
|
|
|
703
607
|
end
|
|
704
608
|
```
|
|
705
609
|
|
|
706
|
-
This will be available inside the API with `configuration`, as if it were
|
|
707
|
-
[mount configuration](#mount-configuration).
|
|
610
|
+
This will be available inside the API with `configuration`, as if it were [mount configuration](#mount-configuration).
|
|
708
611
|
|
|
709
612
|
## Parameters
|
|
710
613
|
|
|
711
|
-
Request parameters are available through the `params` hash object. This includes `GET`, `POST`
|
|
712
|
-
and `PUT` parameters, along with any named parameters you specify in your route strings.
|
|
614
|
+
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.
|
|
713
615
|
|
|
714
616
|
```ruby
|
|
715
617
|
get :public_timeline do
|
|
@@ -717,8 +619,7 @@ get :public_timeline do
|
|
|
717
619
|
end
|
|
718
620
|
```
|
|
719
621
|
|
|
720
|
-
Parameters are automatically populated from the request body on `POST` and `PUT` for form input, JSON and
|
|
721
|
-
XML content-types.
|
|
622
|
+
Parameters are automatically populated from the request body on `POST` and `PUT` for form input, JSON and XML content-types.
|
|
722
623
|
|
|
723
624
|
The request:
|
|
724
625
|
|
|
@@ -764,7 +665,7 @@ By default parameters are available as `ActiveSupport::HashWithIndifferentAccess
|
|
|
764
665
|
|
|
765
666
|
```ruby
|
|
766
667
|
class API < Grape::API
|
|
767
|
-
|
|
668
|
+
build_with :hashie_mash
|
|
768
669
|
|
|
769
670
|
params do
|
|
770
671
|
optional :color, type: String
|
|
@@ -778,16 +679,15 @@ The class can also be overridden on individual parameter blocks using `build_wit
|
|
|
778
679
|
|
|
779
680
|
```ruby
|
|
780
681
|
params do
|
|
781
|
-
build_with
|
|
682
|
+
build_with :hash
|
|
782
683
|
optional :color, type: String
|
|
783
684
|
end
|
|
784
685
|
```
|
|
785
686
|
|
|
786
|
-
Or globally with the [Configuration](#configuration) `Grape.configure.param_builder`.
|
|
787
|
-
|
|
788
687
|
In the example above, `params["color"]` will return `nil` since `params` is a plain `Hash`.
|
|
789
688
|
|
|
790
|
-
Available parameter builders are
|
|
689
|
+
Available parameter builders are `:hash`, `:hash_with_indifferent_access`, and `:hashie_mash`.
|
|
690
|
+
See [params_builder](lib/grape/params_builder).
|
|
791
691
|
|
|
792
692
|
### Declared
|
|
793
693
|
|
|
@@ -1057,8 +957,7 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/users/signup -d
|
|
|
1057
957
|
}
|
|
1058
958
|
````
|
|
1059
959
|
|
|
1060
|
-
Note that an attribute with a `nil` value is not considered *missing* and will also be returned
|
|
1061
|
-
when `include_missing` is set to `false`:
|
|
960
|
+
Note that an attribute with a `nil` value is not considered *missing* and will also be returned when `include_missing` is set to `false`:
|
|
1062
961
|
|
|
1063
962
|
**Request**
|
|
1064
963
|
|
|
@@ -1176,6 +1075,35 @@ curl -X POST -H "Content-Type: application/json" localhost:9292/child -d '{"chil
|
|
|
1176
1075
|
}
|
|
1177
1076
|
````
|
|
1178
1077
|
|
|
1078
|
+
### Parameter Precedence
|
|
1079
|
+
|
|
1080
|
+
Using `route_param` takes higher precedence over a regular parameter defined with same name:
|
|
1081
|
+
|
|
1082
|
+
```ruby
|
|
1083
|
+
params do
|
|
1084
|
+
requires :foo, type: String
|
|
1085
|
+
end
|
|
1086
|
+
route_param :foo do
|
|
1087
|
+
get do
|
|
1088
|
+
{ value: params[:foo] }
|
|
1089
|
+
end
|
|
1090
|
+
end
|
|
1091
|
+
```
|
|
1092
|
+
|
|
1093
|
+
**Request**
|
|
1094
|
+
|
|
1095
|
+
```bash
|
|
1096
|
+
curl -X POST -H "Content-Type: application/json" localhost:9292/bar -d '{"foo": "baz"}'
|
|
1097
|
+
```
|
|
1098
|
+
|
|
1099
|
+
**Response**
|
|
1100
|
+
|
|
1101
|
+
```json
|
|
1102
|
+
{
|
|
1103
|
+
"value": "bar"
|
|
1104
|
+
}
|
|
1105
|
+
```
|
|
1106
|
+
|
|
1179
1107
|
## Parameter Validation and Coercion
|
|
1180
1108
|
|
|
1181
1109
|
You can define validations and coercion options for your parameters using a `params` block.
|
|
@@ -1197,8 +1125,7 @@ put ':id' do
|
|
|
1197
1125
|
end
|
|
1198
1126
|
```
|
|
1199
1127
|
|
|
1200
|
-
When a type is specified an implicit validation is done after the coercion to ensure
|
|
1201
|
-
the output type is the one declared.
|
|
1128
|
+
When a type is specified an implicit validation is done after the coercion to ensure the output type is the one declared.
|
|
1202
1129
|
|
|
1203
1130
|
Optional parameters can have a default value.
|
|
1204
1131
|
|
|
@@ -1210,9 +1137,7 @@ params do
|
|
|
1210
1137
|
end
|
|
1211
1138
|
```
|
|
1212
1139
|
|
|
1213
|
-
Default values are eagerly evaluated. Above `:non_random_number` will evaluate to the same
|
|
1214
|
-
number for each call to the endpoint of this `params` block. To have the default evaluate
|
|
1215
|
-
lazily with each request use a lambda, like `:random_number` above.
|
|
1140
|
+
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.
|
|
1216
1141
|
|
|
1217
1142
|
Note that default values will be passed through to any validation options specified.
|
|
1218
1143
|
The following example will always fail if `:color` is not explicitly provided.
|
|
@@ -1231,6 +1156,15 @@ params do
|
|
|
1231
1156
|
end
|
|
1232
1157
|
```
|
|
1233
1158
|
|
|
1159
|
+
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.
|
|
1160
|
+
|
|
1161
|
+
```ruby
|
|
1162
|
+
params do
|
|
1163
|
+
optional :color, type: String, default: 'blue'
|
|
1164
|
+
optional :primary_color, type: String, default: -> (params) { params[:color] }
|
|
1165
|
+
end
|
|
1166
|
+
```
|
|
1167
|
+
|
|
1234
1168
|
### Supported Parameter Types
|
|
1235
1169
|
|
|
1236
1170
|
The following are all valid types, supported out of the box by Grape:
|
|
@@ -1248,36 +1182,9 @@ The following are all valid types, supported out of the box by Grape:
|
|
|
1248
1182
|
* Rack::Multipart::UploadedFile (alias `File`)
|
|
1249
1183
|
* JSON
|
|
1250
1184
|
|
|
1251
|
-
### Integer/Fixnum and Coercions
|
|
1252
|
-
|
|
1253
|
-
Please be aware that the behavior differs between Ruby 2.4 and earlier versions.
|
|
1254
|
-
In Ruby 2.4, values consisting of numbers are converted to Integer, but in earlier versions it will be treated as Fixnum.
|
|
1255
|
-
|
|
1256
|
-
```ruby
|
|
1257
|
-
params do
|
|
1258
|
-
requires :integers, type: Hash do
|
|
1259
|
-
requires :int, coerce: Integer
|
|
1260
|
-
end
|
|
1261
|
-
end
|
|
1262
|
-
get '/int' do
|
|
1263
|
-
params[:integers][:int].class
|
|
1264
|
-
end
|
|
1265
|
-
|
|
1266
|
-
...
|
|
1267
|
-
|
|
1268
|
-
get '/int' integers: { int: '45' }
|
|
1269
|
-
#=> Integer in ruby 2.4
|
|
1270
|
-
#=> Fixnum in earlier ruby versions
|
|
1271
|
-
```
|
|
1272
|
-
|
|
1273
1185
|
### Custom Types and Coercions
|
|
1274
1186
|
|
|
1275
|
-
Aside from the default set of supported types listed above, any class can be
|
|
1276
|
-
used as a type as long as an explicit coercion method is supplied. If the type
|
|
1277
|
-
implements a class-level `parse` method, Grape will use it automatically.
|
|
1278
|
-
This method must take one string argument and return an instance of the correct
|
|
1279
|
-
type, or return an instance of `Grape::Types::InvalidValue` which optionally
|
|
1280
|
-
accepts a message to be returned in the response.
|
|
1187
|
+
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.
|
|
1281
1188
|
|
|
1282
1189
|
```ruby
|
|
1283
1190
|
class Color
|
|
@@ -1287,7 +1194,7 @@ class Color
|
|
|
1287
1194
|
end
|
|
1288
1195
|
|
|
1289
1196
|
def self.parse(value)
|
|
1290
|
-
return new(value) if %w[blue red green]
|
|
1197
|
+
return new(value) if %w[blue red green].include?(value)
|
|
1291
1198
|
|
|
1292
1199
|
Grape::Types::InvalidValue.new('Unsupported color')
|
|
1293
1200
|
end
|
|
@@ -1305,10 +1212,7 @@ get '/stuff' do
|
|
|
1305
1212
|
end
|
|
1306
1213
|
```
|
|
1307
1214
|
|
|
1308
|
-
Alternatively, a custom coercion method may be supplied for any type of parameter
|
|
1309
|
-
using `coerce_with`. Any class or object may be given that implements a `parse` or
|
|
1310
|
-
`call` method, in that order of precedence. The method must accept a single string
|
|
1311
|
-
parameter, and the return value must match the given `type`.
|
|
1215
|
+
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`.
|
|
1312
1216
|
|
|
1313
1217
|
```ruby
|
|
1314
1218
|
params do
|
|
@@ -1332,9 +1236,7 @@ params do
|
|
|
1332
1236
|
end
|
|
1333
1237
|
```
|
|
1334
1238
|
|
|
1335
|
-
Grape will assert that coerced values match the given `type`, and will reject the request
|
|
1336
|
-
if they do not. To override this behaviour, custom types may implement a `parsed?` method
|
|
1337
|
-
that should accept a single argument and return `true` if the value passes type validation.
|
|
1239
|
+
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.
|
|
1338
1240
|
|
|
1339
1241
|
```ruby
|
|
1340
1242
|
class SecureUri
|
|
@@ -1369,9 +1271,7 @@ end
|
|
|
1369
1271
|
|
|
1370
1272
|
### First-Class `JSON` Types
|
|
1371
1273
|
|
|
1372
|
-
Grape supports complex parameters given as JSON-formatted strings using the special `type: JSON`
|
|
1373
|
-
declaration. JSON objects and arrays of objects are accepted equally, with nested validation
|
|
1374
|
-
rules applied to all objects in either case:
|
|
1274
|
+
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:
|
|
1375
1275
|
|
|
1376
1276
|
```ruby
|
|
1377
1277
|
params do
|
|
@@ -1390,8 +1290,7 @@ client.get('/', json: '{"int":4}') # => HTTP 400
|
|
|
1390
1290
|
client.get('/', json: '[{"int":4}]') # => HTTP 400
|
|
1391
1291
|
```
|
|
1392
1292
|
|
|
1393
|
-
Additionally `type: Array[JSON]` may be used, which explicitly marks the parameter as an array
|
|
1394
|
-
of objects. If a single object is supplied it will be wrapped.
|
|
1293
|
+
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.
|
|
1395
1294
|
|
|
1396
1295
|
```ruby
|
|
1397
1296
|
params do
|
|
@@ -1403,8 +1302,7 @@ get '/' do
|
|
|
1403
1302
|
params[:json].each { |obj| ... } # always works
|
|
1404
1303
|
end
|
|
1405
1304
|
```
|
|
1406
|
-
For stricter control over the type of JSON structure which may be supplied,
|
|
1407
|
-
use `type: Array, coerce_with: JSON` or `type: Hash, coerce_with: JSON`.
|
|
1305
|
+
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`.
|
|
1408
1306
|
|
|
1409
1307
|
### Multiple Allowed Types
|
|
1410
1308
|
|
|
@@ -1423,8 +1321,7 @@ client.get('/', status_code: 300) # => 300
|
|
|
1423
1321
|
client.get('/', status_code: %w(404 NOT FOUND)) # => [404, "NOT", "FOUND"]
|
|
1424
1322
|
```
|
|
1425
1323
|
|
|
1426
|
-
As a special case, variant-member-type collections may also be declared, by
|
|
1427
|
-
passing a `Set` or `Array` with more than one member to `type`:
|
|
1324
|
+
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`:
|
|
1428
1325
|
|
|
1429
1326
|
```ruby
|
|
1430
1327
|
params do
|
|
@@ -1437,14 +1334,61 @@ end
|
|
|
1437
1334
|
client.get('/', status_codes: %w(1 two)) # => [1, "two"]
|
|
1438
1335
|
```
|
|
1439
1336
|
|
|
1337
|
+
### Multiple Hash Schemas with `oneof`
|
|
1338
|
+
|
|
1339
|
+
A Hash parameter that may take one of several different shapes can be declared with the `oneof:` option. Each variant is a `Proc` that uses the normal `params` DSL — so the full validator surface (`requires`, `optional`, `regexp:`, `values:`, `allow_blank:`, nested `Hash`/`Array`, etc.) is available inside.
|
|
1340
|
+
|
|
1341
|
+
```ruby
|
|
1342
|
+
params do
|
|
1343
|
+
requires :value, type: Hash, oneof: [
|
|
1344
|
+
proc { requires :fixed_price, type: Float },
|
|
1345
|
+
proc do
|
|
1346
|
+
requires :time_unit, type: String
|
|
1347
|
+
requires :rate, type: Float
|
|
1348
|
+
end
|
|
1349
|
+
]
|
|
1350
|
+
end
|
|
1351
|
+
post '/pricing' do
|
|
1352
|
+
params[:value]
|
|
1353
|
+
end
|
|
1354
|
+
```
|
|
1355
|
+
|
|
1356
|
+
Both of these requests succeed:
|
|
1357
|
+
|
|
1358
|
+
```bash
|
|
1359
|
+
curl -d '{"value":{"fixed_price":100.0}}' -H 'Content-Type: application/json' /pricing
|
|
1360
|
+
curl -d '{"value":{"time_unit":"hour","rate":50.0}}' -H 'Content-Type: application/json' /pricing
|
|
1361
|
+
```
|
|
1362
|
+
|
|
1363
|
+
A request that doesn't match any variant returns `400` with `value does not match any of the allowed schemas`.
|
|
1364
|
+
|
|
1365
|
+
Variants are tried in declaration order; the first variant that validates without errors wins, and any coercions it performed (e.g. `"150.5"` → `150.5`) are applied to the request params. Nested hash structures inside variants work the same as elsewhere in Grape:
|
|
1366
|
+
|
|
1367
|
+
```ruby
|
|
1368
|
+
params do
|
|
1369
|
+
requires :options, type: Hash, oneof: [
|
|
1370
|
+
proc do
|
|
1371
|
+
requires :form, type: Hash do
|
|
1372
|
+
requires :colour, type: String
|
|
1373
|
+
optional :size, type: Integer
|
|
1374
|
+
end
|
|
1375
|
+
end,
|
|
1376
|
+
proc do
|
|
1377
|
+
requires :api, type: Hash do
|
|
1378
|
+
requires :authenticated, type: Grape::API::Boolean
|
|
1379
|
+
end
|
|
1380
|
+
end
|
|
1381
|
+
]
|
|
1382
|
+
end
|
|
1383
|
+
```
|
|
1384
|
+
|
|
1385
|
+
`oneof:` requires `type: Hash`. The variants array must be non-empty and each entry must be a `Proc`; violating either raises an `ArgumentError` at definition time.
|
|
1386
|
+
|
|
1440
1387
|
### Validation of Nested Parameters
|
|
1441
1388
|
|
|
1442
1389
|
Parameters can be nested using `group` or by calling `requires` or `optional` with a block.
|
|
1443
|
-
In the [above example](#parameter-validation-and-coercion), this means `params[:media][:url]` is required along with `params[:id]`,
|
|
1444
|
-
and `
|
|
1445
|
-
With a block, `group`, `requires` and `optional` accept an additional option `type` which can
|
|
1446
|
-
be either `Array` or `Hash`, and defaults to `Array`. Depending on the value, the nested
|
|
1447
|
-
parameters will be treated either as values of a hash or as values of hashes in an array.
|
|
1390
|
+
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.
|
|
1391
|
+
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.
|
|
1448
1392
|
|
|
1449
1393
|
```ruby
|
|
1450
1394
|
params do
|
|
@@ -1462,9 +1406,7 @@ end
|
|
|
1462
1406
|
|
|
1463
1407
|
### Dependent Parameters
|
|
1464
1408
|
|
|
1465
|
-
Suppose some of your parameters are only relevant if another parameter is given;
|
|
1466
|
-
Grape allows you to express this relationship through the `given` method in your
|
|
1467
|
-
parameters block, like so:
|
|
1409
|
+
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:
|
|
1468
1410
|
|
|
1469
1411
|
```ruby
|
|
1470
1412
|
params do
|
|
@@ -1503,31 +1445,45 @@ Note: param in `given` should be the renamed one. In the example, it should be `
|
|
|
1503
1445
|
|
|
1504
1446
|
### Group Options
|
|
1505
1447
|
|
|
1506
|
-
Parameters options can be grouped. It can be useful if you want to extract
|
|
1507
|
-
|
|
1508
|
-
|
|
1448
|
+
Parameters options can be grouped. It can be useful if you want to extract common validation or types for several parameters.
|
|
1449
|
+
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.
|
|
1450
|
+
|
|
1451
|
+
The example below presents a typical case when parameters share common options.
|
|
1509
1452
|
|
|
1510
1453
|
```ruby
|
|
1511
1454
|
params do
|
|
1512
|
-
requires :first_name, type: String, regexp: /w+/, desc: 'First name'
|
|
1513
|
-
|
|
1514
|
-
requires :last_name, type: String, regexp: /w+/, desc: 'Last name'
|
|
1455
|
+
requires :first_name, type: String, regexp: /w+/, desc: 'First name', documentation: { in: 'body' }
|
|
1456
|
+
optional :middle_name, type: String, regexp: /w+/, desc: 'Middle name', documentation: { in: 'body', x: { nullable: true } }
|
|
1457
|
+
requires :last_name, type: String, regexp: /w+/, desc: 'Last name', documentation: { in: 'body' }
|
|
1515
1458
|
end
|
|
1516
1459
|
```
|
|
1517
1460
|
|
|
1518
|
-
Grape allows you to present the same logic through the `with` method in your
|
|
1519
|
-
parameters block, like so:
|
|
1461
|
+
Grape allows you to present the same logic through the `with` method in your parameters block, like so:
|
|
1520
1462
|
|
|
1521
1463
|
```ruby
|
|
1522
1464
|
params do
|
|
1523
|
-
with(type: String, regexp: /w
|
|
1465
|
+
with(type: String, regexp: /w+/, documentation: { in: 'body' }) do
|
|
1524
1466
|
requires :first_name, desc: 'First name'
|
|
1525
|
-
|
|
1467
|
+
optional :middle_name, desc: 'Middle name', documentation: { x: { nullable: true } }
|
|
1526
1468
|
requires :last_name, desc: 'Last name'
|
|
1527
1469
|
end
|
|
1528
1470
|
end
|
|
1529
1471
|
```
|
|
1530
1472
|
|
|
1473
|
+
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.
|
|
1474
|
+
|
|
1475
|
+
```ruby
|
|
1476
|
+
params do
|
|
1477
|
+
with(documentation: { in: 'body' }) do # Applies documentation to all nested parameters
|
|
1478
|
+
with(type: String, regexp: /\w+/) do # Applies type and validation to names
|
|
1479
|
+
requires :first_name, desc: 'First name'
|
|
1480
|
+
requires :last_name, desc: 'Last name'
|
|
1481
|
+
end
|
|
1482
|
+
optional :age, type: Integer, desc: 'Age', documentation: { x: { nullable: true } } # Specific settings for 'age'
|
|
1483
|
+
end
|
|
1484
|
+
end
|
|
1485
|
+
```
|
|
1486
|
+
|
|
1531
1487
|
### Renaming
|
|
1532
1488
|
|
|
1533
1489
|
You can rename parameters using `as`, which can be useful when refactoring existing APIs:
|
|
@@ -1550,13 +1506,9 @@ The value passed to `as` will be the key when calling `declared(params)`.
|
|
|
1550
1506
|
|
|
1551
1507
|
#### `allow_blank`
|
|
1552
1508
|
|
|
1553
|
-
Parameters can be defined as `allow_blank`, ensuring that they contain a value. By default, `requires`
|
|
1554
|
-
only validates that a parameter was sent in the request, regardless its value. With `allow_blank: false`,
|
|
1555
|
-
empty values or whitespace only values are invalid.
|
|
1509
|
+
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.
|
|
1556
1510
|
|
|
1557
|
-
`allow_blank` can be combined with both `requires` and `optional`. If the parameter is required, it has to contain
|
|
1558
|
-
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
|
|
1559
|
-
some value, and not an empty string/only whitespaces.
|
|
1511
|
+
`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.
|
|
1560
1512
|
|
|
1561
1513
|
|
|
1562
1514
|
```ruby
|
|
@@ -1593,7 +1545,7 @@ Note endless ranges are also supported with ActiveSupport >= 6.0, but they requi
|
|
|
1593
1545
|
```ruby
|
|
1594
1546
|
params do
|
|
1595
1547
|
requires :minimum, type: Integer, values: 10..
|
|
1596
|
-
optional :maximum, type: Integer, values: ..10
|
|
1548
|
+
optional :maximum, type: Integer, values: ..10
|
|
1597
1549
|
end
|
|
1598
1550
|
```
|
|
1599
1551
|
|
|
@@ -1607,11 +1559,9 @@ end
|
|
|
1607
1559
|
```
|
|
1608
1560
|
|
|
1609
1561
|
The `:values` option can also be supplied with a `Proc`, evaluated lazily with each request.
|
|
1610
|
-
If the Proc has arity zero (i.e. it takes no arguments) it is expected to return either a list
|
|
1611
|
-
or a range which will then be used to validate the parameter.
|
|
1562
|
+
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.
|
|
1612
1563
|
|
|
1613
|
-
For example, given a status model you may want to restrict by hashtags that you have
|
|
1614
|
-
previously defined in the `HashTag` model.
|
|
1564
|
+
For example, given a status model you may want to restrict by hashtags that you have previously defined in the `HashTag` model.
|
|
1615
1565
|
|
|
1616
1566
|
```ruby
|
|
1617
1567
|
params do
|
|
@@ -1619,10 +1569,7 @@ params do
|
|
|
1619
1569
|
end
|
|
1620
1570
|
```
|
|
1621
1571
|
|
|
1622
|
-
Alternatively, a Proc with arity one (i.e. taking one argument) can be used to explicitly validate
|
|
1623
|
-
each parameter value. In that case, the Proc is expected to return a truthy value if the parameter
|
|
1624
|
-
value is valid. The parameter will be considered invalid if the Proc returns a falsy value or if it
|
|
1625
|
-
raises a StandardError.
|
|
1572
|
+
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.
|
|
1626
1573
|
|
|
1627
1574
|
```ruby
|
|
1628
1575
|
params do
|
|
@@ -1632,21 +1579,21 @@ end
|
|
|
1632
1579
|
|
|
1633
1580
|
While Procs are convenient for single cases, consider using [Custom Validators](#custom-validators) in cases where a validation is used more than once.
|
|
1634
1581
|
|
|
1635
|
-
|
|
1582
|
+
When using `optional` together with `:values`, a missing key, a `nil` value, and any value that coerces to `nil` (such as `""` for `type: Symbol`) all pass validation — `optional` collapses "key may be absent" with "value may be nil". To reject blank values while still allowing the key to be absent, add `allow_blank: false`:
|
|
1636
1583
|
|
|
1637
1584
|
```ruby
|
|
1638
1585
|
params do
|
|
1639
|
-
|
|
1586
|
+
optional :state, type: Symbol, values: [:active, :inactive], allow_blank: false
|
|
1640
1587
|
end
|
|
1641
1588
|
```
|
|
1642
1589
|
|
|
1590
|
+
With `requires`, blank values are already rejected: `requires` enforces presence and `:values` rejects `nil`.
|
|
1591
|
+
|
|
1643
1592
|
#### `except_values`
|
|
1644
1593
|
|
|
1645
1594
|
Parameters can be restricted from having a specific set of values with the `:except_values` option.
|
|
1646
1595
|
|
|
1647
|
-
The `except_values` validator behaves similarly to the `values` validator in that it accepts either
|
|
1648
|
-
an Array, a Range, or a Proc. Unlike the `values` validator, however, `except_values` only accepts
|
|
1649
|
-
Procs with arity zero.
|
|
1596
|
+
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.
|
|
1650
1597
|
|
|
1651
1598
|
```ruby
|
|
1652
1599
|
params do
|
|
@@ -1667,11 +1614,24 @@ params do
|
|
|
1667
1614
|
end
|
|
1668
1615
|
```
|
|
1669
1616
|
|
|
1617
|
+
#### `length`
|
|
1618
|
+
|
|
1619
|
+
Parameters with types that support `#length` method can be restricted to have a specific length with the `:length` option.
|
|
1620
|
+
|
|
1621
|
+
The validator accepts `:min` or `:max` or both options or only `:is` to validate that the value of the parameter is within the given limits.
|
|
1622
|
+
|
|
1623
|
+
```ruby
|
|
1624
|
+
params do
|
|
1625
|
+
requires :code, type: String, length: { is: 2 }
|
|
1626
|
+
requires :str, type: String, length: { min: 3 }
|
|
1627
|
+
requires :list, type: [Integer], length: { min: 3, max: 5 }
|
|
1628
|
+
requires :hash, type: Hash, length: { max: 5 }
|
|
1629
|
+
end
|
|
1630
|
+
```
|
|
1631
|
+
|
|
1670
1632
|
#### `regexp`
|
|
1671
1633
|
|
|
1672
|
-
Parameters can be restricted to match a specific regular expression with the `:regexp` option. If the value
|
|
1673
|
-
does not match the regular expression an error will be returned. Note that this is true for both `requires`
|
|
1674
|
-
and `optional` parameters.
|
|
1634
|
+
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.
|
|
1675
1635
|
|
|
1676
1636
|
```ruby
|
|
1677
1637
|
params do
|
|
@@ -1806,8 +1766,7 @@ namespace :statuses do
|
|
|
1806
1766
|
end
|
|
1807
1767
|
```
|
|
1808
1768
|
|
|
1809
|
-
The `namespace` method has a number of aliases, including: `group`, `resource`,
|
|
1810
|
-
`resources`, and `segment`. Use whichever reads the best for your API.
|
|
1769
|
+
The `namespace` method has a number of aliases, including: `group`, `resource`, `resources`, and `segment`. Use whichever reads the best for your API.
|
|
1811
1770
|
|
|
1812
1771
|
You can conveniently define a route parameter as a namespace using `route_param`.
|
|
1813
1772
|
|
|
@@ -1844,9 +1803,9 @@ end
|
|
|
1844
1803
|
```ruby
|
|
1845
1804
|
class AlphaNumeric < Grape::Validations::Validators::Base
|
|
1846
1805
|
def validate_param!(attr_name, params)
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1806
|
+
return if params[attr_name].match?(/\A[[:alnum:]]+\z/)
|
|
1807
|
+
|
|
1808
|
+
validation_error!(attr_name, 'must consist of alpha-numeric characters')
|
|
1850
1809
|
end
|
|
1851
1810
|
end
|
|
1852
1811
|
```
|
|
@@ -1862,9 +1821,9 @@ You can also create custom classes that take parameters.
|
|
|
1862
1821
|
```ruby
|
|
1863
1822
|
class Length < Grape::Validations::Validators::Base
|
|
1864
1823
|
def validate_param!(attr_name, params)
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1824
|
+
return if params[attr_name].length <= @options
|
|
1825
|
+
|
|
1826
|
+
validation_error!(attr_name, "must be at the most #{@options} characters long")
|
|
1868
1827
|
end
|
|
1869
1828
|
end
|
|
1870
1829
|
```
|
|
@@ -1886,10 +1845,10 @@ class Admin < Grape::Validations::Validators::Base
|
|
|
1886
1845
|
# @attrs being [:admin_field] and once with @attrs being [:admin_false_field]
|
|
1887
1846
|
return unless request.params.key?(@attrs.first)
|
|
1888
1847
|
# check if admin flag is set to true
|
|
1889
|
-
return unless @
|
|
1848
|
+
return unless @options
|
|
1890
1849
|
# check if user is admin or not
|
|
1891
1850
|
# as an example get a token from request and check if it's admin or not
|
|
1892
|
-
|
|
1851
|
+
validation_error!(@attrs, 'Can not set admin-only field.') unless request.headers['X-Access-Token'] == 'admin'
|
|
1893
1852
|
end
|
|
1894
1853
|
end
|
|
1895
1854
|
```
|
|
@@ -1904,7 +1863,50 @@ params do
|
|
|
1904
1863
|
end
|
|
1905
1864
|
```
|
|
1906
1865
|
|
|
1907
|
-
|
|
1866
|
+
Each validator is instantiated once at route definition time and frozen. Any setup (option parsing, message building) should happen in `initialize`, not in `validate_param!` or `validate`.
|
|
1867
|
+
|
|
1868
|
+
#### Available helpers
|
|
1869
|
+
|
|
1870
|
+
The following protected/private helpers are available in any `Grape::Validations::Validators::Base` subclass:
|
|
1871
|
+
|
|
1872
|
+
| Helper | Description |
|
|
1873
|
+
|---|---|
|
|
1874
|
+
| `default_message_key(key)` | Class-level macro. Declares the default I18n key for `validation_error!`. A per-option `:message` override still takes precedence. |
|
|
1875
|
+
| `validation_error!(attr_name_or_params, message = @exception_message)` | Raises `Grape::Exceptions::Validation`. Accepts a single attribute name or a pre-computed array of full param names. |
|
|
1876
|
+
| `@options` | The validator option value, deep-frozen at initialization. |
|
|
1877
|
+
| `@attrs` | Frozen array of attribute names this validator applies to. |
|
|
1878
|
+
| `@scope` | The `ParamsScope` — use `@scope.full_name(attr_name)` for the fully-qualified param name. |
|
|
1879
|
+
| `option_value` | Returns `@options[:value]` if present, otherwise `@options`. |
|
|
1880
|
+
| `options_key?(key)` | Returns true if `@options` is a hash with a non-nil `key`. |
|
|
1881
|
+
| `hash_like?(obj)` | Returns true if `obj` responds to `key?`. |
|
|
1882
|
+
| `scrub(value)` | Returns `value` with invalid byte sequences scrubbed. |
|
|
1883
|
+
| `translate(key, **opts)` | I18n lookup with `:en` fallback and `grape.errors.messages` scope. Called at request time to respect per-request locale. |
|
|
1884
|
+
|
|
1885
|
+
Use `default_message_key` for a fixed I18n key. The message is resolved once at route definition time via `message`, so a per-option `:message` override still wins:
|
|
1886
|
+
|
|
1887
|
+
```ruby
|
|
1888
|
+
class SpecialValidator < Grape::Validations::Validators::Base
|
|
1889
|
+
default_message_key :special
|
|
1890
|
+
|
|
1891
|
+
def validate_param!(attr_name, params)
|
|
1892
|
+
return if valid?(params[attr_name])
|
|
1893
|
+
|
|
1894
|
+
validation_error!(attr_name)
|
|
1895
|
+
end
|
|
1896
|
+
end
|
|
1897
|
+
```
|
|
1898
|
+
|
|
1899
|
+
For interpolated messages that must respect per-request locale, call `translate` directly inside `validate_param!`:
|
|
1900
|
+
|
|
1901
|
+
```ruby
|
|
1902
|
+
class SpecialValidator < Grape::Validations::Validators::Base
|
|
1903
|
+
def validate_param!(attr_name, params)
|
|
1904
|
+
return if valid?(params[attr_name])
|
|
1905
|
+
|
|
1906
|
+
validation_error!(attr_name, translate(:special, min: 2, max: 10))
|
|
1907
|
+
end
|
|
1908
|
+
end
|
|
1909
|
+
```
|
|
1908
1910
|
|
|
1909
1911
|
### Validation Errors
|
|
1910
1912
|
|
|
@@ -1947,8 +1949,8 @@ To skip all subsequent validation checks when a specific param is found invalid,
|
|
|
1947
1949
|
The following example will not check if `:wine` is present unless it finds `:beer`.
|
|
1948
1950
|
```ruby
|
|
1949
1951
|
params do
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
+
requires :beer, fail_fast: true
|
|
1953
|
+
requires :wine
|
|
1952
1954
|
end
|
|
1953
1955
|
```
|
|
1954
1956
|
The result of empty params would be a single `Grape::Exceptions::ValidationErrors` error.
|
|
@@ -1956,17 +1958,38 @@ The result of empty params would be a single `Grape::Exceptions::ValidationError
|
|
|
1956
1958
|
Similarly, no regular expression test will be performed if `:blah` is blank in the following example.
|
|
1957
1959
|
```ruby
|
|
1958
1960
|
params do
|
|
1959
|
-
|
|
1961
|
+
requires :blah, allow_blank: false, regexp: /blah/, fail_fast: true
|
|
1960
1962
|
end
|
|
1961
1963
|
```
|
|
1962
1964
|
|
|
1963
1965
|
### I18n
|
|
1964
1966
|
|
|
1965
|
-
Grape supports I18n for parameter-related error messages, but will fallback to English if
|
|
1966
|
-
translations for the default locale have not been provided. See [en.yml](lib/grape/locale/en.yml) for message keys.
|
|
1967
|
+
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.
|
|
1967
1968
|
|
|
1968
1969
|
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.
|
|
1969
1970
|
|
|
1971
|
+
Custom validators that inherit from `Grape::Validations::Validators::Base` have access to a `translate` helper (see `Grape::Util::Translation`) and should use it instead of calling `I18n` directly. It applies the same `:en` fallback as built-in validators, defaults `scope` to `'grape.errors.messages'`, and handles interpolation without needing `format`:
|
|
1972
|
+
|
|
1973
|
+
```ruby
|
|
1974
|
+
# Good — scope defaults to 'grape.errors.messages', interpolation forwarded automatically
|
|
1975
|
+
translate(:special, min: 2, max: 10)
|
|
1976
|
+
|
|
1977
|
+
# Bad — format is unnecessary and risks conflicting with I18n reserved keys
|
|
1978
|
+
format I18n.t(:special, scope: 'grape.errors.messages'), min: 2, max: 10
|
|
1979
|
+
```
|
|
1980
|
+
|
|
1981
|
+
Example custom validator using an interpolated i18n message:
|
|
1982
|
+
|
|
1983
|
+
```ruby
|
|
1984
|
+
class SpecialValidator < Grape::Validations::Validators::Base
|
|
1985
|
+
def validate_param!(attr_name, params)
|
|
1986
|
+
return if valid?(params[attr_name])
|
|
1987
|
+
|
|
1988
|
+
validation_error!(attr_name, translate(:special, min: 2, max: 10))
|
|
1989
|
+
end
|
|
1990
|
+
end
|
|
1991
|
+
```
|
|
1992
|
+
|
|
1970
1993
|
### Custom Validation messages
|
|
1971
1994
|
|
|
1972
1995
|
Grape supports custom validation messages for parameter-related and coerce-related error messages.
|
|
@@ -1988,6 +2011,16 @@ params do
|
|
|
1988
2011
|
end
|
|
1989
2012
|
```
|
|
1990
2013
|
|
|
2014
|
+
#### `length`
|
|
2015
|
+
|
|
2016
|
+
```ruby
|
|
2017
|
+
params do
|
|
2018
|
+
requires :code, type: String, length: { is: 2, message: 'code is expected to be exactly 2 characters long' }
|
|
2019
|
+
requires :str, type: String, length: { min: 5, message: 'str is expected to be at least 5 characters long' }
|
|
2020
|
+
requires :list, type: [Integer], length: { min: 2, max: 3, message: 'list is expected to have between 2 and 3 elements' }
|
|
2021
|
+
end
|
|
2022
|
+
```
|
|
2023
|
+
|
|
1991
2024
|
#### `all_or_none_of`
|
|
1992
2025
|
|
|
1993
2026
|
```ruby
|
|
@@ -2096,6 +2129,40 @@ params do
|
|
|
2096
2129
|
end
|
|
2097
2130
|
```
|
|
2098
2131
|
|
|
2132
|
+
### Using `dry-validation` or `dry-schema`
|
|
2133
|
+
|
|
2134
|
+
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`.
|
|
2135
|
+
|
|
2136
|
+
Then call `contract` with a contract or schema defined previously:
|
|
2137
|
+
|
|
2138
|
+
```rb
|
|
2139
|
+
CreateOrdersSchema = Dry::Schema.Params do
|
|
2140
|
+
required(:orders).array(:hash) do
|
|
2141
|
+
required(:name).filled(:string)
|
|
2142
|
+
optional(:volume).maybe(:integer, lt?: 9)
|
|
2143
|
+
end
|
|
2144
|
+
end
|
|
2145
|
+
|
|
2146
|
+
# ...
|
|
2147
|
+
|
|
2148
|
+
contract CreateOrdersSchema
|
|
2149
|
+
```
|
|
2150
|
+
|
|
2151
|
+
or with a block, using the [schema definition syntax](https://dry-rb.org/gems/dry-schema/1.13/#quick-start):
|
|
2152
|
+
|
|
2153
|
+
```rb
|
|
2154
|
+
contract do
|
|
2155
|
+
required(:orders).array(:hash) do
|
|
2156
|
+
required(:name).filled(:string)
|
|
2157
|
+
optional(:volume).maybe(:integer, lt?: 9)
|
|
2158
|
+
end
|
|
2159
|
+
end
|
|
2160
|
+
```
|
|
2161
|
+
|
|
2162
|
+
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.
|
|
2163
|
+
|
|
2164
|
+
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.
|
|
2165
|
+
|
|
2099
2166
|
## Headers
|
|
2100
2167
|
|
|
2101
2168
|
### Request
|
|
@@ -2123,8 +2190,9 @@ curl -H "secret_PassWord: swordfish" ...
|
|
|
2123
2190
|
|
|
2124
2191
|
The header name will have been normalized for you.
|
|
2125
2192
|
|
|
2126
|
-
- In the `header` helper names will be coerced into a
|
|
2127
|
-
- In the `
|
|
2193
|
+
- In the `header` helper names will be coerced into a downcased kebab case as `secret-password` if using Rack 3.
|
|
2194
|
+
- In the `header` helper names will be coerced into a capitalized kebab case as `Secret-PassWord` if using Rack < 3.
|
|
2195
|
+
- In the `env` collection they appear in all uppercase, in snake case, and prefixed with 'HTTP_' as `HTTP_SECRET_PASSWORD`
|
|
2128
2196
|
|
|
2129
2197
|
The header name will have been normalized per HTTP standards defined in [RFC2616 Section 4.2](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2) regardless of what is being sent by a client.
|
|
2130
2198
|
|
|
@@ -2194,8 +2262,7 @@ namespace ':id' do
|
|
|
2194
2262
|
end
|
|
2195
2263
|
```
|
|
2196
2264
|
|
|
2197
|
-
Optionally, you can define requirements for your named route parameters using regular
|
|
2198
|
-
expressions on namespace or endpoint. The route will match only if all requirements are met.
|
|
2265
|
+
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.
|
|
2199
2266
|
|
|
2200
2267
|
```ruby
|
|
2201
2268
|
get ':id', requirements: { id: /[0-9]*/ } do
|
|
@@ -2213,8 +2280,7 @@ end
|
|
|
2213
2280
|
|
|
2214
2281
|
## Helpers
|
|
2215
2282
|
|
|
2216
|
-
You can define helper methods that your endpoints can use with the `helpers`
|
|
2217
|
-
macro by either giving a block or an array of modules.
|
|
2283
|
+
You can define helper methods that your endpoints can use with the `helpers` macro by either giving a block or an array of modules.
|
|
2218
2284
|
|
|
2219
2285
|
```ruby
|
|
2220
2286
|
module StatusHelpers
|
|
@@ -2453,11 +2519,36 @@ end
|
|
|
2453
2519
|
API.recognize_path '/statuses'
|
|
2454
2520
|
```
|
|
2455
2521
|
|
|
2522
|
+
Since version `2.1.0`, the `recognize_path` method takes into account the parameters type to determine which endpoint should match with given path.
|
|
2523
|
+
|
|
2524
|
+
```ruby
|
|
2525
|
+
class Books < Grape::API
|
|
2526
|
+
resource :books do
|
|
2527
|
+
route_param :id, type: Integer do
|
|
2528
|
+
# GET /books/:id
|
|
2529
|
+
get do
|
|
2530
|
+
#...
|
|
2531
|
+
end
|
|
2532
|
+
end
|
|
2533
|
+
|
|
2534
|
+
resource :share do
|
|
2535
|
+
# POST /books/share
|
|
2536
|
+
post do
|
|
2537
|
+
# ....
|
|
2538
|
+
end
|
|
2539
|
+
end
|
|
2540
|
+
end
|
|
2541
|
+
end
|
|
2542
|
+
|
|
2543
|
+
API.recognize_path '/books/1' # => /books/:id
|
|
2544
|
+
API.recognize_path '/books/share' # => /books/share
|
|
2545
|
+
API.recognize_path '/books/other' # => nil
|
|
2546
|
+
```
|
|
2547
|
+
|
|
2548
|
+
|
|
2456
2549
|
## Allowed Methods
|
|
2457
2550
|
|
|
2458
|
-
When you add a `GET` route for a resource, a route for the `HEAD`
|
|
2459
|
-
method will also be added automatically. You can disable this
|
|
2460
|
-
behavior with `do_not_route_head!`.
|
|
2551
|
+
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!`.
|
|
2461
2552
|
|
|
2462
2553
|
``` ruby
|
|
2463
2554
|
class API < Grape::API
|
|
@@ -2469,11 +2560,7 @@ class API < Grape::API
|
|
|
2469
2560
|
end
|
|
2470
2561
|
```
|
|
2471
2562
|
|
|
2472
|
-
When you add a route for a resource, a route for the `OPTIONS`
|
|
2473
|
-
method will also be added. The response to an OPTIONS request will
|
|
2474
|
-
include an "Allow" header listing the supported methods. If the resource
|
|
2475
|
-
has `before` and `after` callbacks they will be executed, but no other callbacks will
|
|
2476
|
-
run.
|
|
2563
|
+
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.
|
|
2477
2564
|
|
|
2478
2565
|
```ruby
|
|
2479
2566
|
class API < Grape::API
|
|
@@ -2502,10 +2589,7 @@ curl -v -X OPTIONS http://localhost:3000/rt_count
|
|
|
2502
2589
|
|
|
2503
2590
|
You can disable this behavior with `do_not_route_options!`.
|
|
2504
2591
|
|
|
2505
|
-
If a request for a resource is made with an unsupported HTTP method, an
|
|
2506
|
-
HTTP 405 (Method Not Allowed) response will be returned. If the resource
|
|
2507
|
-
has `before` callbacks they will be executed, but no other callbacks will
|
|
2508
|
-
run.
|
|
2592
|
+
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.
|
|
2509
2593
|
|
|
2510
2594
|
``` shell
|
|
2511
2595
|
curl -X DELETE -v http://localhost:3000/rt_count/
|
|
@@ -2531,8 +2615,7 @@ Anything that responds to `#to_s` can be given as a first argument to `error!`.
|
|
|
2531
2615
|
error! :not_found, 404
|
|
2532
2616
|
```
|
|
2533
2617
|
|
|
2534
|
-
You can also return JSON formatted objects by raising error! and passing a hash
|
|
2535
|
-
instead of a message.
|
|
2618
|
+
You can also return JSON formatted objects by raising error! and passing a hash instead of a message.
|
|
2536
2619
|
|
|
2537
2620
|
```ruby
|
|
2538
2621
|
error!({ error: 'unexpected error', detail: 'missing widget' }, 500)
|
|
@@ -2544,7 +2627,7 @@ You can set additional headers for the response. They will be merged with header
|
|
|
2544
2627
|
error!('Something went wrong', 500, 'X-Error-Detail' => 'Invalid token.')
|
|
2545
2628
|
```
|
|
2546
2629
|
|
|
2547
|
-
You can present documented errors with a Grape entity using the
|
|
2630
|
+
You can present documented errors with a Grape entity using the [grape-entity](https://github.com/ruby-grape/grape-entity) gem.
|
|
2548
2631
|
|
|
2549
2632
|
```ruby
|
|
2550
2633
|
module API
|
|
@@ -2597,8 +2680,7 @@ route :any, '*path' do
|
|
|
2597
2680
|
end
|
|
2598
2681
|
```
|
|
2599
2682
|
|
|
2600
|
-
It is very crucial to __define this endpoint at the very end of your API__, as it
|
|
2601
|
-
literally accepts every request.
|
|
2683
|
+
It is very crucial to __define this endpoint at the very end of your API__, as it literally accepts every request.
|
|
2602
2684
|
|
|
2603
2685
|
## Exception Handling
|
|
2604
2686
|
|
|
@@ -2656,12 +2738,12 @@ end
|
|
|
2656
2738
|
|
|
2657
2739
|
The error format will match the request format. See "Content-Types" below.
|
|
2658
2740
|
|
|
2659
|
-
Custom error formatters for existing and additional types can be defined with a proc.
|
|
2741
|
+
Custom error formatters for existing and additional types can be defined with a proc. The formatter receives a `Grape::Exceptions::ErrorResponse` value object as `error:` plus three context kwargs — `env:`, `include_backtrace:`, `include_original_exception:`. Pull just the keys you need with `**` to ignore the rest:
|
|
2660
2742
|
|
|
2661
2743
|
```ruby
|
|
2662
2744
|
class Twitter::API < Grape::API
|
|
2663
|
-
error_formatter :txt, ->(
|
|
2664
|
-
"error: #{message} from #{backtrace}"
|
|
2745
|
+
error_formatter :txt, ->(error:, **) {
|
|
2746
|
+
"error #{error.status}: #{error.message} from #{error.backtrace}"
|
|
2665
2747
|
}
|
|
2666
2748
|
end
|
|
2667
2749
|
```
|
|
@@ -2670,8 +2752,8 @@ You can also use a module or class.
|
|
|
2670
2752
|
|
|
2671
2753
|
```ruby
|
|
2672
2754
|
module CustomFormatter
|
|
2673
|
-
def self.call(
|
|
2674
|
-
{ message: message, backtrace: backtrace }
|
|
2755
|
+
def self.call(error:, **)
|
|
2756
|
+
{ status: error.status, message: error.message, backtrace: error.backtrace }
|
|
2675
2757
|
end
|
|
2676
2758
|
end
|
|
2677
2759
|
|
|
@@ -2701,6 +2783,41 @@ class Twitter::API < Grape::API
|
|
|
2701
2783
|
end
|
|
2702
2784
|
```
|
|
2703
2785
|
|
|
2786
|
+
#### Re-raising from inside a `rescue_from` block
|
|
2787
|
+
|
|
2788
|
+
A `rescue_from` block can re-raise an exception to invoke a different handler. This is useful for translating one exception class into another:
|
|
2789
|
+
|
|
2790
|
+
```ruby
|
|
2791
|
+
class Twitter::API < Grape::API
|
|
2792
|
+
rescue_from Grape::Exceptions::ValidationErrors do |e|
|
|
2793
|
+
raise Api::Exceptions::InvalidValueError, e.full_messages
|
|
2794
|
+
end
|
|
2795
|
+
|
|
2796
|
+
rescue_from Api::Exceptions::InvalidValueError do |e|
|
|
2797
|
+
error!({ errors: e.message }, 422)
|
|
2798
|
+
end
|
|
2799
|
+
end
|
|
2800
|
+
```
|
|
2801
|
+
|
|
2802
|
+
The first handler re-raises; the second handler runs against the new exception.
|
|
2803
|
+
|
|
2804
|
+
If the re-raised exception has no registered `rescue_from` and is a `Grape::Exceptions::Base` subclass, it is rendered through the default Grape error path (using its own `status` and `message`). Anything else — typos, `NoMethodError`, an unrelated `StandardError` — is treated as an internal error: it is exposed on `env['grape.exception']` for upstream Rack middleware to observe, and rendered to the API consumer as a generic `500 Internal Server Error`. This avoids leaking internal detail in the response body.
|
|
2805
|
+
|
|
2806
|
+
You can take control of the internal-error path by opting in with `rescue_from :internal_grape_exceptions`:
|
|
2807
|
+
|
|
2808
|
+
```ruby
|
|
2809
|
+
class Twitter::API < Grape::API
|
|
2810
|
+
rescue_from :internal_grape_exceptions do |e|
|
|
2811
|
+
Sentry.capture_exception(e)
|
|
2812
|
+
error!({ message: 'Something went wrong' }, 500)
|
|
2813
|
+
end
|
|
2814
|
+
end
|
|
2815
|
+
```
|
|
2816
|
+
|
|
2817
|
+
When this handler is registered the framework hands the original exception to you, and you own the response shape and any logging. The framework deliberately does not log internal errors itself — it has no way to know your preferred format or destination.
|
|
2818
|
+
|
|
2819
|
+
A second raise inside the redispatched handler is not redispatched again — it goes straight to the framework's generic 500. This bounds the chain at one redispatch and prevents loops.
|
|
2820
|
+
|
|
2704
2821
|
You can also rescue all exceptions with a code block and handle the Rack response at the lowest level.
|
|
2705
2822
|
|
|
2706
2823
|
```ruby
|
|
@@ -2840,33 +2957,11 @@ Any exception that is not subclass of `StandardError` should be rescued explicit
|
|
|
2840
2957
|
Usually it is not a case for an application logic as such errors point to problems in Ruby runtime.
|
|
2841
2958
|
This is following [standard recommendations for exceptions handling](https://ruby-doc.org/core/Exception.html).
|
|
2842
2959
|
|
|
2843
|
-
### Rails 3.x
|
|
2844
|
-
|
|
2845
|
-
When mounted inside containers, such as Rails 3.x, errors such as "404 Not Found" or
|
|
2846
|
-
"406 Not Acceptable" will likely be handled and rendered by Rails handlers. For instance,
|
|
2847
|
-
accessing a nonexistent route "/api/foo" raises a 404, which inside rails will ultimately
|
|
2848
|
-
be translated to an `ActionController::RoutingError`, which most likely will get rendered
|
|
2849
|
-
to a HTML error page.
|
|
2850
|
-
|
|
2851
|
-
Most APIs will enjoy preventing downstream handlers from handling errors. You may set the
|
|
2852
|
-
`:cascade` option to `false` for the entire API or separately on specific `version` definitions,
|
|
2853
|
-
which will remove the `X-Cascade: true` header from API responses.
|
|
2854
|
-
|
|
2855
|
-
```ruby
|
|
2856
|
-
cascade false
|
|
2857
|
-
```
|
|
2858
|
-
|
|
2859
|
-
```ruby
|
|
2860
|
-
version 'v1', using: :header, vendor: 'twitter', cascade: false
|
|
2861
|
-
```
|
|
2862
|
-
|
|
2863
2960
|
## Logging
|
|
2864
2961
|
|
|
2865
|
-
`Grape::API` provides a `logger` method which by default will return an instance of the `Logger`
|
|
2866
|
-
class from Ruby's standard library.
|
|
2962
|
+
`Grape::API` provides a `logger` method which by default will return an instance of the `Logger` class from Ruby's standard library.
|
|
2867
2963
|
|
|
2868
|
-
To log messages from within an endpoint, you need to define a helper to make the logger
|
|
2869
|
-
available in the endpoint context.
|
|
2964
|
+
To log messages from within an endpoint, you need to define a helper to make the logger available in the endpoint context.
|
|
2870
2965
|
|
|
2871
2966
|
```ruby
|
|
2872
2967
|
class API < Grape::API
|
|
@@ -2915,9 +3010,7 @@ For similar to Rails request logging try the [grape_logging](https://github.com/
|
|
|
2915
3010
|
|
|
2916
3011
|
## API Formats
|
|
2917
3012
|
|
|
2918
|
-
Your API can declare which content-types to support by using `content_type`. If you do not specify any, Grape will support
|
|
2919
|
-
_XML_, _JSON_, _BINARY_, and _TXT_ content-types. The default format is `:txt`; you can change this with `default_format`.
|
|
2920
|
-
Essentially, the two APIs below are equivalent.
|
|
3013
|
+
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.
|
|
2921
3014
|
|
|
2922
3015
|
```ruby
|
|
2923
3016
|
class Twitter::API < Grape::API
|
|
@@ -2936,9 +3029,7 @@ class Twitter::API < Grape::API
|
|
|
2936
3029
|
end
|
|
2937
3030
|
```
|
|
2938
3031
|
|
|
2939
|
-
If you declare any `content_type` whatsoever, the Grape defaults will be overridden. For example, the following API will only
|
|
2940
|
-
support the `:xml` and `:rss` content-types, but not `:txt`, `:json`, or `:binary`. Importantly, this means the `:txt`
|
|
2941
|
-
default format is not supported! So, make sure to set a new `default_format`.
|
|
3032
|
+
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`.
|
|
2942
3033
|
|
|
2943
3034
|
```ruby
|
|
2944
3035
|
class Twitter::API < Grape::API
|
|
@@ -2949,8 +3040,7 @@ class Twitter::API < Grape::API
|
|
|
2949
3040
|
end
|
|
2950
3041
|
```
|
|
2951
3042
|
|
|
2952
|
-
Serialization takes place automatically. For example, you do not have to call `to_json` in each JSON API endpoint
|
|
2953
|
-
implementation. The response format (and thus the automatic serialization) is determined in the following order:
|
|
3043
|
+
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:
|
|
2954
3044
|
* Use the file extension, if specified. If the file is .json, choose the JSON format.
|
|
2955
3045
|
* Use the value of the `format` parameter in the query string, if specified.
|
|
2956
3046
|
* Use the format set by the `format` option, if specified.
|
|
@@ -2973,20 +3063,15 @@ class MultipleFormatAPI < Grape::API
|
|
|
2973
3063
|
end
|
|
2974
3064
|
```
|
|
2975
3065
|
|
|
2976
|
-
* `GET /hello` (with an `Accept: */*` header) does not have an extension or a `format` parameter, so it will respond with
|
|
2977
|
-
JSON (the default format).
|
|
3066
|
+
* `GET /hello` (with an `Accept: */*` header) does not have an extension or a `format` parameter, so it will respond with JSON (the default format).
|
|
2978
3067
|
* `GET /hello.xml` has a recognized extension, so it will respond with XML.
|
|
2979
3068
|
* `GET /hello?format=xml` has a recognized `format` parameter, so it will respond with XML.
|
|
2980
|
-
* `GET /hello.xml?format=json` has a recognized extension (which takes precedence over the `format` parameter), so it will
|
|
2981
|
-
|
|
2982
|
-
* `GET /hello.xls`
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
* `GET /hello.xls` with an `Accept: text/plain` header has an unrecognized extension *and* an unrecognized `Accept` header,
|
|
2987
|
-
so it will respond with JSON (the default format).
|
|
2988
|
-
|
|
2989
|
-
You can override this process explicitly by specifying `env['api.format']` in the API itself.
|
|
3069
|
+
* `GET /hello.xml?format=json` has a recognized extension (which takes precedence over the `format` parameter), so it will respond with XML.
|
|
3070
|
+
* `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).
|
|
3071
|
+
* `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.
|
|
3072
|
+
* `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).
|
|
3073
|
+
|
|
3074
|
+
You can override this process explicitly by calling `api_format` in the API itself.
|
|
2990
3075
|
For example, the following API will let you upload arbitrary files and return their contents as an attachment with the correct MIME type.
|
|
2991
3076
|
|
|
2992
3077
|
```ruby
|
|
@@ -2994,15 +3079,14 @@ class Twitter::API < Grape::API
|
|
|
2994
3079
|
post 'attachment' do
|
|
2995
3080
|
filename = params[:file][:filename]
|
|
2996
3081
|
content_type MIME::Types.type_for(filename)[0].to_s
|
|
2997
|
-
|
|
3082
|
+
api_format :binary # there's no formatter for :binary, data will be returned "as is"
|
|
2998
3083
|
header 'Content-Disposition', "attachment; filename*=UTF-8''#{CGI.escape(filename)}"
|
|
2999
3084
|
params[:file][:tempfile].read
|
|
3000
3085
|
end
|
|
3001
3086
|
end
|
|
3002
3087
|
```
|
|
3003
3088
|
|
|
3004
|
-
You can have your API only respond to a single format with `format`. If you use this, the API will **not** respond to file
|
|
3005
|
-
extensions other than specified in `format`. For example, consider the following API.
|
|
3089
|
+
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.
|
|
3006
3090
|
|
|
3007
3091
|
```ruby
|
|
3008
3092
|
class SingleFormatAPI < Grape::API
|
|
@@ -3017,14 +3101,10 @@ end
|
|
|
3017
3101
|
* `GET /hello` will respond with JSON.
|
|
3018
3102
|
* `GET /hello.json` will respond with JSON.
|
|
3019
3103
|
* `GET /hello.xml`, `GET /hello.foobar`, or *any* other extension will respond with an HTTP 404 error code.
|
|
3020
|
-
* `GET /hello?format=xml` will respond with an HTTP 406 error code, because the XML format specified by the request parameter
|
|
3021
|
-
|
|
3022
|
-
* `GET /hello` with an `Accept: application/xml` header will still respond with JSON, since it could not negotiate a
|
|
3023
|
-
recognized content-type from the headers and JSON is the effective default.
|
|
3104
|
+
* `GET /hello?format=xml` will respond with an HTTP 406 error code, because the XML format specified by the request parameter is not supported.
|
|
3105
|
+
* `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.
|
|
3024
3106
|
|
|
3025
|
-
The formats apply to parsing, too. The following API will only respond to the JSON content-type and will not parse any other
|
|
3026
|
-
input than `application/json`, `application/x-www-form-urlencoded`, `multipart/form-data`, `multipart/related` and
|
|
3027
|
-
`multipart/mixed`. All other requests will fail with an HTTP 406 error code.
|
|
3107
|
+
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.
|
|
3028
3108
|
|
|
3029
3109
|
```ruby
|
|
3030
3110
|
class Twitter::API < Grape::API
|
|
@@ -3080,23 +3160,18 @@ end
|
|
|
3080
3160
|
Built-in formatters are the following.
|
|
3081
3161
|
|
|
3082
3162
|
* `:json`: use object's `to_json` when available, otherwise call `MultiJson.dump`
|
|
3083
|
-
* `:xml`: use object's `to_xml` when available, usually via `MultiXml
|
|
3163
|
+
* `:xml`: use object's `to_xml` when available, usually via `MultiXml`
|
|
3084
3164
|
* `:txt`: use object's `to_txt` when available, otherwise `to_s`
|
|
3085
3165
|
* `:serializable_hash`: use object's `serializable_hash` when available, otherwise fallback to `:json`
|
|
3086
3166
|
* `:binary`: data will be returned "as is"
|
|
3087
3167
|
|
|
3088
|
-
If a body is present in a request to an API, with a Content-Type header value that is of an unsupported type a
|
|
3089
|
-
"415 Unsupported Media Type" error code will be returned by Grape.
|
|
3168
|
+
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.
|
|
3090
3169
|
|
|
3091
|
-
Response statuses that indicate no content as defined by [Rack](https://github.com/rack)
|
|
3092
|
-
[here](https://github.com/rack/rack/blob/master/lib/rack/utils.rb#L567)
|
|
3093
|
-
will bypass serialization and the body entity - though there should be none -
|
|
3094
|
-
will not be modified.
|
|
3170
|
+
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.
|
|
3095
3171
|
|
|
3096
3172
|
### JSONP
|
|
3097
3173
|
|
|
3098
|
-
Grape supports JSONP via [Rack::JSONP](https://github.com/rack/rack-contrib), part of the
|
|
3099
|
-
[rack-contrib](https://github.com/rack/rack-contrib) gem. Add `rack-contrib` to your `Gemfile`.
|
|
3174
|
+
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`.
|
|
3100
3175
|
|
|
3101
3176
|
```ruby
|
|
3102
3177
|
require 'rack/contrib'
|
|
@@ -3112,9 +3187,7 @@ end
|
|
|
3112
3187
|
|
|
3113
3188
|
### CORS
|
|
3114
3189
|
|
|
3115
|
-
Grape supports CORS via [Rack::CORS](https://github.com/cyu/rack-cors), part of the
|
|
3116
|
-
[rack-cors](https://github.com/cyu/rack-cors) gem. Add `rack-cors` to your `Gemfile`,
|
|
3117
|
-
then use the middleware in your config.ru file.
|
|
3190
|
+
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.
|
|
3118
3191
|
|
|
3119
3192
|
```ruby
|
|
3120
3193
|
require 'rack/cors'
|
|
@@ -3132,8 +3205,7 @@ run Twitter::API
|
|
|
3132
3205
|
|
|
3133
3206
|
## Content-type
|
|
3134
3207
|
|
|
3135
|
-
Content-type is set by the formatter. You can override the content-type of the response at runtime
|
|
3136
|
-
by setting the `Content-Type` header.
|
|
3208
|
+
Content-type is set by the formatter. You can override the content-type of the response at runtime by setting the `Content-Type` header.
|
|
3137
3209
|
|
|
3138
3210
|
```ruby
|
|
3139
3211
|
class API < Grape::API
|
|
@@ -3146,16 +3218,12 @@ end
|
|
|
3146
3218
|
|
|
3147
3219
|
## API Data Formats
|
|
3148
3220
|
|
|
3149
|
-
Grape accepts and parses input data sent with the POST and PUT methods as described in the Parameters
|
|
3150
|
-
section above. It also supports custom data formats. You must declare additional content-types via
|
|
3151
|
-
`content_type` and optionally supply a parser via `parser` unless a parser is already available within
|
|
3152
|
-
Grape to enable a custom format. Such a parser can be a function or a class.
|
|
3221
|
+
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.
|
|
3153
3222
|
|
|
3154
3223
|
With a parser, parsed data is available "as-is" in `env['api.request.body']`.
|
|
3155
3224
|
Without a parser, data is available "as-is" and in `env['api.request.input']`.
|
|
3156
3225
|
|
|
3157
|
-
The following example is a trivial parser that will assign any input with the "text/custom" content-type
|
|
3158
|
-
to `:value`. The parameter will be available via `params[:value]` inside the API call.
|
|
3226
|
+
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.
|
|
3159
3227
|
|
|
3160
3228
|
```ruby
|
|
3161
3229
|
module CustomParser
|
|
@@ -3189,9 +3257,7 @@ Grape uses `JSON` and `ActiveSupport::XmlMini` for JSON and XML parsing by defau
|
|
|
3189
3257
|
|
|
3190
3258
|
## RESTful Model Representations
|
|
3191
3259
|
|
|
3192
|
-
Grape supports a range of ways to present your data with some help from a generic `present` method,
|
|
3193
|
-
which accepts two arguments: the object to be presented and the options associated with it. The options
|
|
3194
|
-
hash may include `:with`, which defines the entity to expose.
|
|
3260
|
+
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.
|
|
3195
3261
|
|
|
3196
3262
|
### Grape Entities
|
|
3197
3263
|
|
|
@@ -3270,8 +3336,7 @@ The response will be
|
|
|
3270
3336
|
}
|
|
3271
3337
|
```
|
|
3272
3338
|
|
|
3273
|
-
In addition to separately organizing entities, it may be useful to put them as namespaced
|
|
3274
|
-
classes underneath the model they represent.
|
|
3339
|
+
In addition to separately organizing entities, it may be useful to put them as namespaced classes underneath the model they represent.
|
|
3275
3340
|
|
|
3276
3341
|
```ruby
|
|
3277
3342
|
class Status
|
|
@@ -3285,11 +3350,7 @@ class Status
|
|
|
3285
3350
|
end
|
|
3286
3351
|
```
|
|
3287
3352
|
|
|
3288
|
-
If you organize your entities this way, Grape will automatically detect the `Entity` class and
|
|
3289
|
-
use it to present your models. In this example, if you added `present Status.new` to your endpoint,
|
|
3290
|
-
Grape will automatically detect that there is a `Status::Entity` class and use that as the
|
|
3291
|
-
representative entity. This can still be overridden by using the `:with` option or an explicit
|
|
3292
|
-
`represents` call.
|
|
3353
|
+
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.
|
|
3293
3354
|
|
|
3294
3355
|
You can present `hash` with `Grape::Presenters::Presenter` to keep things consistent.
|
|
3295
3356
|
|
|
@@ -3322,15 +3383,11 @@ You can use [Roar](https://github.com/apotonick/roar) to render HAL or Collectio
|
|
|
3322
3383
|
|
|
3323
3384
|
### Rabl
|
|
3324
3385
|
|
|
3325
|
-
You can use [Rabl](https://github.com/nesquena/rabl) templates with the help of the
|
|
3326
|
-
[grape-rabl](https://github.com/ruby-grape/grape-rabl) gem, which defines a custom Grape Rabl
|
|
3327
|
-
formatter.
|
|
3386
|
+
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.
|
|
3328
3387
|
|
|
3329
3388
|
### Active Model Serializers
|
|
3330
3389
|
|
|
3331
|
-
You can use [Active Model Serializers](https://github.com/rails-api/active_model_serializers) serializers with the help of the
|
|
3332
|
-
[grape-active_model_serializers](https://github.com/jrhe/grape-active_model_serializers) gem, which defines a custom Grape AMS
|
|
3333
|
-
formatter.
|
|
3390
|
+
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.
|
|
3334
3391
|
|
|
3335
3392
|
## Sending Raw or No Data
|
|
3336
3393
|
|
|
@@ -3370,9 +3427,7 @@ class API < Grape::API
|
|
|
3370
3427
|
end
|
|
3371
3428
|
```
|
|
3372
3429
|
|
|
3373
|
-
You can also set the response to a file with `sendfile`. This works with the
|
|
3374
|
-
[Rack::Sendfile](https://www.rubydoc.info/gems/rack/Rack/Sendfile) middleware to optimally send
|
|
3375
|
-
the file through your web server software.
|
|
3430
|
+
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.
|
|
3376
3431
|
|
|
3377
3432
|
```ruby
|
|
3378
3433
|
class API < Grape::API
|
|
@@ -3414,11 +3469,9 @@ end
|
|
|
3414
3469
|
|
|
3415
3470
|
## Authentication
|
|
3416
3471
|
|
|
3417
|
-
### Basic
|
|
3472
|
+
### Basic Auth
|
|
3418
3473
|
|
|
3419
|
-
Grape has built-in Basic
|
|
3420
|
-
is executed in the context of the current `Endpoint`). Authentication
|
|
3421
|
-
applies to the current namespace and any children, but not parents.
|
|
3474
|
+
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.
|
|
3422
3475
|
|
|
3423
3476
|
```ruby
|
|
3424
3477
|
http_basic do |username, password|
|
|
@@ -3427,32 +3480,15 @@ http_basic do |username, password|
|
|
|
3427
3480
|
end
|
|
3428
3481
|
```
|
|
3429
3482
|
|
|
3430
|
-
Digest auth supports clear-text passwords and password hashes.
|
|
3431
|
-
|
|
3432
|
-
```ruby
|
|
3433
|
-
http_digest({ realm: 'Test Api', opaque: 'app secret' }) do |username|
|
|
3434
|
-
# lookup the user's password here
|
|
3435
|
-
end
|
|
3436
|
-
```
|
|
3437
|
-
|
|
3438
|
-
```ruby
|
|
3439
|
-
http_digest(realm: { realm: 'Test Api', opaque: 'app secret', passwords_hashed: true }) do |username|
|
|
3440
|
-
# lookup the user's password hash here
|
|
3441
|
-
end
|
|
3442
|
-
```
|
|
3443
|
-
|
|
3444
3483
|
### Register custom middleware for authentication
|
|
3445
3484
|
|
|
3446
|
-
Grape can use custom Middleware for authentication. How to implement these
|
|
3447
|
-
Middleware have a look at `Rack::Auth::Basic` or similar implementations.
|
|
3448
|
-
|
|
3485
|
+
Grape can use custom Middleware for authentication. How to implement these Middleware have a look at `Rack::Auth::Basic` or similar implementations.
|
|
3449
3486
|
|
|
3450
3487
|
For registering a Middleware you need the following options:
|
|
3451
3488
|
|
|
3452
3489
|
* `label` - the name for your authenticator to use it later
|
|
3453
3490
|
* `MiddlewareClass` - the MiddlewareClass to use for authentication
|
|
3454
|
-
* `option_lookup_proc` - A Proc with one Argument to lookup the options at
|
|
3455
|
-
runtime (return value is an `Array` as Parameter for the Middleware).
|
|
3491
|
+
* `option_lookup_proc` - A Proc with one Argument to lookup the options at runtime (return value is an `Array` as Parameter for the Middleware).
|
|
3456
3492
|
|
|
3457
3493
|
Example:
|
|
3458
3494
|
|
|
@@ -3476,7 +3512,7 @@ You can access the controller params, headers, and helpers through the context w
|
|
|
3476
3512
|
|
|
3477
3513
|
Grape routes can be reflected at runtime. This can notably be useful for generating documentation.
|
|
3478
3514
|
|
|
3479
|
-
Grape exposes arrays of API versions and compiled routes. Each route contains a `
|
|
3515
|
+
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`.
|
|
3480
3516
|
|
|
3481
3517
|
```ruby
|
|
3482
3518
|
class TwitterAPI < Grape::API
|
|
@@ -3499,14 +3535,14 @@ TwitterAPI::routes[0].description # => 'Includes custom settings.'
|
|
|
3499
3535
|
TwitterAPI::routes[0].settings[:custom] # => { key: 'value' }
|
|
3500
3536
|
```
|
|
3501
3537
|
|
|
3502
|
-
Note that `Route#route_xyz` methods have been deprecated since 0.15.0.
|
|
3538
|
+
Note that `Route#route_xyz` methods have been deprecated since 0.15.0 and removed since 2.0.1.
|
|
3503
3539
|
|
|
3504
3540
|
Please use `Route#xyz` instead.
|
|
3505
3541
|
|
|
3506
3542
|
Note that difference of `Route#options` and `Route#settings`.
|
|
3507
3543
|
|
|
3508
|
-
The `options` can be referred from your route, it should be set by
|
|
3509
|
-
The `settings` can also be referred from your route, but it should be set by
|
|
3544
|
+
The `options` can be referred from your route, it should be set by specifying key and value on verb methods such as `get`, `post` and `put`.
|
|
3545
|
+
The `settings` can also be referred from your route, but it should be set by specifying key and value on `route_setting`.
|
|
3510
3546
|
|
|
3511
3547
|
## Current Route and Endpoint
|
|
3512
3548
|
|
|
@@ -3519,15 +3555,12 @@ class MyAPI < Grape::API
|
|
|
3519
3555
|
requires :id, type: Integer, desc: 'Identity.'
|
|
3520
3556
|
end
|
|
3521
3557
|
get 'params/:id' do
|
|
3522
|
-
route.
|
|
3558
|
+
route.params[params[:id]] # yields the parameter description
|
|
3523
3559
|
end
|
|
3524
3560
|
end
|
|
3525
3561
|
```
|
|
3526
3562
|
|
|
3527
|
-
The current endpoint responding to the request is `self` within the API block
|
|
3528
|
-
or `env['api.endpoint']` elsewhere. The endpoint has some interesting properties,
|
|
3529
|
-
such as `source` which gives you access to the original code block of the API
|
|
3530
|
-
implementation. This can be particularly useful for building a logger middleware.
|
|
3563
|
+
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.
|
|
3531
3564
|
|
|
3532
3565
|
```ruby
|
|
3533
3566
|
class ApiLogger < Grape::Middleware::Base
|
|
@@ -3541,10 +3574,8 @@ end
|
|
|
3541
3574
|
|
|
3542
3575
|
## Before, After and Finally
|
|
3543
3576
|
|
|
3544
|
-
Blocks can be executed before or after every API call, using `before`, `after`,
|
|
3545
|
-
`
|
|
3546
|
-
If the API fails the `after` call will not be triggered, if you need code to execute for sure
|
|
3547
|
-
use the `finally`.
|
|
3577
|
+
Blocks can be executed before or after every API call, using `before`, `after`, `before_validation` and `after_validation`.
|
|
3578
|
+
If the API fails the `after` call will not be triggered, if you need code to execute for sure use the `finally`.
|
|
3548
3579
|
|
|
3549
3580
|
Before and after callbacks execute in the following order:
|
|
3550
3581
|
|
|
@@ -3558,13 +3589,9 @@ Before and after callbacks execute in the following order:
|
|
|
3558
3589
|
|
|
3559
3590
|
Steps 4, 5 and 6 only happen if validation succeeds.
|
|
3560
3591
|
|
|
3561
|
-
If a request for a resource is made with an unsupported HTTP method (returning
|
|
3562
|
-
HTTP 405) only `before` callbacks will be executed. The remaining callbacks will
|
|
3563
|
-
be bypassed.
|
|
3592
|
+
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.
|
|
3564
3593
|
|
|
3565
|
-
If a request for a resource is made that triggers the built-in `OPTIONS` handler,
|
|
3566
|
-
only `before` and `after` callbacks will be executed. The remaining callbacks will
|
|
3567
|
-
be bypassed.
|
|
3594
|
+
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.
|
|
3568
3595
|
|
|
3569
3596
|
For example, using a simple `before` block to set a header.
|
|
3570
3597
|
|
|
@@ -3709,11 +3736,7 @@ Instead of altering a response, you can also terminate and rewrite it from any c
|
|
|
3709
3736
|
|
|
3710
3737
|
## Anchoring
|
|
3711
3738
|
|
|
3712
|
-
Grape by default anchors all request paths, which means that the request URL
|
|
3713
|
-
should match from start to end to match, otherwise a `404 Not Found` is
|
|
3714
|
-
returned. However, this is sometimes not what you want, because it is not always
|
|
3715
|
-
known upfront what can be expected from the call. This is because Rack-mount by
|
|
3716
|
-
default anchors requests to match from the start to the end, or not at all.
|
|
3739
|
+
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.
|
|
3717
3740
|
Rails solves this problem by using a `anchor: false` option in your routes.
|
|
3718
3741
|
In Grape this option can be used as well when a method is defined.
|
|
3719
3742
|
|
|
@@ -3729,12 +3752,44 @@ class TwitterAPI < Grape::API
|
|
|
3729
3752
|
end
|
|
3730
3753
|
```
|
|
3731
3754
|
|
|
3732
|
-
This will match all paths starting with '/statuses/'. There is one caveat though:
|
|
3733
|
-
the `
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3755
|
+
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.
|
|
3756
|
+
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.
|
|
3757
|
+
|
|
3758
|
+
## Instance Variables
|
|
3759
|
+
|
|
3760
|
+
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.
|
|
3761
|
+
|
|
3762
|
+
```ruby
|
|
3763
|
+
class TwitterAPI < Grape::API
|
|
3764
|
+
before do
|
|
3765
|
+
@var = 1
|
|
3766
|
+
end
|
|
3767
|
+
|
|
3768
|
+
get '/' do
|
|
3769
|
+
puts @var # => 1
|
|
3770
|
+
raise
|
|
3771
|
+
end
|
|
3772
|
+
|
|
3773
|
+
rescue_from :all do
|
|
3774
|
+
puts @var # => 1
|
|
3775
|
+
end
|
|
3776
|
+
end
|
|
3777
|
+
```
|
|
3778
|
+
|
|
3779
|
+
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.
|
|
3780
|
+
|
|
3781
|
+
```ruby
|
|
3782
|
+
class TwitterAPI < Grape::API
|
|
3783
|
+
get '/first' do
|
|
3784
|
+
@var = 1
|
|
3785
|
+
puts @var # => 1
|
|
3786
|
+
end
|
|
3787
|
+
|
|
3788
|
+
get '/second' do
|
|
3789
|
+
puts @var # => nil
|
|
3790
|
+
end
|
|
3791
|
+
end
|
|
3792
|
+
```
|
|
3738
3793
|
|
|
3739
3794
|
## Using Custom Middleware
|
|
3740
3795
|
|
|
@@ -3943,8 +3998,7 @@ describe Twitter::API do
|
|
|
3943
3998
|
end
|
|
3944
3999
|
```
|
|
3945
4000
|
|
|
3946
|
-
In Rails, HTTP request tests would go into the `spec/requests` group. You may want your API code to go into
|
|
3947
|
-
`app/api` - you can match that layout under `spec` by adding the following in `spec/rails_helper.rb`.
|
|
4001
|
+
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`.
|
|
3948
4002
|
|
|
3949
4003
|
```ruby
|
|
3950
4004
|
RSpec.configure do |config|
|
|
@@ -3978,10 +4032,15 @@ end
|
|
|
3978
4032
|
|
|
3979
4033
|
### Stubbing Helpers
|
|
3980
4034
|
|
|
3981
|
-
Because helpers are mixed in based on the context when an endpoint is defined, it can
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
4035
|
+
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. `Grape::Endpoint.before_each` allows you to define behavior on the endpoint that will run before every request.
|
|
4036
|
+
|
|
4037
|
+
This feature is provided by `Grape::Testing`, a standalone module intended for test environments only. Add it to your test helper:
|
|
4038
|
+
|
|
4039
|
+
```ruby
|
|
4040
|
+
require 'grape/testing'
|
|
4041
|
+
```
|
|
4042
|
+
|
|
4043
|
+
Then use it in your tests:
|
|
3985
4044
|
|
|
3986
4045
|
```ruby
|
|
3987
4046
|
describe 'an endpoint that needs helpers stubbed' do
|
|
@@ -3992,7 +4051,7 @@ describe 'an endpoint that needs helpers stubbed' do
|
|
|
3992
4051
|
end
|
|
3993
4052
|
|
|
3994
4053
|
after do
|
|
3995
|
-
Grape::Endpoint.
|
|
4054
|
+
Grape::Endpoint.reset_before_each
|
|
3996
4055
|
end
|
|
3997
4056
|
|
|
3998
4057
|
it 'stubs the helper' do
|
|
@@ -4009,6 +4068,25 @@ Use [grape-reload](https://github.com/AlexYankee/grape-reload).
|
|
|
4009
4068
|
|
|
4010
4069
|
### Reloading in Rails Applications
|
|
4011
4070
|
|
|
4071
|
+
#### Rails 7+ (Zeitwerk)
|
|
4072
|
+
|
|
4073
|
+
Rails 7+ uses [Zeitwerk](https://github.com/fxn/zeitwerk) as the default autoloader, which automatically handles reloading of code in development mode without any additional configuration.
|
|
4074
|
+
|
|
4075
|
+
If your API files are in `app/api`, Zeitwerk will automatically autoload and reload them. No additional configuration is needed.
|
|
4076
|
+
|
|
4077
|
+
If you encounter issues with reloading, ensure that:
|
|
4078
|
+
|
|
4079
|
+
1. Your API files follow Zeitwerk naming conventions (file names should match class names).
|
|
4080
|
+
2. The `config.enable_reloading` is set to `true` in `config/environments/development.rb` (this is the default).
|
|
4081
|
+
|
|
4082
|
+
For troubleshooting autoloading issues, have a look at the [Rails documentation](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#troubleshooting).
|
|
4083
|
+
|
|
4084
|
+
See the [Rails Autoloading and Reloading Constants guide](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html) for more information.
|
|
4085
|
+
|
|
4086
|
+
#### Rails 6 and Earlier
|
|
4087
|
+
|
|
4088
|
+
For Rails versions before 7, you need to configure reloading manually.
|
|
4089
|
+
|
|
4012
4090
|
Add API paths to `config/application.rb`.
|
|
4013
4091
|
|
|
4014
4092
|
```ruby
|
|
@@ -4027,28 +4105,12 @@ if Rails.env.development?
|
|
|
4027
4105
|
api_reloader = ActiveSupport::FileUpdateChecker.new(api_files) do
|
|
4028
4106
|
Rails.application.reload_routes!
|
|
4029
4107
|
end
|
|
4030
|
-
|
|
4108
|
+
ActiveSupport::Reloader.to_prepare do
|
|
4031
4109
|
api_reloader.execute_if_updated
|
|
4032
4110
|
end
|
|
4033
4111
|
end
|
|
4034
4112
|
```
|
|
4035
4113
|
|
|
4036
|
-
For Rails >= 5.1.4, change this:
|
|
4037
|
-
|
|
4038
|
-
```ruby
|
|
4039
|
-
ActionDispatch::Callbacks.to_prepare do
|
|
4040
|
-
api_reloader.execute_if_updated
|
|
4041
|
-
end
|
|
4042
|
-
```
|
|
4043
|
-
|
|
4044
|
-
to this:
|
|
4045
|
-
|
|
4046
|
-
```ruby
|
|
4047
|
-
ActiveSupport::Reloader.to_prepare do
|
|
4048
|
-
api_reloader.execute_if_updated
|
|
4049
|
-
end
|
|
4050
|
-
```
|
|
4051
|
-
|
|
4052
4114
|
See [StackOverflow #3282655](http://stackoverflow.com/questions/3282655/ruby-on-rails-3-reload-lib-directory-for-each-request/4368838#4368838) for more information.
|
|
4053
4115
|
|
|
4054
4116
|
## Performance Monitoring
|
|
@@ -4057,27 +4119,30 @@ See [StackOverflow #3282655](http://stackoverflow.com/questions/3282655/ruby-on-
|
|
|
4057
4119
|
|
|
4058
4120
|
Grape has built-in support for [ActiveSupport::Notifications](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html) which provides simple hook points to instrument key parts of your application.
|
|
4059
4121
|
|
|
4060
|
-
The following are currently supported:
|
|
4061
4122
|
|
|
4062
|
-
####
|
|
4123
|
+
#### Hook Points
|
|
4124
|
+
|
|
4125
|
+
The following hook points are currently supported:
|
|
4126
|
+
|
|
4127
|
+
##### endpoint_run.grape
|
|
4063
4128
|
|
|
4064
4129
|
The main execution of an endpoint, includes filters and rendering.
|
|
4065
4130
|
|
|
4066
4131
|
* *endpoint* - The endpoint instance
|
|
4067
4132
|
|
|
4068
|
-
|
|
4133
|
+
##### endpoint_render.grape
|
|
4069
4134
|
|
|
4070
4135
|
The execution of the main content block of the endpoint.
|
|
4071
4136
|
|
|
4072
4137
|
* *endpoint* - The endpoint instance
|
|
4073
4138
|
|
|
4074
|
-
|
|
4139
|
+
##### endpoint_run_filters.grape
|
|
4075
4140
|
|
|
4076
4141
|
* *endpoint* - The endpoint instance
|
|
4077
4142
|
* *filters* - The filters being executed
|
|
4078
4143
|
* *type* - The type of filters (before, before_validation, after_validation, after)
|
|
4079
4144
|
|
|
4080
|
-
|
|
4145
|
+
##### endpoint_run_validators.grape
|
|
4081
4146
|
|
|
4082
4147
|
The execution of validators.
|
|
4083
4148
|
|
|
@@ -4085,7 +4150,7 @@ The execution of validators.
|
|
|
4085
4150
|
* *validators* - The validators being executed
|
|
4086
4151
|
* *request* - The request being validated
|
|
4087
4152
|
|
|
4088
|
-
|
|
4153
|
+
##### format_response.grape
|
|
4089
4154
|
|
|
4090
4155
|
Serialization or template rendering.
|
|
4091
4156
|
|
|
@@ -4094,12 +4159,44 @@ Serialization or template rendering.
|
|
|
4094
4159
|
|
|
4095
4160
|
See the [ActiveSupport::Notifications documentation](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html) for information on how to subscribe to these events.
|
|
4096
4161
|
|
|
4162
|
+
#### Subscribe to Hooks
|
|
4163
|
+
|
|
4164
|
+
Once subscribed to the instrumentation, you can intercept the events reported above.
|
|
4165
|
+
|
|
4166
|
+
```ruby
|
|
4167
|
+
ActiveSupport::Notifications.subscribe(/<api_path>/) do |name, start, finish, id, payload|
|
|
4168
|
+
# your code to intercept the notification
|
|
4169
|
+
end
|
|
4170
|
+
```
|
|
4171
|
+
|
|
4172
|
+
The request data, the API’s internal data, and the response can be retrieved from the payload.
|
|
4173
|
+
|
|
4174
|
+
You can use `payload.fetch(:endpoint)` or directly `payload[:endpoint]`.
|
|
4175
|
+
|
|
4176
|
+
The `:endpoint` contains the data currently being processed, and access to attributes such as `body`, `request`, `params`, `headers`, `cookies` and `response_cookies`
|
|
4177
|
+
|
|
4178
|
+
For example, `payload[:endpoint].body` provides the current state of the response.
|
|
4179
|
+
|
|
4180
|
+
```ruby
|
|
4181
|
+
ActiveSupport::Notifications.subscribe(/v1/) do |name, start, finish, id, payload|
|
|
4182
|
+
hook_record = {
|
|
4183
|
+
hook: name
|
|
4184
|
+
status: payload[:env]&.dig("api.endpoint")&.status
|
|
4185
|
+
format: payload[:env]&.dig("api.format")
|
|
4186
|
+
body: payload[:endpoint]&.body
|
|
4187
|
+
duration: (finish - start) * 1000
|
|
4188
|
+
}
|
|
4189
|
+
# your code to save the notification
|
|
4190
|
+
end
|
|
4191
|
+
```
|
|
4192
|
+
|
|
4097
4193
|
### Monitoring Products
|
|
4098
4194
|
|
|
4099
4195
|
Grape integrates with following third-party tools:
|
|
4100
4196
|
|
|
4101
4197
|
* **New Relic** - [built-in support](https://docs.newrelic.com/docs/agents/ruby-agent/frameworks/grape-instrumentation) from v3.10.0 of the official [newrelic_rpm](https://github.com/newrelic/rpm) gem, also [newrelic-grape](https://github.com/xinminlabs/newrelic-grape) gem
|
|
4102
4198
|
* **Librato Metrics** - [grape-librato](https://github.com/seanmoon/grape-librato) gem
|
|
4199
|
+
* **Rails Performance** - [rails_performance](https://github.com/igorkasyanchuk/rails_performance) gem
|
|
4103
4200
|
* **[Skylight](https://www.skylight.io/)** - [skylight](https://github.com/skylightio/skylight-ruby) gem, [documentation](https://docs.skylight.io/grape/)
|
|
4104
4201
|
* **[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)
|
|
4105
4202
|
* **[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)
|
|
@@ -4107,8 +4204,7 @@ Grape integrates with following third-party tools:
|
|
|
4107
4204
|
|
|
4108
4205
|
## Contributing to Grape
|
|
4109
4206
|
|
|
4110
|
-
Grape is work of hundreds of contributors. You're encouraged to submit pull requests, propose
|
|
4111
|
-
features and discuss issues.
|
|
4207
|
+
Grape is work of hundreds of contributors. You're encouraged to submit pull requests, propose features and discuss issues.
|
|
4112
4208
|
|
|
4113
4209
|
See [CONTRIBUTING](CONTRIBUTING.md).
|
|
4114
4210
|
|