grape 0.5.0 → 3.2.1
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 +7 -0
- data/CHANGELOG.md +1223 -164
- data/CONTRIBUTING.md +148 -0
- data/LICENSE +1 -1
- data/README.md +3531 -675
- data/UPGRADING.md +1851 -0
- data/grape.gemspec +26 -32
- data/grape.png +0 -0
- data/lib/grape/api/instance.rb +194 -0
- data/lib/grape/api.rb +110 -485
- data/lib/grape/content_types.rb +28 -0
- data/lib/grape/cookies.rb +32 -25
- data/lib/grape/declared_params_handler.rb +118 -0
- data/lib/grape/dry_types.rb +54 -0
- data/lib/grape/dsl/callbacks.rb +19 -0
- data/lib/grape/dsl/declared.rb +35 -0
- data/lib/grape/dsl/desc.rb +65 -0
- data/lib/grape/dsl/headers.rb +21 -0
- data/lib/grape/dsl/helpers.rb +103 -0
- data/lib/grape/dsl/inside_route.rb +293 -0
- data/lib/grape/dsl/logger.rb +19 -0
- data/lib/grape/dsl/middleware.rb +36 -0
- data/lib/grape/dsl/parameters.rb +239 -0
- data/lib/grape/dsl/request_response.rb +168 -0
- data/lib/grape/dsl/routing.rb +288 -0
- data/lib/grape/dsl/settings.rb +67 -0
- data/lib/grape/dsl/validations.rb +51 -0
- data/lib/grape/endpoint.rb +287 -386
- data/lib/grape/env.rb +20 -0
- data/lib/grape/error_formatter/base.rb +56 -19
- data/lib/grape/error_formatter/json.rb +18 -7
- data/lib/grape/error_formatter/serializable_hash.rb +7 -0
- data/lib/grape/error_formatter/txt.rb +14 -11
- data/lib/grape/error_formatter/xml.rb +5 -11
- data/lib/grape/error_formatter.rb +15 -0
- data/lib/grape/exceptions/base.rb +31 -47
- data/lib/grape/exceptions/incompatible_option_values.rb +11 -0
- data/lib/grape/exceptions/invalid_accept_header.rb +11 -0
- data/lib/grape/exceptions/invalid_formatter.rb +3 -5
- data/lib/grape/exceptions/invalid_message_body.rb +11 -0
- data/lib/grape/exceptions/invalid_response.rb +11 -0
- data/lib/grape/exceptions/invalid_version_header.rb +11 -0
- data/lib/grape/exceptions/invalid_versioner_option.rb +3 -6
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +3 -7
- data/lib/grape/exceptions/method_not_allowed.rb +11 -0
- data/lib/grape/exceptions/missing_group_type.rb +11 -0
- data/lib/grape/exceptions/missing_mime_type.rb +3 -6
- data/lib/grape/exceptions/missing_vendor_option.rb +3 -5
- 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 +11 -0
- data/lib/grape/exceptions/unknown_params_builder.rb +11 -0
- data/lib/grape/exceptions/unknown_validator.rb +3 -4
- data/lib/grape/exceptions/unsupported_group_type.rb +11 -0
- data/lib/grape/exceptions/validation.rb +20 -8
- data/lib/grape/exceptions/validation_array_errors.rb +14 -0
- data/lib/grape/exceptions/validation_errors.rb +59 -0
- data/lib/grape/formatter/base.rb +9 -26
- data/lib/grape/formatter/json.rb +6 -7
- data/lib/grape/formatter/serializable_hash.rb +24 -20
- data/lib/grape/formatter/txt.rb +5 -7
- data/lib/grape/formatter/xml.rb +6 -7
- data/lib/grape/formatter.rb +17 -0
- data/lib/grape/json.rb +10 -0
- data/lib/grape/locale/en.yml +47 -20
- data/lib/grape/middleware/auth/base.rb +25 -0
- data/lib/grape/middleware/auth/dsl.rb +38 -0
- data/lib/grape/middleware/auth/strategies.rb +25 -0
- data/lib/grape/middleware/auth/strategy_info.rb +15 -0
- data/lib/grape/middleware/base.rb +73 -19
- data/lib/grape/middleware/error.rb +104 -44
- data/lib/grape/middleware/filter.rb +5 -3
- data/lib/grape/middleware/formatter.rb +115 -105
- data/lib/grape/middleware/globals.rb +14 -0
- data/lib/grape/middleware/stack.rb +110 -0
- data/lib/grape/middleware/versioner/accept_version_header.rb +11 -40
- data/lib/grape/middleware/versioner/base.rb +76 -0
- data/lib/grape/middleware/versioner/header.rb +61 -78
- data/lib/grape/middleware/versioner/param.rb +9 -21
- data/lib/grape/middleware/versioner/path.rb +16 -31
- data/lib/grape/middleware/versioner.rb +10 -15
- data/lib/grape/namespace.rb +39 -10
- 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 +9 -24
- data/lib/grape/parser/json.rb +8 -7
- data/lib/grape/parser/xml.rb +8 -7
- data/lib/grape/parser.rb +15 -0
- data/lib/grape/path.rb +76 -0
- data/lib/grape/presenters/presenter.rb +11 -0
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +182 -0
- data/lib/grape/router/base_route.rb +48 -0
- data/lib/grape/router/greedy_route.rb +26 -0
- data/lib/grape/router/pattern.rb +67 -0
- data/lib/grape/router/route.rb +58 -0
- data/lib/grape/router.rb +167 -0
- data/lib/grape/serve_stream/file_body.rb +36 -0
- data/lib/grape/serve_stream/sendfile_response.rb +21 -0
- data/lib/grape/serve_stream/stream_response.rb +23 -0
- data/lib/grape/util/api_description.rb +58 -0
- data/lib/grape/util/base_inheritable.rb +46 -0
- data/lib/grape/util/cache.rb +18 -0
- data/lib/grape/util/deep_freeze.rb +35 -0
- data/lib/grape/util/endpoint_configuration.rb +8 -0
- data/lib/grape/util/header.rb +13 -0
- data/lib/grape/util/inheritable_setting.rb +107 -0
- data/lib/grape/util/inheritable_values.rb +29 -0
- data/lib/grape/util/lazy/block.rb +29 -0
- data/lib/grape/util/lazy/value.rb +38 -0
- data/lib/grape/util/lazy/value_array.rb +21 -0
- data/lib/grape/util/lazy/value_enumerable.rb +34 -0
- data/lib/grape/util/lazy/value_hash.rb +21 -0
- data/lib/grape/util/media_type.rb +70 -0
- data/lib/grape/util/registry.rb +27 -0
- data/lib/grape/util/reverse_stackable_values.rb +15 -0
- data/lib/grape/util/stackable_values.rb +36 -0
- data/lib/grape/util/translation.rb +42 -0
- data/lib/grape/validations/attributes_iterator.rb +77 -0
- data/lib/grape/validations/contract_scope.rb +28 -0
- data/lib/grape/validations/multiple_attributes_iterator.rb +13 -0
- data/lib/grape/validations/param_scope_tracker.rb +57 -0
- data/lib/grape/validations/params_documentation.rb +50 -0
- data/lib/grape/validations/params_scope.rb +529 -0
- data/lib/grape/validations/single_attribute_iterator.rb +26 -0
- data/lib/grape/validations/types/array_coercer.rb +60 -0
- data/lib/grape/validations/types/custom_type_coercer.rb +164 -0
- data/lib/grape/validations/types/custom_type_collection_coercer.rb +56 -0
- data/lib/grape/validations/types/dry_type_coercer.rb +59 -0
- data/lib/grape/validations/types/file.rb +31 -0
- data/lib/grape/validations/types/invalid_value.rb +17 -0
- data/lib/grape/validations/types/json.rb +71 -0
- data/lib/grape/validations/types/multiple_type_coercer.rb +57 -0
- data/lib/grape/validations/types/primitive_coercer.rb +46 -0
- data/lib/grape/validations/types/set_coercer.rb +35 -0
- data/lib/grape/validations/types/variant_collection_coercer.rb +51 -0
- data/lib/grape/validations/types.rb +198 -0
- data/lib/grape/validations/validators/all_or_none_of_validator.rb +19 -0
- data/lib/grape/validations/validators/allow_blank_validator.rb +25 -0
- data/lib/grape/validations/validators/as_validator.rb +14 -0
- data/lib/grape/validations/validators/at_least_one_of_validator.rb +18 -0
- data/lib/grape/validations/validators/base.rb +158 -0
- data/lib/grape/validations/validators/coerce_validator.rb +55 -0
- data/lib/grape/validations/validators/contract_scope_validator.rb +45 -0
- data/lib/grape/validations/validators/default_validator.rb +31 -0
- data/lib/grape/validations/validators/exactly_one_of_validator.rb +24 -0
- data/lib/grape/validations/validators/except_values_validator.rb +33 -0
- data/lib/grape/validations/validators/length_validator.rb +48 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +34 -0
- data/lib/grape/validations/validators/mutually_exclusive_validator.rb +18 -0
- data/lib/grape/validations/validators/presence_validator.rb +17 -0
- data/lib/grape/validations/validators/regexp_validator.rb +24 -0
- data/lib/grape/validations/validators/same_as_validator.rb +20 -0
- data/lib/grape/validations/validators/values_validator.rb +68 -0
- data/lib/grape/validations.rb +10 -226
- data/lib/grape/version.rb +4 -1
- data/lib/grape/xml.rb +10 -0
- data/lib/grape.rb +79 -77
- metadata +160 -306
- data/.gitignore +0 -38
- data/.rspec +0 -2
- data/.travis.yml +0 -9
- data/.yardopts +0 -2
- data/Gemfile +0 -19
- data/Guardfile +0 -15
- data/Rakefile +0 -64
- data/lib/grape/exceptions/missing_option.rb +0 -13
- data/lib/grape/exceptions/unknown_options.rb +0 -14
- data/lib/grape/http/request.rb +0 -28
- data/lib/grape/middleware/auth/basic.rb +0 -30
- data/lib/grape/middleware/auth/digest.rb +0 -30
- data/lib/grape/middleware/auth/oauth2.rb +0 -72
- data/lib/grape/route.rb +0 -23
- data/lib/grape/util/content_types.rb +0 -17
- data/lib/grape/util/deep_merge.rb +0 -23
- data/lib/grape/util/hash_stack.rb +0 -120
- data/lib/grape/validations/coerce.rb +0 -62
- data/lib/grape/validations/default.rb +0 -24
- data/lib/grape/validations/presence.rb +0 -12
- data/lib/grape/validations/regexp.rb +0 -14
- data/spec/grape/api_spec.rb +0 -2129
- data/spec/grape/endpoint_spec.rb +0 -605
- data/spec/grape/entity_spec.rb +0 -281
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -18
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -18
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -24
- data/spec/grape/exceptions/missing_option_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -18
- data/spec/grape/middleware/auth/basic_spec.rb +0 -31
- data/spec/grape/middleware/auth/digest_spec.rb +0 -47
- data/spec/grape/middleware/auth/oauth2_spec.rb +0 -90
- data/spec/grape/middleware/base_spec.rb +0 -63
- data/spec/grape/middleware/error_spec.rb +0 -49
- data/spec/grape/middleware/exception_spec.rb +0 -164
- data/spec/grape/middleware/formatter_spec.rb +0 -244
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -121
- data/spec/grape/middleware/versioner/header_spec.rb +0 -277
- data/spec/grape/middleware/versioner/param_spec.rb +0 -58
- data/spec/grape/middleware/versioner/path_spec.rb +0 -44
- data/spec/grape/middleware/versioner_spec.rb +0 -22
- data/spec/grape/util/hash_stack_spec.rb +0 -133
- data/spec/grape/validations/coerce_spec.rb +0 -143
- data/spec/grape/validations/default_spec.rb +0 -67
- data/spec/grape/validations/presence_spec.rb +0 -144
- data/spec/grape/validations/regexp_spec.rb +0 -33
- data/spec/grape/validations/zh-CN.yml +0 -9
- data/spec/grape/validations_spec.rb +0 -217
- data/spec/shared/versioning_examples.rb +0 -121
- data/spec/spec_helper.rb +0 -25
- data/spec/support/basic_auth_encode_helpers.rb +0 -4
- data/spec/support/versioned_helpers.rb +0 -48
data/lib/grape/cookies.rb
CHANGED
|
@@ -1,43 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
class Cookies
|
|
5
|
+
extend Forwardable
|
|
3
6
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
DELETED_COOKIES_ATTRS = {
|
|
8
|
+
max_age: '0',
|
|
9
|
+
value: '',
|
|
10
|
+
expires: Time.at(0)
|
|
11
|
+
}.freeze
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
request.cookies.each do |name, value|
|
|
11
|
-
@cookies[name.to_s] = value
|
|
12
|
-
end
|
|
13
|
-
end
|
|
13
|
+
def_delegators :cookies, :[], :each
|
|
14
14
|
|
|
15
|
-
def
|
|
16
|
-
@cookies
|
|
17
|
-
|
|
18
|
-
}.each { |name, value|
|
|
19
|
-
cookie_value = value.is_a?(Hash) ? value : { :value => value }
|
|
20
|
-
Rack::Utils.set_cookie_header! header, name, cookie_value
|
|
21
|
-
}
|
|
15
|
+
def initialize(rack_cookies)
|
|
16
|
+
@cookies = rack_cookies
|
|
17
|
+
@send_cookies = nil
|
|
22
18
|
end
|
|
23
19
|
|
|
24
|
-
def
|
|
25
|
-
@
|
|
20
|
+
def response_cookies
|
|
21
|
+
return unless @send_cookies
|
|
22
|
+
|
|
23
|
+
send_cookies.each do |name|
|
|
24
|
+
yield name, cookies[name]
|
|
25
|
+
end
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def []=(name, value)
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
cookies[name] = value
|
|
30
|
+
send_cookies << name
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
# see https://github.com/rack/rack/blob/main/lib/rack/utils.rb#L338-L340
|
|
34
|
+
def delete(name, **opts)
|
|
35
|
+
self.[]=(name, opts.merge(DELETED_COOKIES_ATTRS))
|
|
35
36
|
end
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def cookies
|
|
41
|
+
return @cookies unless @cookies.is_a?(Proc)
|
|
42
|
+
|
|
43
|
+
@cookies = @cookies.call.with_indifferent_access
|
|
40
44
|
end
|
|
41
45
|
|
|
46
|
+
def send_cookies
|
|
47
|
+
@send_cookies ||= Set.new
|
|
48
|
+
end
|
|
42
49
|
end
|
|
43
50
|
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
class DeclaredParamsHandler
|
|
5
|
+
def initialize(include_missing: true, evaluate_given: false, stringify: false, contract_key_map: nil)
|
|
6
|
+
@include_missing = include_missing
|
|
7
|
+
@evaluate_given = evaluate_given
|
|
8
|
+
@stringify = stringify
|
|
9
|
+
@contract_key_map = contract_key_map
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def call(passed_params, declared_params, route_params, renamed_params)
|
|
13
|
+
recursive_declared(
|
|
14
|
+
passed_params,
|
|
15
|
+
declared_params:,
|
|
16
|
+
route_params:,
|
|
17
|
+
renamed_params:
|
|
18
|
+
)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def recursive_declared(passed_params, declared_params:, route_params:, renamed_params:, params_nested_path: [])
|
|
24
|
+
res = if passed_params.is_a?(Array)
|
|
25
|
+
passed_params.map do |passed_param|
|
|
26
|
+
recursive_declared(passed_param, declared_params:, params_nested_path:, renamed_params:, route_params:)
|
|
27
|
+
end
|
|
28
|
+
else
|
|
29
|
+
declared_hash(passed_params, declared_params:, params_nested_path:, renamed_params:, route_params:)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
@contract_key_map&.each { |key_map| key_map.write(passed_params, res) }
|
|
33
|
+
|
|
34
|
+
res
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def declared_hash(passed_params, declared_params:, params_nested_path:, renamed_params:, route_params:)
|
|
38
|
+
declared_params.each_with_object(passed_params.class.new) do |declared_param_attr, memo|
|
|
39
|
+
next if @evaluate_given && !declared_param_attr.scope.attr_meets_dependency?(passed_params)
|
|
40
|
+
|
|
41
|
+
declared_hash_attr(
|
|
42
|
+
passed_params,
|
|
43
|
+
declared_param: declared_param_attr.key,
|
|
44
|
+
params_nested_path:,
|
|
45
|
+
memo:,
|
|
46
|
+
renamed_params:,
|
|
47
|
+
route_params:
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def declared_hash_attr(passed_params, declared_param:, params_nested_path:, memo:, renamed_params:, route_params:)
|
|
53
|
+
if declared_param.is_a?(Hash)
|
|
54
|
+
declared_param.each_pair do |declared_parent_param, declared_children_params|
|
|
55
|
+
next unless @include_missing || passed_params.key?(declared_parent_param)
|
|
56
|
+
|
|
57
|
+
memo_key = build_memo_key(params_nested_path, declared_parent_param, renamed_params)
|
|
58
|
+
passed_children_params = passed_params[declared_parent_param] || passed_params.class.new
|
|
59
|
+
|
|
60
|
+
params_nested_path_dup = params_nested_path.dup
|
|
61
|
+
params_nested_path_dup << declared_parent_param.to_s
|
|
62
|
+
|
|
63
|
+
memo[memo_key] = handle_passed_param(params_nested_path_dup, route_params:, has_passed_children: passed_children_params.any?) do
|
|
64
|
+
recursive_declared(
|
|
65
|
+
passed_children_params,
|
|
66
|
+
declared_params: declared_children_params,
|
|
67
|
+
params_nested_path: params_nested_path_dup,
|
|
68
|
+
renamed_params:,
|
|
69
|
+
route_params:
|
|
70
|
+
)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
else
|
|
74
|
+
# If it is not a Hash then it does not have children.
|
|
75
|
+
# Find its value or set it to nil.
|
|
76
|
+
return unless @include_missing || (passed_params.respond_to?(:key?) && passed_params.key?(declared_param))
|
|
77
|
+
|
|
78
|
+
memo_key = build_memo_key(params_nested_path, declared_param, renamed_params)
|
|
79
|
+
passed_param = passed_params[declared_param]
|
|
80
|
+
|
|
81
|
+
params_nested_path_dup = params_nested_path.dup
|
|
82
|
+
params_nested_path_dup << declared_param.to_s
|
|
83
|
+
|
|
84
|
+
memo[memo_key] = passed_param || handle_passed_param(params_nested_path_dup, route_params:) do
|
|
85
|
+
passed_param
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def build_memo_key(params_nested_path, declared_param, renamed_params)
|
|
91
|
+
rename_path = params_nested_path + [declared_param.to_s]
|
|
92
|
+
renamed_param_name = renamed_params[rename_path]
|
|
93
|
+
|
|
94
|
+
param = renamed_param_name || declared_param
|
|
95
|
+
@stringify ? param.to_s : param.to_sym
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def handle_passed_param(params_nested_path, route_params:, has_passed_children: false, &_block)
|
|
99
|
+
return yield if has_passed_children
|
|
100
|
+
|
|
101
|
+
key = params_nested_path[0]
|
|
102
|
+
key += "[#{params_nested_path[1..].join('][')}]" if params_nested_path.size > 1
|
|
103
|
+
|
|
104
|
+
type = route_params.dig(key, :type)
|
|
105
|
+
has_children = route_params.keys.any? { |k| k != key && k.start_with?("#{key}[") }
|
|
106
|
+
|
|
107
|
+
if type == 'Hash' && !has_children
|
|
108
|
+
{}
|
|
109
|
+
elsif type == 'Array' || (type&.start_with?('[') && !type.include?(','))
|
|
110
|
+
[]
|
|
111
|
+
elsif type == 'Set' || type&.start_with?('#<Set', 'Set')
|
|
112
|
+
Set.new
|
|
113
|
+
else
|
|
114
|
+
yield
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module DryTypes
|
|
5
|
+
# https://dry-rb.org/gems/dry-types/main/getting-started/
|
|
6
|
+
# limit to what Grape is using
|
|
7
|
+
include Dry.Types(:params, :coercible, :strict)
|
|
8
|
+
|
|
9
|
+
class StrictCache < Grape::Util::Cache
|
|
10
|
+
MAPPING = {
|
|
11
|
+
Grape::API::Boolean => DryTypes::Strict::Bool,
|
|
12
|
+
BigDecimal => DryTypes::Strict::Decimal,
|
|
13
|
+
Numeric => DryTypes::Strict::Integer | DryTypes::Strict::Float | DryTypes::Strict::Decimal,
|
|
14
|
+
TrueClass => DryTypes::Strict::Bool.constrained(eql: true),
|
|
15
|
+
FalseClass => DryTypes::Strict::Bool.constrained(eql: false)
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
def initialize
|
|
19
|
+
super
|
|
20
|
+
@cache = Hash.new do |h, strict_type|
|
|
21
|
+
h[strict_type] = MAPPING.fetch(strict_type) do
|
|
22
|
+
DryTypes.wrapped_dry_types_const_get(DryTypes::Strict, strict_type)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class ParamsCache < Grape::Util::Cache
|
|
29
|
+
MAPPING = {
|
|
30
|
+
Grape::API::Boolean => DryTypes::Params::Bool,
|
|
31
|
+
BigDecimal => DryTypes::Params::Decimal,
|
|
32
|
+
Numeric => DryTypes::Params::Integer | DryTypes::Params::Float | DryTypes::Params::Decimal,
|
|
33
|
+
TrueClass => DryTypes::Params::Bool.constrained(eql: true),
|
|
34
|
+
FalseClass => DryTypes::Params::Bool.constrained(eql: false),
|
|
35
|
+
String => DryTypes::Coercible::String
|
|
36
|
+
}.freeze
|
|
37
|
+
|
|
38
|
+
def initialize
|
|
39
|
+
super
|
|
40
|
+
@cache = Hash.new do |h, params_type|
|
|
41
|
+
h[params_type] = MAPPING.fetch(params_type) do
|
|
42
|
+
DryTypes.wrapped_dry_types_const_get(DryTypes::Params, params_type)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.wrapped_dry_types_const_get(dry_type, type)
|
|
49
|
+
dry_type.const_get(type.name, false)
|
|
50
|
+
rescue NameError
|
|
51
|
+
raise ArgumentError, "type #{type} should support coercion via `[]`" unless type.respond_to?(:[])
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module DSL
|
|
5
|
+
module Callbacks
|
|
6
|
+
# before: execute the given block before validation, coercion, or any endpoint
|
|
7
|
+
# before_validation: execute the given block after `before`, but prior to validation or coercion
|
|
8
|
+
# after_validation: execute the given block after validations and coercions, but before any endpoint code
|
|
9
|
+
# after: execute the given block after the endpoint code has run except in unsuccessful
|
|
10
|
+
# finally: execute the given block after the endpoint code even if unsuccessful
|
|
11
|
+
|
|
12
|
+
%w[before before_validation after_validation after finally].each do |callback_method|
|
|
13
|
+
define_method callback_method.to_sym do |&block|
|
|
14
|
+
inheritable_setting.namespace_stackable[callback_method.pluralize.to_sym] = block
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module DSL
|
|
5
|
+
module Declared
|
|
6
|
+
# Denotes a situation where a DSL method has been invoked in a
|
|
7
|
+
# filter which it should not yet be available in
|
|
8
|
+
class MethodNotYetAvailable < StandardError
|
|
9
|
+
def initialize(msg = '#declared is not available prior to parameter validation')
|
|
10
|
+
super
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# A filtering method that will return a hash
|
|
15
|
+
# consisting only of keys that have been declared by a
|
|
16
|
+
# `params` statement against the current/target endpoint or parent
|
|
17
|
+
# namespaces.
|
|
18
|
+
# @param params [Hash] The initial hash to filter. Usually this will just be `params`
|
|
19
|
+
# @param options [Hash] Can pass `:include_missing`, `:stringify` and `:include_parent_namespaces`
|
|
20
|
+
# options. `:include_parent_namespaces` defaults to true, hence must be set to false if
|
|
21
|
+
# you want only to return params declared against the current/target endpoint.
|
|
22
|
+
def declared(passed_params, include_parent_namespaces: true, include_missing: true, evaluate_given: false, stringify: false)
|
|
23
|
+
raise MethodNotYetAvailable unless before_filter_passed
|
|
24
|
+
|
|
25
|
+
contract_key_map = inheritable_setting.namespace_stackable[:contract_key_map]
|
|
26
|
+
handler = DeclaredParamsHandler.new(include_missing:, evaluate_given:, stringify:, contract_key_map:)
|
|
27
|
+
declared_params = include_parent_namespaces ? inheritable_setting.route[:declared_params] : (inheritable_setting.namespace_stackable[:declared_params].last || [])
|
|
28
|
+
renamed_params = inheritable_setting.route[:renamed_params] || {}
|
|
29
|
+
route_params = options.dig(:route_options, :params) || {} # options = endpoint's option
|
|
30
|
+
|
|
31
|
+
handler.call(passed_params, declared_params, route_params, renamed_params)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module DSL
|
|
5
|
+
module Desc
|
|
6
|
+
extend Grape::DSL::Settings
|
|
7
|
+
|
|
8
|
+
# Add a description to the next namespace or function.
|
|
9
|
+
# @param description [String] descriptive string for this endpoint
|
|
10
|
+
# or namespace
|
|
11
|
+
# @param options [Hash] other properties you can set to describe the
|
|
12
|
+
# endpoint or namespace. Optional.
|
|
13
|
+
# @option options :detail [String] additional detail about this endpoint
|
|
14
|
+
# @option options :summary [String] summary for this endpoint
|
|
15
|
+
# @option options :params [Hash] param types and info. normally, you set
|
|
16
|
+
# these via the `params` dsl method.
|
|
17
|
+
# @option options :entity [Grape::Entity] the entity returned upon a
|
|
18
|
+
# successful call to this action
|
|
19
|
+
# @option options :http_codes [Array[Array]] possible HTTP codes this
|
|
20
|
+
# endpoint may return, with their meanings, in a 2d array
|
|
21
|
+
# @option options :named [String] a specific name to help find this route
|
|
22
|
+
# @option options :body_name [String] override the autogenerated body name param
|
|
23
|
+
# @option options :headers [Hash] HTTP headers this method can accept
|
|
24
|
+
# @option options :hidden [Boolean] hide the endpoint or not
|
|
25
|
+
# @option options :deprecated [Boolean] deprecate the endpoint or not
|
|
26
|
+
# @option options :is_array [Boolean] response entity is array or not
|
|
27
|
+
# @option options :nickname [String] nickname of the endpoint
|
|
28
|
+
# @option options :produces [Array[String]] a list of MIME types the endpoint produce
|
|
29
|
+
# @option options :consumes [Array[String]] a list of MIME types the endpoint consume
|
|
30
|
+
# @option options :security [Array[Hash]] a list of security schemes
|
|
31
|
+
# @option options :tags [Array[String]] a list of tags
|
|
32
|
+
# @yield a block yielding an instance context with methods mapping to
|
|
33
|
+
# each of the above, except that :entity is also aliased as #success
|
|
34
|
+
# and :http_codes is aliased as #failure.
|
|
35
|
+
#
|
|
36
|
+
# @example
|
|
37
|
+
#
|
|
38
|
+
# desc 'create a user'
|
|
39
|
+
# post '/users' do
|
|
40
|
+
# # ...
|
|
41
|
+
# end
|
|
42
|
+
#
|
|
43
|
+
# desc 'find a user' do
|
|
44
|
+
# detail 'locates the user from the given user ID'
|
|
45
|
+
# failure [ [404, 'Couldn\'t find the given user' ] ]
|
|
46
|
+
# success User::Entity
|
|
47
|
+
# end
|
|
48
|
+
# get '/user/:id' do
|
|
49
|
+
# # ...
|
|
50
|
+
# end
|
|
51
|
+
#
|
|
52
|
+
def desc(description, options = {}, &config_block)
|
|
53
|
+
settings =
|
|
54
|
+
if config_block
|
|
55
|
+
endpoint_config = defined?(configuration) ? configuration : nil
|
|
56
|
+
Grape::Util::ApiDescription.new(description, endpoint_config, &config_block).settings
|
|
57
|
+
else
|
|
58
|
+
options.merge(description:)
|
|
59
|
+
end
|
|
60
|
+
inheritable_setting.namespace[:description] = settings
|
|
61
|
+
inheritable_setting.route[:description] = settings
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module DSL
|
|
5
|
+
module Headers
|
|
6
|
+
# This method has four responsibilities:
|
|
7
|
+
# 1. Set a specifc header value by key
|
|
8
|
+
# 2. Retrieve a specifc header value by key
|
|
9
|
+
# 3. Retrieve all headers that have been set
|
|
10
|
+
# 4. Delete a specifc header key-value pair
|
|
11
|
+
def header(key = nil, val = nil)
|
|
12
|
+
if key
|
|
13
|
+
val ? header[key] = val : header.delete(key)
|
|
14
|
+
else
|
|
15
|
+
@header ||= Grape::Util::Header.new
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
alias headers header
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module DSL
|
|
5
|
+
module Helpers
|
|
6
|
+
# Add helper methods that will be accessible from any
|
|
7
|
+
# endpoint within this namespace (and child namespaces).
|
|
8
|
+
#
|
|
9
|
+
# When called without a block, all known helpers within this scope
|
|
10
|
+
# are included.
|
|
11
|
+
#
|
|
12
|
+
# @param [Array] new_modules optional array of modules to include
|
|
13
|
+
# @param [Block] block optional block of methods to include
|
|
14
|
+
#
|
|
15
|
+
# @example Define some helpers.
|
|
16
|
+
#
|
|
17
|
+
# class ExampleAPI < Grape::API
|
|
18
|
+
# helpers do
|
|
19
|
+
# def current_user
|
|
20
|
+
# User.find_by_id(params[:token])
|
|
21
|
+
# end
|
|
22
|
+
# end
|
|
23
|
+
# end
|
|
24
|
+
#
|
|
25
|
+
# @example Include many modules
|
|
26
|
+
#
|
|
27
|
+
# class ExampleAPI < Grape::API
|
|
28
|
+
# helpers Authentication, Mailer, OtherModule
|
|
29
|
+
# end
|
|
30
|
+
#
|
|
31
|
+
def helpers(*new_modules, &block)
|
|
32
|
+
include_new_modules(new_modules)
|
|
33
|
+
include_block(block)
|
|
34
|
+
include_all_in_scope if !block && new_modules.empty?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def include_new_modules(modules)
|
|
40
|
+
return if modules.empty?
|
|
41
|
+
|
|
42
|
+
modules.each { |mod| make_inclusion(mod) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def include_block(block)
|
|
46
|
+
return unless block
|
|
47
|
+
|
|
48
|
+
Module.new.tap do |mod|
|
|
49
|
+
make_inclusion(mod) { mod.class_eval(&block) }
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def make_inclusion(mod, &)
|
|
54
|
+
define_boolean_in_mod(mod)
|
|
55
|
+
inject_api_helpers_to_mod(mod, &)
|
|
56
|
+
inheritable_setting.namespace_stackable[:helpers] = mod
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def include_all_in_scope
|
|
60
|
+
Module.new.tap do |mod|
|
|
61
|
+
namespace_stackable(:helpers).each { |mod_to_include| mod.include mod_to_include }
|
|
62
|
+
change!
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def define_boolean_in_mod(mod)
|
|
67
|
+
return if defined? mod::Boolean
|
|
68
|
+
|
|
69
|
+
mod.const_set(:Boolean, Grape::API::Boolean)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def inject_api_helpers_to_mod(mod, &block)
|
|
73
|
+
mod.extend(BaseHelper) unless mod.is_a?(BaseHelper)
|
|
74
|
+
yield if block
|
|
75
|
+
mod.api_changed(self)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# This module extends user defined helpers
|
|
79
|
+
# to provide some API-specific functionality.
|
|
80
|
+
module BaseHelper
|
|
81
|
+
attr_accessor :api
|
|
82
|
+
|
|
83
|
+
def params(name, &block)
|
|
84
|
+
@named_params ||= {}
|
|
85
|
+
@named_params[name] = block
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def api_changed(new_api)
|
|
89
|
+
@api = new_api
|
|
90
|
+
process_named_params
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
protected
|
|
94
|
+
|
|
95
|
+
def process_named_params
|
|
96
|
+
return if @named_params.blank?
|
|
97
|
+
|
|
98
|
+
api.inheritable_setting.namespace_stackable[:named_params] = @named_params
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|