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,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Exceptions
|
|
5
|
+
class ValidationErrors < Base
|
|
6
|
+
include Enumerable
|
|
7
|
+
|
|
8
|
+
attr_reader :errors
|
|
9
|
+
|
|
10
|
+
def initialize(errors: [], headers: {})
|
|
11
|
+
@errors = errors.group_by(&:params)
|
|
12
|
+
super(message: full_messages.join(', '), status: 400, headers:)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def each
|
|
16
|
+
errors.each_pair do |attribute, errors|
|
|
17
|
+
errors.each do |error|
|
|
18
|
+
yield attribute, error
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def as_json(**_opts)
|
|
24
|
+
errors.map do |k, v|
|
|
25
|
+
{
|
|
26
|
+
params: k,
|
|
27
|
+
messages: v.map(&:to_s)
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def to_json(*_opts)
|
|
33
|
+
as_json.to_json
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def full_messages
|
|
37
|
+
messages = map do |attributes, error|
|
|
38
|
+
translate(
|
|
39
|
+
:format,
|
|
40
|
+
scope: 'grape.errors',
|
|
41
|
+
default: '%<attributes>s %<message>s',
|
|
42
|
+
attributes: translate_attributes(attributes),
|
|
43
|
+
message: error.message
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
messages.uniq!
|
|
47
|
+
messages
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def translate_attributes(keys)
|
|
53
|
+
keys.map do |key|
|
|
54
|
+
translate(key, scope: 'grape.errors.attributes', default: key.to_s)
|
|
55
|
+
end.join(', ')
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
data/lib/grape/formatter/base.rb
CHANGED
|
@@ -1,32 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Formatter
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
FORMATTERS = {
|
|
8
|
-
:json => Grape::Formatter::Json,
|
|
9
|
-
:serializable_hash => Grape::Formatter::SerializableHash,
|
|
10
|
-
:txt => Grape::Formatter::Txt,
|
|
11
|
-
:xml => Grape::Formatter::Xml
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
def formatters(options)
|
|
15
|
-
FORMATTERS.merge(options[:formatters] || {})
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def formatter_for(api_format, options = {})
|
|
19
|
-
spec = formatters(options)[api_format]
|
|
20
|
-
case spec
|
|
21
|
-
when nil
|
|
22
|
-
lambda { |obj, env| obj }
|
|
23
|
-
when Symbol
|
|
24
|
-
method(spec)
|
|
25
|
-
else
|
|
26
|
-
spec
|
|
27
|
-
end
|
|
28
|
-
end
|
|
5
|
+
class Base
|
|
6
|
+
def self.call(_object, _env)
|
|
7
|
+
raise NotImplementedError
|
|
8
|
+
end
|
|
29
9
|
|
|
10
|
+
def self.inherited(klass)
|
|
11
|
+
super
|
|
12
|
+
Formatter.register(klass)
|
|
30
13
|
end
|
|
31
14
|
end
|
|
32
15
|
end
|
data/lib/grape/formatter/json.rb
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Formatter
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def call(object, env)
|
|
7
|
-
return object.to_json if object.respond_to?(:to_json)
|
|
8
|
-
MultiJson.dump(object)
|
|
9
|
-
end
|
|
5
|
+
class Json < Base
|
|
6
|
+
def self.call(object, _env)
|
|
7
|
+
return object.to_json if object.respond_to?(:to_json)
|
|
10
8
|
|
|
9
|
+
::Grape::Json.dump(object)
|
|
11
10
|
end
|
|
12
11
|
end
|
|
13
12
|
end
|
|
@@ -1,34 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Formatter
|
|
3
|
-
|
|
5
|
+
class SerializableHash < Base
|
|
4
6
|
class << self
|
|
5
|
-
|
|
6
|
-
def call(object, env)
|
|
7
|
+
def call(object, _env)
|
|
7
8
|
return object if object.is_a?(String)
|
|
8
|
-
return
|
|
9
|
+
return ::Grape::Json.dump(serialize(object)) if serializable?(object)
|
|
9
10
|
return object.to_json if object.respond_to?(:to_json)
|
|
10
|
-
|
|
11
|
+
|
|
12
|
+
::Grape::Json.dump(object)
|
|
11
13
|
end
|
|
12
14
|
|
|
13
15
|
private
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
object.kind_of?(Hash)
|
|
19
|
-
end
|
|
17
|
+
def serializable?(object)
|
|
18
|
+
object.respond_to?(:serializable_hash) || array_serializable?(object) || object.is_a?(Hash)
|
|
19
|
+
end
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
end
|
|
21
|
+
def serialize(object)
|
|
22
|
+
if object.respond_to? :serializable_hash
|
|
23
|
+
object.serializable_hash
|
|
24
|
+
elsif array_serializable?(object)
|
|
25
|
+
object.map(&:serializable_hash)
|
|
26
|
+
elsif object.is_a?(Hash)
|
|
27
|
+
object.transform_values { |v| serialize(v) }
|
|
28
|
+
else
|
|
29
|
+
object
|
|
31
30
|
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def array_serializable?(object)
|
|
34
|
+
object.is_a?(Array) && object.all? { |o| o.respond_to? :serializable_hash }
|
|
35
|
+
end
|
|
32
36
|
end
|
|
33
37
|
end
|
|
34
38
|
end
|
data/lib/grape/formatter/txt.rb
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Formatter
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def call(object, env)
|
|
7
|
-
object.respond_to?(:to_txt) ? object.to_txt : object.to_s
|
|
8
|
-
end
|
|
9
|
-
|
|
5
|
+
class Txt < Base
|
|
6
|
+
def self.call(object, _env)
|
|
7
|
+
object.respond_to?(:to_txt) ? object.to_txt : object.to_s
|
|
10
8
|
end
|
|
11
9
|
end
|
|
12
10
|
end
|
data/lib/grape/formatter/xml.rb
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Formatter
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def call(object, env)
|
|
7
|
-
return object.to_xml if object.respond_to?(:to_xml)
|
|
8
|
-
raise Grape::Exceptions::InvalidFormatter.new(object.class, 'xml')
|
|
9
|
-
end
|
|
5
|
+
class Xml < Base
|
|
6
|
+
def self.call(object, _env)
|
|
7
|
+
return object.to_xml if object.respond_to?(:to_xml)
|
|
10
8
|
|
|
9
|
+
raise Grape::Exceptions::InvalidFormatter.new(object.class, 'xml')
|
|
11
10
|
end
|
|
12
11
|
end
|
|
13
12
|
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Formatter
|
|
5
|
+
extend Grape::Util::Registry
|
|
6
|
+
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
DEFAULT_LAMBDA_FORMATTER = ->(obj, _env) { obj }
|
|
10
|
+
|
|
11
|
+
def formatter_for(api_format, formatters)
|
|
12
|
+
return formatters[api_format] if formatters&.key?(api_format)
|
|
13
|
+
|
|
14
|
+
registry[api_format] || DEFAULT_LAMBDA_FORMATTER
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/grape/json.rb
ADDED
data/lib/grape/locale/en.yml
CHANGED
|
@@ -1,27 +1,54 @@
|
|
|
1
|
+
---
|
|
1
2
|
en:
|
|
2
3
|
grape:
|
|
3
4
|
errors:
|
|
5
|
+
format: '%{attributes} %{message}'
|
|
4
6
|
messages:
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
problem: '
|
|
14
|
-
resolution:
|
|
15
|
-
"you can choose exist mime type from Grape::ContentTypes::CONTENT_TYPES
|
|
16
|
-
or add your own with content_type :%{new_format}, 'application/%{new_format}'
|
|
17
|
-
"
|
|
18
|
-
invalid_with_option_for_represent:
|
|
19
|
-
problem: 'You must specify an entity class in the :with option.'
|
|
20
|
-
resolution: 'eg: represent User, :with => Entity::User'
|
|
21
|
-
missing_option: 'You must specify :%{option} options.'
|
|
7
|
+
all_or_none: 'provide all or none of parameters'
|
|
8
|
+
at_least_one: 'are missing, at least one parameter must be provided'
|
|
9
|
+
blank: 'is empty'
|
|
10
|
+
coerce: 'is invalid'
|
|
11
|
+
exactly_one: 'are missing, exactly one parameter must be provided'
|
|
12
|
+
except_values: 'has a value not allowed'
|
|
13
|
+
incompatible_option_values: '%{option1}: %{value1} is incompatible with %{option2}: %{value2}'
|
|
14
|
+
invalid_accept_header:
|
|
15
|
+
problem: 'invalid accept header'
|
|
16
|
+
resolution: '%{message}'
|
|
22
17
|
invalid_formatter: 'cannot convert %{klass} to %{to_format}'
|
|
18
|
+
invalid_message_body:
|
|
19
|
+
problem: 'message body does not match declared format'
|
|
20
|
+
resolution: 'when specifying %{body_format} as content-type, you must pass valid %{body_format} in the request''s ''body'' '
|
|
21
|
+
invalid_response: 'Invalid response'
|
|
22
|
+
invalid_version_header:
|
|
23
|
+
problem: 'invalid version header'
|
|
24
|
+
resolution: '%{message}'
|
|
23
25
|
invalid_versioner_option:
|
|
24
|
-
problem: '
|
|
25
|
-
resolution: 'available strategy for :using is :path, :header, :param'
|
|
26
|
-
|
|
26
|
+
problem: 'unknown :using for versioner: %{strategy}'
|
|
27
|
+
resolution: 'available strategy for :using is :path, :header, :accept_version_header, :param'
|
|
28
|
+
invalid_with_option_for_represent:
|
|
29
|
+
problem: 'you must specify an entity class in the :with option'
|
|
30
|
+
resolution: 'eg: represent User, :with => Entity::User'
|
|
31
|
+
length: 'is expected to have length within %{min} and %{max}'
|
|
32
|
+
length_is: 'is expected to have length exactly equal to %{is}'
|
|
33
|
+
length_max: 'is expected to have length less than or equal to %{max}'
|
|
34
|
+
length_min: 'is expected to have length greater than or equal to %{min}'
|
|
35
|
+
missing_group_type: 'group type is required'
|
|
36
|
+
missing_mime_type:
|
|
37
|
+
problem: 'missing mime type for %{new_format}'
|
|
38
|
+
resolution: 'you can choose existing mime type from Grape::ContentTypes::CONTENT_TYPES or add your own with content_type :%{new_format}, ''application/%{new_format}'' '
|
|
39
|
+
missing_option: 'you must specify :%{option} options'
|
|
40
|
+
missing_vendor_option:
|
|
41
|
+
problem: 'missing :vendor option'
|
|
42
|
+
resolution: 'eg: version ''v1'', using: :header, vendor: ''twitter'''
|
|
43
|
+
summary: 'when version using header, you must specify :vendor option'
|
|
44
|
+
mutual_exclusion: 'are mutually exclusive'
|
|
45
|
+
presence: 'is missing'
|
|
46
|
+
regexp: 'is invalid'
|
|
47
|
+
same_as: 'is not the same as %{parameter}'
|
|
48
|
+
unknown_auth_strategy: 'unknown auth strategy: %{strategy}'
|
|
27
49
|
unknown_options: 'unknown options: %{options}'
|
|
50
|
+
unknown_parameter: 'unknown parameter: %{param}'
|
|
51
|
+
unknown_params_builder: 'unknown params_builder: %{params_builder_type}'
|
|
52
|
+
unknown_validator: 'unknown validator: %{validator_type}'
|
|
53
|
+
unsupported_group_type: 'group type must be Array, Hash, JSON or Array[JSON]'
|
|
54
|
+
values: 'does not have a valid value'
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Middleware
|
|
5
|
+
module Auth
|
|
6
|
+
class Base < Grape::Middleware::Base
|
|
7
|
+
def initialize(app, **options)
|
|
8
|
+
super
|
|
9
|
+
return unless options.key?(:type)
|
|
10
|
+
|
|
11
|
+
@auth_strategy = Grape::Middleware::Auth::Strategies[options[:type]].tap do |auth_strategy|
|
|
12
|
+
raise Grape::Exceptions::UnknownAuthStrategy.new(strategy: options[:type]) unless auth_strategy
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call!(env)
|
|
17
|
+
@env = env
|
|
18
|
+
@auth_strategy.create(app, options) do |*args|
|
|
19
|
+
context.instance_exec(*args, &options[:proc])
|
|
20
|
+
end.call(env)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Middleware
|
|
5
|
+
module Auth
|
|
6
|
+
module DSL
|
|
7
|
+
def auth(type = nil, options = {}, &block)
|
|
8
|
+
namespace_inheritable = inheritable_setting.namespace_inheritable
|
|
9
|
+
return namespace_inheritable[:auth] unless type
|
|
10
|
+
|
|
11
|
+
namespace_inheritable[:auth] = options.reverse_merge(type: type.to_sym, proc: block)
|
|
12
|
+
use Grape::Middleware::Auth::Base, namespace_inheritable[:auth]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Add HTTP Basic authorization to the API.
|
|
16
|
+
#
|
|
17
|
+
# @param [Hash] options A hash of options.
|
|
18
|
+
# @option options [String] :realm "API Authorization" The HTTP Basic realm.
|
|
19
|
+
def http_basic(options = {}, &)
|
|
20
|
+
options[:realm] ||= 'API Authorization'
|
|
21
|
+
auth(:http_basic, options, &)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def http_digest(options = {}, &)
|
|
25
|
+
options[:realm] ||= 'API Authorization'
|
|
26
|
+
|
|
27
|
+
if options[:realm].respond_to?(:values_at)
|
|
28
|
+
options[:realm][:opaque] ||= 'secret'
|
|
29
|
+
else
|
|
30
|
+
options[:opaque] ||= 'secret'
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
auth(:http_digest, options, &)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Middleware
|
|
5
|
+
module Auth
|
|
6
|
+
module Strategies
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def add(label, strategy, option_fetcher = ->(_) { [] })
|
|
10
|
+
auth_strategies[label] = StrategyInfo.new(strategy, option_fetcher)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def auth_strategies
|
|
14
|
+
@auth_strategies ||= {
|
|
15
|
+
http_basic: StrategyInfo.new(Rack::Auth::Basic, ->(settings) { [settings[:realm]] })
|
|
16
|
+
}
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def [](label)
|
|
20
|
+
auth_strategies[label]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Middleware
|
|
5
|
+
module Auth
|
|
6
|
+
StrategyInfo = Struct.new(:auth_class, :settings_fetcher) do
|
|
7
|
+
def create(app, options, &block)
|
|
8
|
+
strategy_args = settings_fetcher.call(options)
|
|
9
|
+
|
|
10
|
+
auth_class.new(app, *strategy_args, &block)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -1,62 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Middleware
|
|
3
5
|
class Base
|
|
6
|
+
include Grape::DSL::Headers
|
|
7
|
+
|
|
4
8
|
attr_reader :app, :env, :options
|
|
5
9
|
|
|
6
10
|
# @param [Rack Application] app The standard argument for a Rack middleware.
|
|
7
11
|
# @param [Hash] options A hash of options, simply stored for use by subclasses.
|
|
8
|
-
def initialize(app, options
|
|
12
|
+
def initialize(app, **options)
|
|
9
13
|
@app = app
|
|
10
|
-
@options =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def default_options
|
|
14
|
-
{}
|
|
14
|
+
@options = merge_default_options(options)
|
|
15
|
+
@app_response = nil
|
|
15
16
|
end
|
|
16
17
|
|
|
17
18
|
def call(env)
|
|
18
|
-
dup.call!(env)
|
|
19
|
+
dup.call!(env).to_a
|
|
19
20
|
end
|
|
20
21
|
|
|
21
22
|
def call!(env)
|
|
22
23
|
@env = env
|
|
23
24
|
before
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
begin
|
|
26
|
+
@app_response = @app.call(@env)
|
|
27
|
+
ensure
|
|
28
|
+
begin
|
|
29
|
+
after_response = after
|
|
30
|
+
rescue StandardError => e
|
|
31
|
+
warn "caught error of type #{e.class} in after callback inside #{self.class.name} : #{e.message}"
|
|
32
|
+
raise e
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
response = after_response || @app_response
|
|
37
|
+
merge_headers response
|
|
38
|
+
response
|
|
26
39
|
end
|
|
27
40
|
|
|
28
41
|
# @abstract
|
|
29
42
|
# Called before the application is called in the middleware lifecycle.
|
|
30
43
|
def before; end
|
|
44
|
+
|
|
31
45
|
# @abstract
|
|
32
46
|
# Called after the application is called in the middleware lifecycle.
|
|
33
47
|
# @return [Response, nil] a Rack SPEC response or nil to call the application afterwards.
|
|
34
48
|
def after; end
|
|
35
49
|
|
|
36
|
-
def
|
|
37
|
-
|
|
50
|
+
def rack_request
|
|
51
|
+
@rack_request ||= Rack::Request.new(env)
|
|
38
52
|
end
|
|
39
53
|
|
|
40
|
-
def
|
|
41
|
-
|
|
54
|
+
def context
|
|
55
|
+
env[Grape::Env::API_ENDPOINT]
|
|
42
56
|
end
|
|
43
57
|
|
|
44
|
-
def
|
|
45
|
-
|
|
58
|
+
def response
|
|
59
|
+
return @app_response if @app_response.is_a?(Rack::Response)
|
|
60
|
+
|
|
61
|
+
@app_response = Rack::Response[*@app_response]
|
|
46
62
|
end
|
|
47
63
|
|
|
48
64
|
def content_types
|
|
49
|
-
ContentTypes.content_types_for(options[:content_types])
|
|
65
|
+
@content_types ||= Grape::ContentTypes.content_types_for(options[:content_types])
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def mime_types
|
|
69
|
+
@mime_types ||= Grape::ContentTypes.mime_types_for(content_types)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def content_type_for(format)
|
|
73
|
+
content_types_indifferent_access[format]
|
|
50
74
|
end
|
|
51
75
|
|
|
52
76
|
def content_type
|
|
53
|
-
content_type_for(env[
|
|
77
|
+
content_type_for(env[Grape::Env::API_FORMAT] || options[:format]) || 'text/html'
|
|
54
78
|
end
|
|
55
79
|
|
|
56
|
-
def
|
|
57
|
-
|
|
80
|
+
def query_params
|
|
81
|
+
rack_request.GET
|
|
82
|
+
rescue *Grape::RACK_ERRORS
|
|
83
|
+
raise Grape::Exceptions::RequestError
|
|
58
84
|
end
|
|
59
85
|
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def merge_headers(response)
|
|
89
|
+
return unless headers.is_a?(Hash)
|
|
90
|
+
|
|
91
|
+
case response
|
|
92
|
+
when Rack::Response then response.headers.merge!(headers)
|
|
93
|
+
when Array then response[1].merge!(headers)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def content_types_indifferent_access
|
|
98
|
+
@content_types_indifferent_access ||= content_types.with_indifferent_access
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def merge_default_options(options)
|
|
102
|
+
if respond_to?(:default_options)
|
|
103
|
+
default_options.deep_merge(options)
|
|
104
|
+
elsif self.class.const_defined?(:DEFAULT_OPTIONS)
|
|
105
|
+
self.class::DEFAULT_OPTIONS.deep_merge(options)
|
|
106
|
+
else
|
|
107
|
+
options
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def try_scrub(obj)
|
|
112
|
+
obj.respond_to?(:valid_encoding?) && !obj.valid_encoding? ? obj.scrub : obj
|
|
113
|
+
end
|
|
60
114
|
end
|
|
61
115
|
end
|
|
62
116
|
end
|