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,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module ServeStream
|
|
5
|
+
# A simple class used to identify responses which represent streams (or files) and do not
|
|
6
|
+
# need to be formatted or pre-read by Rack::Response
|
|
7
|
+
class StreamResponse
|
|
8
|
+
attr_reader :stream
|
|
9
|
+
|
|
10
|
+
# @param stream [Object]
|
|
11
|
+
def initialize(stream)
|
|
12
|
+
@stream = stream
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Equality provided mostly for tests.
|
|
16
|
+
#
|
|
17
|
+
# @return [Boolean]
|
|
18
|
+
def ==(other)
|
|
19
|
+
stream == other.stream
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
class ApiDescription
|
|
6
|
+
DSL_METHODS = %i[
|
|
7
|
+
body_name
|
|
8
|
+
consumes
|
|
9
|
+
default
|
|
10
|
+
deprecated
|
|
11
|
+
detail
|
|
12
|
+
entity
|
|
13
|
+
headers
|
|
14
|
+
hidden
|
|
15
|
+
http_codes
|
|
16
|
+
is_array
|
|
17
|
+
named
|
|
18
|
+
nickname
|
|
19
|
+
params
|
|
20
|
+
produces
|
|
21
|
+
security
|
|
22
|
+
summary
|
|
23
|
+
tags
|
|
24
|
+
].freeze
|
|
25
|
+
|
|
26
|
+
def initialize(description, endpoint_configuration, &)
|
|
27
|
+
@endpoint_configuration = endpoint_configuration
|
|
28
|
+
@attributes = { description: }
|
|
29
|
+
instance_eval(&)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
DSL_METHODS.each do |attribute|
|
|
33
|
+
define_method attribute do |value|
|
|
34
|
+
@attributes[attribute] = value
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
alias success entity
|
|
39
|
+
alias failure http_codes
|
|
40
|
+
|
|
41
|
+
def configuration
|
|
42
|
+
@configuration ||= eval_endpoint_config(@endpoint_configuration)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def settings
|
|
46
|
+
@attributes
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def eval_endpoint_config(configuration)
|
|
52
|
+
return configuration if configuration.is_a?(Hash)
|
|
53
|
+
|
|
54
|
+
configuration.evaluate
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
# Base for classes which need to operate with own values kept
|
|
6
|
+
# in the hash and inherited values kept in a Hash-like object.
|
|
7
|
+
class BaseInheritable
|
|
8
|
+
attr_accessor :inherited_values, :new_values
|
|
9
|
+
|
|
10
|
+
# @param inherited_values [Object] An object implementing an interface
|
|
11
|
+
# of the Hash class.
|
|
12
|
+
def initialize(inherited_values = nil)
|
|
13
|
+
@inherited_values = inherited_values || {}
|
|
14
|
+
@new_values = {}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def delete(*keys)
|
|
18
|
+
keys.map do |key|
|
|
19
|
+
# since delete returns the deleted value, seems natural to `map` the result
|
|
20
|
+
new_values.delete key
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def initialize_copy(other)
|
|
25
|
+
super
|
|
26
|
+
self.inherited_values = other.inherited_values
|
|
27
|
+
self.new_values = other.new_values.dup
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def keys
|
|
31
|
+
if new_values.any?
|
|
32
|
+
inherited_values.keys.tap do |combined|
|
|
33
|
+
combined.concat(new_values.keys)
|
|
34
|
+
combined.uniq!
|
|
35
|
+
end
|
|
36
|
+
else
|
|
37
|
+
inherited_values.keys
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def key?(name)
|
|
42
|
+
inherited_values.key?(name) || new_values.key?(name)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
class Cache
|
|
6
|
+
include Singleton
|
|
7
|
+
|
|
8
|
+
attr_reader :cache
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
extend Forwardable
|
|
12
|
+
|
|
13
|
+
def_delegators :cache, :[]
|
|
14
|
+
def_delegators :instance, :cache
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
module DeepFreeze
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# Recursively freezes Hash (keys and values), Array (elements), and String
|
|
9
|
+
# objects. All other types are returned as-is.
|
|
10
|
+
#
|
|
11
|
+
# Intentionally left unfrozen:
|
|
12
|
+
# - Procs / lambdas — may be deferred DB-backed callables
|
|
13
|
+
# - Coercers (e.g. ArrayCoercer) — use lazy ivar memoization at request time
|
|
14
|
+
# - Classes / Modules — shared constants that must remain open
|
|
15
|
+
# - ParamsScope — self-freezes at the end of its own initialize
|
|
16
|
+
def deep_freeze(obj)
|
|
17
|
+
case obj
|
|
18
|
+
when Hash
|
|
19
|
+
obj.each do |k, v|
|
|
20
|
+
deep_freeze(k)
|
|
21
|
+
deep_freeze(v)
|
|
22
|
+
end
|
|
23
|
+
obj.freeze
|
|
24
|
+
when Array
|
|
25
|
+
obj.each { |v| deep_freeze(v) }
|
|
26
|
+
obj.freeze
|
|
27
|
+
when String
|
|
28
|
+
obj.freeze
|
|
29
|
+
else
|
|
30
|
+
obj
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
if Gem::Version.new(Rack.release) >= Gem::Version.new('3')
|
|
6
|
+
require 'rack/headers'
|
|
7
|
+
Header = Rack::Headers
|
|
8
|
+
else
|
|
9
|
+
require 'rack/utils'
|
|
10
|
+
Header = Rack::Utils::HeaderHash
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
# A branchable, inheritable settings object which can store both stackable
|
|
6
|
+
# and inheritable values (see InheritableValues and StackableValues).
|
|
7
|
+
class InheritableSetting
|
|
8
|
+
attr_accessor :route, :api_class, :namespace, :namespace_inheritable, :namespace_stackable, :namespace_reverse_stackable, :parent, :point_in_time_copies
|
|
9
|
+
|
|
10
|
+
# Retrieve global settings.
|
|
11
|
+
def self.global
|
|
12
|
+
@global ||= {}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Clear all global settings.
|
|
16
|
+
# @api private
|
|
17
|
+
# @note only for testing
|
|
18
|
+
def self.reset_global!
|
|
19
|
+
@global = {}
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Instantiate a new settings instance, with blank values. The fresh
|
|
23
|
+
# instance can then be set to inherit from an existing instance (see
|
|
24
|
+
# #inherit_from).
|
|
25
|
+
def initialize
|
|
26
|
+
self.route = {}
|
|
27
|
+
self.api_class = {}
|
|
28
|
+
self.namespace = InheritableValues.new # only inheritable from a parent when
|
|
29
|
+
# used with a mount, or should every API::Class be a separate namespace by default?
|
|
30
|
+
self.namespace_inheritable = InheritableValues.new
|
|
31
|
+
self.namespace_stackable = StackableValues.new
|
|
32
|
+
self.namespace_reverse_stackable = ReverseStackableValues.new
|
|
33
|
+
|
|
34
|
+
self.point_in_time_copies = []
|
|
35
|
+
|
|
36
|
+
self.parent = nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Return the class-level global properties.
|
|
40
|
+
def global
|
|
41
|
+
self.class.global
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Set our inherited values to the given parent's current values. Also,
|
|
45
|
+
# update the inherited values on any settings instances which were forked
|
|
46
|
+
# from us.
|
|
47
|
+
# @param parent [InheritableSetting]
|
|
48
|
+
def inherit_from(parent)
|
|
49
|
+
return if parent.nil?
|
|
50
|
+
|
|
51
|
+
self.parent = parent
|
|
52
|
+
|
|
53
|
+
namespace_inheritable.inherited_values = parent.namespace_inheritable
|
|
54
|
+
namespace_stackable.inherited_values = parent.namespace_stackable
|
|
55
|
+
namespace_reverse_stackable.inherited_values = parent.namespace_reverse_stackable
|
|
56
|
+
self.route = parent.route.merge(route)
|
|
57
|
+
|
|
58
|
+
point_in_time_copies.each { |cloned_one| cloned_one.inherit_from parent }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Create a point-in-time copy of this settings instance, with clones of
|
|
62
|
+
# all our values. Note that, should this instance's parent be set or
|
|
63
|
+
# changed via #inherit_from, it will copy that inheritence to any copies
|
|
64
|
+
# which were made.
|
|
65
|
+
def point_in_time_copy
|
|
66
|
+
self.class.new.tap do |new_setting|
|
|
67
|
+
point_in_time_copies << new_setting
|
|
68
|
+
new_setting.point_in_time_copies = []
|
|
69
|
+
|
|
70
|
+
new_setting.namespace = namespace.clone
|
|
71
|
+
new_setting.namespace_inheritable = namespace_inheritable.clone
|
|
72
|
+
new_setting.namespace_stackable = namespace_stackable.clone
|
|
73
|
+
new_setting.namespace_reverse_stackable = namespace_reverse_stackable.clone
|
|
74
|
+
new_setting.route = route.clone
|
|
75
|
+
new_setting.api_class = api_class
|
|
76
|
+
|
|
77
|
+
new_setting.inherit_from(parent)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Resets the instance store of per-route settings.
|
|
82
|
+
# @api private
|
|
83
|
+
def route_end
|
|
84
|
+
@route = {}
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Return a serializable hash of our values.
|
|
88
|
+
def to_hash
|
|
89
|
+
{
|
|
90
|
+
global: global.clone,
|
|
91
|
+
route: route.clone,
|
|
92
|
+
namespace: namespace.to_hash,
|
|
93
|
+
namespace_inheritable: namespace_inheritable.to_hash,
|
|
94
|
+
namespace_stackable: namespace_stackable.to_hash,
|
|
95
|
+
namespace_reverse_stackable: namespace_reverse_stackable.to_hash
|
|
96
|
+
}
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def namespace_stackable_with_hash(key)
|
|
100
|
+
data = namespace_stackable[key]
|
|
101
|
+
return if data.blank?
|
|
102
|
+
|
|
103
|
+
data.each_with_object({}) { |value, result| result.deep_merge!(value) }
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
class InheritableValues < BaseInheritable
|
|
6
|
+
def [](name)
|
|
7
|
+
values[name]
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def []=(name, value)
|
|
11
|
+
new_values[name] = value
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def merge(new_hash)
|
|
15
|
+
values.merge!(new_hash)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def to_hash
|
|
19
|
+
values
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
protected
|
|
23
|
+
|
|
24
|
+
def values
|
|
25
|
+
@inherited_values.merge(@new_values)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
module Lazy
|
|
6
|
+
class Block
|
|
7
|
+
def initialize(&new_block)
|
|
8
|
+
@block = new_block
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def evaluate_from(configuration)
|
|
12
|
+
@block.call(configuration)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def evaluate
|
|
16
|
+
@block.call({})
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def lazy?
|
|
20
|
+
true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def to_s
|
|
24
|
+
evaluate.to_s
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
module Lazy
|
|
6
|
+
class Value
|
|
7
|
+
attr_reader :access_keys
|
|
8
|
+
|
|
9
|
+
def initialize(value, access_keys = [])
|
|
10
|
+
@value = value
|
|
11
|
+
@access_keys = access_keys
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def evaluate_from(configuration)
|
|
15
|
+
matching_lazy_value = configuration.fetch(@access_keys)
|
|
16
|
+
matching_lazy_value.evaluate
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def evaluate
|
|
20
|
+
@value
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def lazy?
|
|
24
|
+
true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def reached_by(parent_access_keys, access_key)
|
|
28
|
+
@access_keys = parent_access_keys + [access_key]
|
|
29
|
+
self
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def to_s
|
|
33
|
+
evaluate.to_s
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
module Lazy
|
|
6
|
+
class ValueArray < ValueEnumerable
|
|
7
|
+
def initialize(array)
|
|
8
|
+
super
|
|
9
|
+
@value_hash = []
|
|
10
|
+
array.each_with_index do |value, index|
|
|
11
|
+
self[index] = value
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def evaluate
|
|
16
|
+
@value_hash.map(&:evaluate)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
module Lazy
|
|
6
|
+
class ValueEnumerable < Value
|
|
7
|
+
def [](key)
|
|
8
|
+
if @value_hash[key].nil?
|
|
9
|
+
Value.new(nil).reached_by(access_keys, key)
|
|
10
|
+
else
|
|
11
|
+
@value_hash[key].reached_by(access_keys, key)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def fetch(access_keys)
|
|
16
|
+
fetched_keys = access_keys.dup
|
|
17
|
+
value = self[fetched_keys.shift]
|
|
18
|
+
fetched_keys.any? ? value.fetch(fetched_keys) : value
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def []=(key, value)
|
|
22
|
+
@value_hash[key] = case value
|
|
23
|
+
when Hash
|
|
24
|
+
ValueHash.new(value)
|
|
25
|
+
when Array
|
|
26
|
+
ValueArray.new(value)
|
|
27
|
+
else
|
|
28
|
+
Value.new(value)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
module Lazy
|
|
6
|
+
class ValueHash < ValueEnumerable
|
|
7
|
+
def initialize(hash)
|
|
8
|
+
super
|
|
9
|
+
@value_hash = ActiveSupport::HashWithIndifferentAccess.new
|
|
10
|
+
hash.each do |key, value|
|
|
11
|
+
self[key] = value
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def evaluate
|
|
16
|
+
@value_hash.transform_values(&:evaluate)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
class MediaType
|
|
6
|
+
attr_reader :type, :subtype, :vendor, :version, :format
|
|
7
|
+
|
|
8
|
+
# based on the HTTP Accept header with the pattern:
|
|
9
|
+
# application/vnd.:vendor-:version+:format
|
|
10
|
+
VENDOR_VERSION_HEADER_REGEX = /\Avnd\.(?<vendor>[a-z0-9.\-_!^]+?)(?:-(?<version>[a-z0-9*.]+))?(?:\+(?<format>[a-z0-9*\-.]+))?\z/
|
|
11
|
+
|
|
12
|
+
def initialize(type:, subtype:)
|
|
13
|
+
@type = type
|
|
14
|
+
@subtype = subtype
|
|
15
|
+
VENDOR_VERSION_HEADER_REGEX.match(subtype) do |m|
|
|
16
|
+
@vendor = m[:vendor]
|
|
17
|
+
@version = m[:version]
|
|
18
|
+
@format = m[:format]
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def ==(other)
|
|
23
|
+
eql?(other)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def eql?(other)
|
|
27
|
+
self.class == other.class &&
|
|
28
|
+
other.type == type &&
|
|
29
|
+
other.subtype == subtype &&
|
|
30
|
+
other.vendor == vendor &&
|
|
31
|
+
other.version == version &&
|
|
32
|
+
other.format == format
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def hash
|
|
36
|
+
[self.class, type, subtype, vendor, version, format].hash
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class << self
|
|
40
|
+
def best_quality(header, available_media_types)
|
|
41
|
+
parse(best_quality_media_type(header, available_media_types))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def parse(media_type)
|
|
45
|
+
return if media_type.blank?
|
|
46
|
+
|
|
47
|
+
type, subtype = media_type.split('/', 2)
|
|
48
|
+
return if type.blank? || subtype.blank?
|
|
49
|
+
|
|
50
|
+
new(type:, subtype:)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def match?(media_type)
|
|
54
|
+
return false if media_type.blank?
|
|
55
|
+
|
|
56
|
+
subtype = media_type.split('/', 2).last
|
|
57
|
+
return false if subtype.blank?
|
|
58
|
+
|
|
59
|
+
VENDOR_VERSION_HEADER_REGEX.match?(subtype)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def best_quality_media_type(header, available_media_types)
|
|
63
|
+
header.blank? ? available_media_types.first : Rack::Utils.best_q_match(header, available_media_types)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private_class_method :best_quality_media_type
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
module Registry
|
|
6
|
+
def register(klass)
|
|
7
|
+
short_name = build_short_name(klass)
|
|
8
|
+
return if short_name.nil?
|
|
9
|
+
|
|
10
|
+
warn "#{short_name} is already registered with class #{registry[short_name]}. It will be overridden globally with the following: #{klass.name}" if registry.key?(short_name)
|
|
11
|
+
registry[short_name] = klass
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def build_short_name(klass)
|
|
17
|
+
return if klass.name.blank?
|
|
18
|
+
|
|
19
|
+
klass.name.demodulize.underscore
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def registry
|
|
23
|
+
@registry ||= {}.with_indifferent_access
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
class ReverseStackableValues < StackableValues
|
|
6
|
+
protected
|
|
7
|
+
|
|
8
|
+
def concat_values(inherited_value, new_value)
|
|
9
|
+
return inherited_value unless new_value
|
|
10
|
+
|
|
11
|
+
new_value + inherited_value
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
class StackableValues < BaseInheritable
|
|
6
|
+
# Even if there is no value, an empty array will be returned.
|
|
7
|
+
def [](name)
|
|
8
|
+
inherited_value = inherited_values[name]
|
|
9
|
+
new_value = new_values[name]
|
|
10
|
+
|
|
11
|
+
return new_value || [] unless inherited_value
|
|
12
|
+
|
|
13
|
+
concat_values(inherited_value, new_value)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def []=(name, value)
|
|
17
|
+
new_values[name] ||= []
|
|
18
|
+
new_values[name].push value
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def to_hash
|
|
22
|
+
keys.each_with_object({}) do |key, result|
|
|
23
|
+
result[key] = self[key]
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
protected
|
|
28
|
+
|
|
29
|
+
def concat_values(inherited_value, new_value)
|
|
30
|
+
return inherited_value unless new_value
|
|
31
|
+
|
|
32
|
+
inherited_value + new_value
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Util
|
|
5
|
+
module Translation
|
|
6
|
+
FALLBACK_LOCALE = :en
|
|
7
|
+
private_constant :FALLBACK_LOCALE
|
|
8
|
+
# Sentinel returned by I18n when a key is missing (passed as the default:
|
|
9
|
+
# value). Using a named class rather than plain Object.new makes it
|
|
10
|
+
# identifiable in debug output and immune to backends that call .to_s on
|
|
11
|
+
# the default before returning it.
|
|
12
|
+
MISSING = Class.new { def inspect = 'Grape::Util::Translation::MISSING' }.new.freeze
|
|
13
|
+
private_constant :MISSING
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
# Extra keyword args (**) are forwarded verbatim to I18n as interpolation
|
|
18
|
+
# variables (e.g. +min:+, +max:+ from LengthValidator's Hash message).
|
|
19
|
+
# Callers must not pass unintended keyword arguments — any extra keyword
|
|
20
|
+
# will silently become an I18n interpolation variable.
|
|
21
|
+
def translate(key, default: MISSING, scope: 'grape.errors.messages', locale: nil, **)
|
|
22
|
+
i18n_opts = { default:, scope:, ** }
|
|
23
|
+
i18n_opts[:locale] = locale if locale
|
|
24
|
+
message = ::I18n.translate(key, **i18n_opts)
|
|
25
|
+
return message unless message.equal?(MISSING)
|
|
26
|
+
|
|
27
|
+
effective_default = default.equal?(MISSING) ? [*Array(scope), key].join('.') : default
|
|
28
|
+
return effective_default if fallback_locale?(locale) || fallback_locale_unavailable?
|
|
29
|
+
|
|
30
|
+
::I18n.translate(key, default: effective_default, scope:, locale: FALLBACK_LOCALE, **)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def fallback_locale?(locale)
|
|
34
|
+
(locale || ::I18n.locale) == FALLBACK_LOCALE
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def fallback_locale_unavailable?
|
|
38
|
+
::I18n.enforce_available_locales && !::I18n.available_locales.include?(FALLBACK_LOCALE)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|