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
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Middleware
|
|
5
|
+
module Versioner
|
|
6
|
+
class Base < Grape::Middleware::Base
|
|
7
|
+
DEFAULT_OPTIONS = {
|
|
8
|
+
pattern: /.*/i,
|
|
9
|
+
prefix: nil,
|
|
10
|
+
mount_path: nil,
|
|
11
|
+
version_options: {
|
|
12
|
+
strict: false,
|
|
13
|
+
cascade: true,
|
|
14
|
+
parameter: 'apiver',
|
|
15
|
+
vendor: nil
|
|
16
|
+
}.freeze
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
CASCADE_PASS_HEADER = { 'X-Cascade' => 'pass' }.freeze
|
|
20
|
+
|
|
21
|
+
DEFAULT_OPTIONS.each_key do |key|
|
|
22
|
+
define_method key do
|
|
23
|
+
options[key]
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
DEFAULT_OPTIONS[:version_options].each_key do |key|
|
|
28
|
+
define_method key do
|
|
29
|
+
options[:version_options][key]
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.inherited(klass)
|
|
34
|
+
super
|
|
35
|
+
Versioner.register(klass)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
attr_reader :error_headers, :versions
|
|
39
|
+
|
|
40
|
+
def initialize(app, **options)
|
|
41
|
+
super
|
|
42
|
+
@error_headers = cascade ? CASCADE_PASS_HEADER : {}
|
|
43
|
+
@versions = options[:versions]&.map(&:to_s) # making sure versions are strings to ease potential match
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def potential_version_match?(potential_version)
|
|
47
|
+
versions.blank? || versions.include?(potential_version)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def version_not_found!
|
|
51
|
+
throw :error, status: 404, message: '404 API Version Not Found', headers: CASCADE_PASS_HEADER
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def available_media_types
|
|
57
|
+
@available_media_types ||= begin
|
|
58
|
+
media_types = []
|
|
59
|
+
base_media_type = "application/vnd.#{vendor}"
|
|
60
|
+
content_types.each_key do |extension|
|
|
61
|
+
versions&.reverse_each do |version|
|
|
62
|
+
media_types << "#{base_media_type}-#{version}+#{extension}"
|
|
63
|
+
media_types << "#{base_media_type}-#{version}"
|
|
64
|
+
end
|
|
65
|
+
media_types << "#{base_media_type}+#{extension}"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
media_types << base_media_type
|
|
69
|
+
media_types.concat(content_types.values.flatten)
|
|
70
|
+
media_types
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Grape
|
|
4
4
|
module Middleware
|
|
@@ -8,120 +8,103 @@ module Grape
|
|
|
8
8
|
# application/vnd.:vendor-:version+:format
|
|
9
9
|
#
|
|
10
10
|
# Example: For request header
|
|
11
|
-
# Accept: application/vnd.mycompany-v1+json
|
|
11
|
+
# Accept: application/vnd.mycompany.a-cool-resource-v1+json
|
|
12
12
|
#
|
|
13
13
|
# The following rack env variables are set:
|
|
14
14
|
#
|
|
15
15
|
# env['api.type'] => 'application'
|
|
16
|
-
# env['api.subtype'] => 'vnd.mycompany-v1+json'
|
|
17
|
-
# env['api.vendor] => 'mycompany'
|
|
16
|
+
# env['api.subtype'] => 'vnd.mycompany.a-cool-resource-v1+json'
|
|
17
|
+
# env['api.vendor] => 'mycompany.a-cool-resource'
|
|
18
18
|
# env['api.version] => 'v1'
|
|
19
|
-
# env['api.format] => '
|
|
19
|
+
# env['api.format] => 'json'
|
|
20
20
|
#
|
|
21
21
|
# If version does not match this route, then a 406 is raised with
|
|
22
|
-
# X-Cascade header to alert
|
|
22
|
+
# X-Cascade header to alert Grape::Router to attempt the next matched
|
|
23
23
|
# route.
|
|
24
24
|
class Header < Base
|
|
25
|
-
|
|
26
25
|
def before
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
header.qvalues.reject! do |media_type,_|
|
|
36
|
-
Rack::Accept::Header.parse_media_type(media_type).find{|s| s == '*'}
|
|
37
|
-
end
|
|
38
|
-
# If all Accept headers included a range:
|
|
39
|
-
if header.qvalues.empty?
|
|
40
|
-
throw :error, :status => 406, :headers => error_headers, :message => 'Accept header must not contain ranges ("*").'
|
|
41
|
-
end
|
|
26
|
+
match_best_quality_media_type! do |media_type|
|
|
27
|
+
env.update(
|
|
28
|
+
Grape::Env::API_TYPE => media_type.type,
|
|
29
|
+
Grape::Env::API_SUBTYPE => media_type.subtype,
|
|
30
|
+
Grape::Env::API_VENDOR => media_type.vendor,
|
|
31
|
+
Grape::Env::API_VERSION => media_type.version,
|
|
32
|
+
Grape::Env::API_FORMAT => media_type.format
|
|
33
|
+
)
|
|
42
34
|
end
|
|
35
|
+
end
|
|
43
36
|
|
|
44
|
-
|
|
37
|
+
private
|
|
45
38
|
|
|
39
|
+
def match_best_quality_media_type!
|
|
40
|
+
return unless vendor
|
|
41
|
+
|
|
42
|
+
strict_header_checks!
|
|
43
|
+
media_type = Grape::Util::MediaType.best_quality(accept_header, available_media_types)
|
|
46
44
|
if media_type
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if /\Avnd\.([a-z0-9*.]+)(?:-([a-z0-9*\-.]+))?(?:\+([a-z0-9*\-.+]+))?\z/ =~ subtype
|
|
52
|
-
env['api.vendor'] = $1
|
|
53
|
-
env['api.version'] = $2
|
|
54
|
-
env['api.format'] = $3 # weird that Grape::Middleware::Formatter also does this
|
|
55
|
-
end
|
|
56
|
-
# If none of the available content types are acceptable:
|
|
57
|
-
elsif strict?
|
|
58
|
-
throw :error, :status => 406, :headers => error_headers, :message => '406 Not Acceptable'
|
|
59
|
-
# If all acceptable content types specify a vendor or version that doesn't exist:
|
|
60
|
-
elsif header.values.all?{ |media_type| has_vendor?(media_type) || has_version?(media_type)}
|
|
61
|
-
throw :error, :status => 406, :headers => error_headers, :message => 'API vendor or version not found.'
|
|
45
|
+
yield media_type
|
|
46
|
+
else
|
|
47
|
+
fail!
|
|
62
48
|
end
|
|
63
49
|
end
|
|
64
50
|
|
|
65
|
-
|
|
51
|
+
def accept_header
|
|
52
|
+
env['HTTP_ACCEPT']
|
|
53
|
+
end
|
|
66
54
|
|
|
67
|
-
def
|
|
68
|
-
|
|
55
|
+
def strict_header_checks!
|
|
56
|
+
return unless strict
|
|
69
57
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
end
|
|
74
|
-
available_media_types << "application/vnd.#{vendor}+#{extension}"
|
|
75
|
-
end
|
|
58
|
+
accept_header_check!
|
|
59
|
+
version_and_vendor_check!
|
|
60
|
+
end
|
|
76
61
|
|
|
77
|
-
|
|
62
|
+
def accept_header_check!
|
|
63
|
+
return if accept_header.present?
|
|
78
64
|
|
|
79
|
-
|
|
80
|
-
available_media_types << media_type
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
available_media_types = available_media_types.flatten
|
|
65
|
+
invalid_accept_header!('Accept header must be set.')
|
|
84
66
|
end
|
|
85
67
|
|
|
86
|
-
def
|
|
87
|
-
|
|
68
|
+
def version_and_vendor_check!
|
|
69
|
+
return if versions.blank? || version_and_vendor?
|
|
70
|
+
|
|
71
|
+
invalid_accept_header!('API vendor or version not found.')
|
|
88
72
|
end
|
|
89
73
|
|
|
90
|
-
def
|
|
91
|
-
|
|
74
|
+
def q_values_mime_types
|
|
75
|
+
@q_values_mime_types ||= Rack::Utils.q_values(accept_header).map(&:first)
|
|
92
76
|
end
|
|
93
77
|
|
|
94
|
-
def
|
|
95
|
-
|
|
78
|
+
def version_and_vendor?
|
|
79
|
+
q_values_mime_types.any? { |mime_type| Grape::Util::MediaType.match?(mime_type) }
|
|
96
80
|
end
|
|
97
81
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
# this behavior, and not add the `X-Cascade` header, one can set the `:cascade` option to `false`.
|
|
101
|
-
def cascade?
|
|
102
|
-
options[:version_options] && options[:version_options].has_key?(:cascade) ?
|
|
103
|
-
!! options[:version_options][:cascade] :
|
|
104
|
-
true
|
|
82
|
+
def invalid_accept_header!(message)
|
|
83
|
+
raise Grape::Exceptions::InvalidAcceptHeader.new(message, error_headers)
|
|
105
84
|
end
|
|
106
85
|
|
|
107
|
-
def
|
|
108
|
-
|
|
86
|
+
def invalid_version_header!(message)
|
|
87
|
+
raise Grape::Exceptions::InvalidVersionHeader.new(message, error_headers)
|
|
109
88
|
end
|
|
110
89
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
90
|
+
def fail!
|
|
91
|
+
return if env[Grape::Env::GRAPE_ALLOWED_METHODS].present?
|
|
92
|
+
|
|
93
|
+
media_types = q_values_mime_types.map { |mime_type| Grape::Util::MediaType.parse(mime_type) }
|
|
94
|
+
vendor_not_found!(media_types) || version_not_found!(media_types)
|
|
116
95
|
end
|
|
117
96
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
subtype[/\Avnd\.[a-z0-9*.]+-[a-z0-9*\-.]+/]
|
|
97
|
+
def vendor_not_found!(media_types)
|
|
98
|
+
return unless media_types.all? { |media_type| media_type&.vendor && media_type.vendor != vendor }
|
|
99
|
+
|
|
100
|
+
invalid_accept_header!('API vendor not found.')
|
|
123
101
|
end
|
|
124
102
|
|
|
103
|
+
def version_not_found!(media_types)
|
|
104
|
+
return unless media_types.all? { |media_type| media_type&.version && versions && !versions.include?(media_type.version) }
|
|
105
|
+
|
|
106
|
+
invalid_version_header!('API version not found.')
|
|
107
|
+
end
|
|
125
108
|
end
|
|
126
109
|
end
|
|
127
110
|
end
|
|
@@ -1,43 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Grape
|
|
4
4
|
module Middleware
|
|
5
5
|
module Versioner
|
|
6
6
|
# This middleware sets various version related rack environment variables
|
|
7
|
-
# based on the request parameters and removes that parameter from the
|
|
7
|
+
# based on the request parameters and removes that parameter from the
|
|
8
8
|
# request parameters for subsequent middleware and API.
|
|
9
9
|
# If the version substring does not match any potential initialized
|
|
10
10
|
# versions, a 404 error is thrown.
|
|
11
11
|
# If the version substring is not passed the version (highest mounted)
|
|
12
12
|
# version will be used.
|
|
13
|
-
#
|
|
13
|
+
#
|
|
14
14
|
# Example: For a uri path
|
|
15
15
|
# /resource?apiver=v1
|
|
16
|
-
#
|
|
16
|
+
#
|
|
17
17
|
# The following rack env variables are set and path is rewritten to
|
|
18
18
|
# '/resource':
|
|
19
|
-
#
|
|
19
|
+
#
|
|
20
20
|
# env['api.version'] => 'v1'
|
|
21
21
|
class Param < Base
|
|
22
|
-
def default_options
|
|
23
|
-
{
|
|
24
|
-
:parameter => "apiver"
|
|
25
|
-
}
|
|
26
|
-
end
|
|
27
|
-
|
|
28
22
|
def before
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
potential_version = query_params[parameter]
|
|
24
|
+
return if potential_version.blank?
|
|
31
25
|
|
|
32
|
-
unless potential_version
|
|
33
|
-
|
|
34
|
-
throw :error, :status => 404, :message => "404 API Version Not Found", :headers => {'X-Cascade' => 'pass'}
|
|
35
|
-
end
|
|
36
|
-
env['api.version'] = potential_version
|
|
37
|
-
env['rack.request.query_hash'].delete(paramkey)
|
|
38
|
-
end
|
|
26
|
+
version_not_found! unless potential_version_match?(potential_version)
|
|
27
|
+
env[Grape::Env::API_VERSION] = env[Rack::RACK_REQUEST_QUERY_HASH].delete(parameter)
|
|
39
28
|
end
|
|
40
|
-
|
|
41
29
|
end
|
|
42
30
|
end
|
|
43
31
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Grape
|
|
4
4
|
module Middleware
|
|
@@ -7,48 +7,33 @@ module Grape
|
|
|
7
7
|
# based on the uri path and removes the version substring from the uri
|
|
8
8
|
# path. If the version substring does not match any potential initialized
|
|
9
9
|
# versions, a 404 error is thrown.
|
|
10
|
-
#
|
|
10
|
+
#
|
|
11
11
|
# Example: For a uri path
|
|
12
12
|
# /v1/resource
|
|
13
|
-
#
|
|
13
|
+
#
|
|
14
14
|
# The following rack env variables are set and path is rewritten to
|
|
15
15
|
# '/resource':
|
|
16
|
-
#
|
|
16
|
+
#
|
|
17
17
|
# env['api.version'] => 'v1'
|
|
18
|
-
#
|
|
18
|
+
#
|
|
19
19
|
class Path < Base
|
|
20
|
-
def default_options
|
|
21
|
-
{
|
|
22
|
-
:pattern => /.*/i
|
|
23
|
-
}
|
|
24
|
-
end
|
|
25
|
-
|
|
26
20
|
def before
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if prefix && path.index(prefix) == 0
|
|
30
|
-
path.sub!(prefix, '')
|
|
31
|
-
path = Rack::Mount::Utils.normalize_path(path)
|
|
32
|
-
end
|
|
21
|
+
path_info = Grape::Router.normalize_path(env[Rack::PATH_INFO])
|
|
22
|
+
return if path_info == '/'
|
|
33
23
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if potential_version =~ options[:pattern]
|
|
37
|
-
if options[:versions] && ! options[:versions].find { |v| v.to_s == potential_version }
|
|
38
|
-
throw :error, :status => 404, :message => "404 API Version Not Found"
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
truncated_path = "/#{pieces[2..-1].join('/')}"
|
|
42
|
-
env['api.version'] = potential_version
|
|
24
|
+
[mount_path, Grape::Router.normalize_path(prefix)].each do |path|
|
|
25
|
+
path_info = path_info.delete_prefix(path) if path.present? && path != '/' && path_info.start_with?(path)
|
|
43
26
|
end
|
|
44
|
-
end
|
|
45
27
|
|
|
46
|
-
|
|
28
|
+
slash_position = path_info.index('/', 1) # omit the first one
|
|
29
|
+
return unless slash_position
|
|
47
30
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
end
|
|
31
|
+
potential_version = path_info[1..(slash_position - 1)]
|
|
32
|
+
return unless potential_version.match?(pattern)
|
|
51
33
|
|
|
34
|
+
version_not_found! unless potential_version_match?(potential_version)
|
|
35
|
+
env[Grape::Env::API_VERSION] = potential_version
|
|
36
|
+
end
|
|
52
37
|
end
|
|
53
38
|
end
|
|
54
39
|
end
|
|
@@ -1,31 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# Versioners set env['api.version'] when a version is defined on an API and
|
|
2
4
|
# on the requests. The current methods for determining version are:
|
|
3
5
|
#
|
|
4
6
|
# :header - version from HTTP Accept header.
|
|
7
|
+
# :accept_version_header - version from HTTP Accept-Version header
|
|
5
8
|
# :path - version from uri. e.g. /v1/resource
|
|
6
9
|
# :param - version from uri query string, e.g. /v1/resource?apiver=v1
|
|
7
|
-
#
|
|
8
10
|
# See individual classes for details.
|
|
9
11
|
module Grape
|
|
10
12
|
module Middleware
|
|
11
13
|
module Versioner
|
|
12
|
-
extend
|
|
14
|
+
extend Grape::Util::Registry
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
# @param strategy [Symbol] :path, :header, :accept_version_header or :param
|
|
15
19
|
# @return a middleware class based on strategy
|
|
16
20
|
def using(strategy)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
when :header
|
|
21
|
-
Header
|
|
22
|
-
when :param
|
|
23
|
-
Param
|
|
24
|
-
when :accept_version_header
|
|
25
|
-
AcceptVersionHeader
|
|
26
|
-
else
|
|
27
|
-
raise Grape::Exceptions::InvalidVersionerOption.new(strategy)
|
|
28
|
-
end
|
|
21
|
+
raise Grape::Exceptions::InvalidVersionerOption, strategy unless registry.key?(strategy)
|
|
22
|
+
|
|
23
|
+
registry[strategy]
|
|
29
24
|
end
|
|
30
25
|
end
|
|
31
26
|
end
|
data/lib/grape/namespace.rb
CHANGED
|
@@ -1,24 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
4
|
+
# A container for endpoints or other namespaces, which allows for both
|
|
5
|
+
# logical grouping of endpoints as well as sharing common configuration.
|
|
6
|
+
# May also be referred to as group, segment, or resource.
|
|
2
7
|
class Namespace
|
|
3
|
-
attr_reader :space, :options
|
|
8
|
+
attr_reader :space, :requirements, :options
|
|
4
9
|
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
# @param space [String] the name of this namespace
|
|
11
|
+
# @param options [Hash] options hash
|
|
12
|
+
# @option options :requirements [Hash] param-regex pairs, all of which must
|
|
13
|
+
# be met by a request's params for all endpoints in this namespace, or
|
|
14
|
+
# validation will fail and return a 422.
|
|
15
|
+
def initialize(space, requirements: nil, **options)
|
|
16
|
+
@space = space.to_s
|
|
17
|
+
@requirements = requirements
|
|
18
|
+
@options = options
|
|
9
19
|
end
|
|
10
20
|
|
|
11
|
-
|
|
12
|
-
|
|
21
|
+
# (see ::joined_space_path)
|
|
22
|
+
def self.joined_space(settings)
|
|
23
|
+
settings&.map(&:space)
|
|
13
24
|
end
|
|
14
25
|
|
|
15
|
-
def
|
|
16
|
-
|
|
26
|
+
def eql?(other)
|
|
27
|
+
other.class == self.class &&
|
|
28
|
+
other.space == space &&
|
|
29
|
+
other.requirements == requirements &&
|
|
30
|
+
other.options == options
|
|
17
31
|
end
|
|
32
|
+
alias == eql?
|
|
18
33
|
|
|
34
|
+
def hash
|
|
35
|
+
[self.class, space, requirements, options].hash
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Join the namespaces from a list of settings to create a path prefix.
|
|
39
|
+
# @param settings [Array] list of Grape::Util::InheritableSettings.
|
|
19
40
|
def self.joined_space_path(settings)
|
|
20
|
-
|
|
41
|
+
JoinedSpaceCache[joined_space(settings)]
|
|
21
42
|
end
|
|
22
43
|
|
|
44
|
+
class JoinedSpaceCache < Grape::Util::Cache
|
|
45
|
+
def initialize
|
|
46
|
+
super
|
|
47
|
+
@cache = Hash.new do |h, joined_space|
|
|
48
|
+
h[joined_space] = Grape::Router.normalize_path(joined_space.join('/'))
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
23
52
|
end
|
|
24
53
|
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module ParamsBuilder
|
|
5
|
+
class Base
|
|
6
|
+
class << self
|
|
7
|
+
def call(_params)
|
|
8
|
+
raise NotImplementedError
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def inherited(klass)
|
|
14
|
+
super
|
|
15
|
+
ParamsBuilder.register(klass)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module ParamsBuilder
|
|
5
|
+
extend Grape::Util::Registry
|
|
6
|
+
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def params_builder_for(short_name)
|
|
10
|
+
raise Grape::Exceptions::UnknownParamsBuilder, short_name unless registry.key?(short_name)
|
|
11
|
+
|
|
12
|
+
registry[short_name]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
data/lib/grape/parser/base.rb
CHANGED
|
@@ -1,30 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Parser
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
PARSERS = {
|
|
8
|
-
:json => Grape::Parser::Json,
|
|
9
|
-
:xml => Grape::Parser::Xml
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
def parsers(options)
|
|
13
|
-
PARSERS.merge(options[:parsers] || {})
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def parser_for(api_format, options = {})
|
|
17
|
-
spec = parsers(options)[api_format]
|
|
18
|
-
case spec
|
|
19
|
-
when nil
|
|
20
|
-
nil
|
|
21
|
-
when Symbol
|
|
22
|
-
method(spec)
|
|
23
|
-
else
|
|
24
|
-
spec
|
|
25
|
-
end
|
|
26
|
-
end
|
|
5
|
+
class Base
|
|
6
|
+
def self.call(_object, _env)
|
|
7
|
+
raise NotImplementedError
|
|
8
|
+
end
|
|
27
9
|
|
|
10
|
+
def self.inherited(klass)
|
|
11
|
+
super
|
|
12
|
+
Parser.register(klass)
|
|
28
13
|
end
|
|
29
14
|
end
|
|
30
15
|
end
|
data/lib/grape/parser/json.rb
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Parser
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
class Json < Base
|
|
6
|
+
def self.call(object, _env)
|
|
7
|
+
::Grape::Json.load(object)
|
|
8
|
+
rescue ::Grape::Json::ParseError
|
|
9
|
+
# handle JSON parsing errors via the rescue handlers or provide error message
|
|
10
|
+
raise Grape::Exceptions::InvalidMessageBody.new('application/json')
|
|
10
11
|
end
|
|
11
12
|
end
|
|
12
13
|
end
|
data/lib/grape/parser/xml.rb
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Parser
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
class Xml < Base
|
|
6
|
+
def self.call(object, _env)
|
|
7
|
+
::Grape::Xml.parse(object)
|
|
8
|
+
rescue ::Grape::Xml::ParseError
|
|
9
|
+
# handle XML parsing errors via the rescue handlers or provide error message
|
|
10
|
+
raise Grape::Exceptions::InvalidMessageBody.new('application/xml')
|
|
10
11
|
end
|
|
11
12
|
end
|
|
12
13
|
end
|
data/lib/grape/parser.rb
ADDED