grape 0.9.0 → 0.10.0
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.
Potentially problematic release.
This version of grape might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -66
- data/.rubocop_todo.yml +78 -17
- data/.travis.yml +7 -3
- data/Appraisals +7 -0
- data/CHANGELOG.md +24 -0
- data/CONTRIBUTING.md +7 -0
- data/Gemfile +1 -7
- data/Guardfile +1 -1
- data/README.md +560 -94
- data/RELEASING.md +1 -1
- data/Rakefile +10 -11
- data/UPGRADING.md +211 -3
- data/gemfiles/rails_3.gemfile +14 -0
- data/gemfiles/rails_4.gemfile +14 -0
- data/grape.gemspec +10 -9
- data/lib/backports/active_support/deep_dup.rb +49 -0
- data/lib/backports/active_support/duplicable.rb +88 -0
- data/lib/grape.rb +29 -2
- data/lib/grape/api.rb +59 -65
- data/lib/grape/dsl/api.rb +19 -0
- data/lib/grape/dsl/callbacks.rb +6 -4
- data/lib/grape/dsl/configuration.rb +49 -5
- data/lib/grape/dsl/helpers.rb +7 -8
- data/lib/grape/dsl/inside_route.rb +22 -10
- data/lib/grape/dsl/middleware.rb +5 -5
- data/lib/grape/dsl/parameters.rb +6 -2
- data/lib/grape/dsl/request_response.rb +23 -20
- data/lib/grape/dsl/routing.rb +52 -49
- data/lib/grape/dsl/settings.rb +110 -0
- data/lib/grape/dsl/validations.rb +14 -6
- data/lib/grape/endpoint.rb +104 -88
- data/lib/grape/exceptions/base.rb +2 -2
- data/lib/grape/exceptions/incompatible_option_values.rb +1 -1
- data/lib/grape/exceptions/invalid_formatter.rb +1 -1
- data/lib/grape/exceptions/invalid_versioner_option.rb +1 -1
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +1 -1
- data/lib/grape/exceptions/missing_mime_type.rb +1 -1
- data/lib/grape/exceptions/missing_option.rb +1 -1
- data/lib/grape/exceptions/missing_vendor_option.rb +1 -1
- data/lib/grape/exceptions/unknown_options.rb +1 -1
- data/lib/grape/exceptions/unknown_validator.rb +1 -1
- data/lib/grape/exceptions/validation.rb +1 -1
- data/lib/grape/exceptions/validation_errors.rb +2 -2
- data/lib/grape/formatter/serializable_hash.rb +1 -1
- data/lib/grape/formatter/xml.rb +1 -1
- data/lib/grape/locale/en.yml +2 -0
- data/lib/grape/middleware/auth/dsl.rb +26 -21
- data/lib/grape/middleware/auth/strategies.rb +1 -1
- data/lib/grape/middleware/auth/strategy_info.rb +0 -2
- data/lib/grape/middleware/base.rb +2 -2
- data/lib/grape/middleware/error.rb +1 -1
- data/lib/grape/middleware/formatter.rb +5 -5
- data/lib/grape/middleware/versioner.rb +1 -1
- data/lib/grape/middleware/versioner/header.rb +3 -3
- data/lib/grape/middleware/versioner/param.rb +2 -2
- data/lib/grape/middleware/versioner/path.rb +1 -1
- data/lib/grape/namespace.rb +1 -1
- data/lib/grape/path.rb +9 -3
- data/lib/grape/util/content_types.rb +16 -8
- data/lib/grape/util/inheritable_setting.rb +74 -0
- data/lib/grape/util/inheritable_values.rb +51 -0
- data/lib/grape/util/stackable_values.rb +52 -0
- data/lib/grape/util/strict_hash_configuration.rb +106 -0
- data/lib/grape/validations.rb +0 -220
- data/lib/grape/validations/attributes_iterator.rb +21 -0
- data/lib/grape/validations/params_scope.rb +176 -0
- data/lib/grape/validations/validators/all_or_none.rb +20 -0
- data/lib/grape/validations/validators/allow_blank.rb +30 -0
- data/lib/grape/validations/validators/at_least_one_of.rb +20 -0
- data/lib/grape/validations/validators/base.rb +37 -0
- data/lib/grape/validations/{coerce.rb → validators/coerce.rb} +3 -3
- data/lib/grape/validations/{default.rb → validators/default.rb} +1 -1
- data/lib/grape/validations/validators/exactly_one_of.rb +20 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +26 -0
- data/lib/grape/validations/validators/mutual_exclusion.rb +25 -0
- data/lib/grape/validations/{presence.rb → validators/presence.rb} +2 -2
- data/lib/grape/validations/validators/regexp.rb +12 -0
- data/lib/grape/validations/validators/values.rb +26 -0
- data/lib/grape/version.rb +1 -1
- data/spec/grape/api_spec.rb +522 -343
- data/spec/grape/dsl/callbacks_spec.rb +4 -4
- data/spec/grape/dsl/configuration_spec.rb +48 -9
- data/spec/grape/dsl/helpers_spec.rb +6 -13
- data/spec/grape/dsl/inside_route_spec.rb +43 -4
- data/spec/grape/dsl/middleware_spec.rb +1 -10
- data/spec/grape/dsl/parameters_spec.rb +8 -1
- data/spec/grape/dsl/request_response_spec.rb +16 -22
- data/spec/grape/dsl/routing_spec.rb +21 -5
- data/spec/grape/dsl/settings_spec.rb +219 -0
- data/spec/grape/dsl/validations_spec.rb +8 -11
- data/spec/grape/endpoint_spec.rb +115 -86
- data/spec/grape/entity_spec.rb +33 -33
- data/spec/grape/exceptions/invalid_formatter_spec.rb +3 -5
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +4 -6
- data/spec/grape/exceptions/missing_mime_type_spec.rb +5 -6
- data/spec/grape/exceptions/missing_option_spec.rb +3 -5
- data/spec/grape/exceptions/unknown_options_spec.rb +3 -5
- data/spec/grape/exceptions/unknown_validator_spec.rb +3 -5
- data/spec/grape/exceptions/validation_errors_spec.rb +5 -5
- data/spec/grape/loading_spec.rb +44 -0
- data/spec/grape/middleware/auth/base_spec.rb +0 -4
- data/spec/grape/middleware/auth/dsl_spec.rb +2 -4
- data/spec/grape/middleware/auth/strategies_spec.rb +5 -6
- data/spec/grape/middleware/exception_spec.rb +8 -10
- data/spec/grape/middleware/formatter_spec.rb +13 -15
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +10 -10
- data/spec/grape/middleware/versioner/header_spec.rb +25 -25
- data/spec/grape/middleware/versioner/param_spec.rb +15 -17
- data/spec/grape/middleware/versioner/path_spec.rb +1 -2
- data/spec/grape/middleware/versioner_spec.rb +0 -1
- data/spec/grape/path_spec.rb +66 -45
- data/spec/grape/util/inheritable_setting_spec.rb +217 -0
- data/spec/grape/util/inheritable_values_spec.rb +63 -0
- data/spec/grape/util/stackable_values_spec.rb +115 -0
- data/spec/grape/util/strict_hash_configuration_spec.rb +38 -0
- data/spec/grape/validations/attributes_iterator_spec.rb +4 -0
- data/spec/grape/validations/params_scope_spec.rb +57 -0
- data/spec/grape/validations/validators/all_or_none_spec.rb +60 -0
- data/spec/grape/validations/validators/allow_blank_spec.rb +170 -0
- data/spec/grape/validations/{at_least_one_of_spec.rb → validators/at_least_one_of_spec.rb} +7 -3
- data/spec/grape/validations/{coerce_spec.rb → validators/coerce_spec.rb} +8 -11
- data/spec/grape/validations/{default_spec.rb → validators/default_spec.rb} +7 -9
- data/spec/grape/validations/{exactly_one_of_spec.rb → validators/exactly_one_of_spec.rb} +15 -11
- data/spec/grape/validations/{mutual_exclusion_spec.rb → validators/mutual_exclusion_spec.rb} +11 -9
- data/spec/grape/validations/{presence_spec.rb → validators/presence_spec.rb} +30 -30
- data/spec/grape/validations/{regexp_spec.rb → validators/regexp_spec.rb} +2 -4
- data/spec/grape/validations/{values_spec.rb → validators/values_spec.rb} +95 -23
- data/spec/grape/validations/{zh-CN.yml → validators/zh-CN.yml} +0 -0
- data/spec/grape/validations_spec.rb +335 -70
- data/spec/shared/versioning_examples.rb +7 -8
- data/spec/spec_helper.rb +2 -0
- data/spec/support/basic_auth_encode_helpers.rb +1 -1
- data/spec/support/content_type_helpers.rb +1 -1
- data/spec/support/versioned_helpers.rb +3 -3
- metadata +80 -33
- data/lib/grape/util/deep_merge.rb +0 -23
- data/lib/grape/util/hash_stack.rb +0 -120
- data/lib/grape/validations/at_least_one_of.rb +0 -25
- data/lib/grape/validations/exactly_one_of.rb +0 -26
- data/lib/grape/validations/mutual_exclusion.rb +0 -25
- data/lib/grape/validations/regexp.rb +0 -12
- data/lib/grape/validations/values.rb +0 -23
- data/spec/grape/util/hash_stack_spec.rb +0 -132
@@ -1,25 +0,0 @@
|
|
1
|
-
module Grape
|
2
|
-
module Validations
|
3
|
-
class AtLeastOneOfValidator < Validator
|
4
|
-
attr_reader :params
|
5
|
-
|
6
|
-
def validate!(params)
|
7
|
-
@params = params
|
8
|
-
if no_exclusive_params_are_present
|
9
|
-
raise Grape::Exceptions::Validation, params: attrs.map(&:to_s), message_key: :at_least_one
|
10
|
-
end
|
11
|
-
params
|
12
|
-
end
|
13
|
-
|
14
|
-
private
|
15
|
-
|
16
|
-
def no_exclusive_params_are_present
|
17
|
-
keys_in_common.length == 0
|
18
|
-
end
|
19
|
-
|
20
|
-
def keys_in_common
|
21
|
-
(attrs.map(&:to_s) & params.stringify_keys.keys).map(&:to_s)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module Grape
|
2
|
-
module Validations
|
3
|
-
require 'grape/validations/mutual_exclusion'
|
4
|
-
class ExactlyOneOfValidator < MutualExclusionValidator
|
5
|
-
attr_reader :params
|
6
|
-
|
7
|
-
def validate!(params)
|
8
|
-
super
|
9
|
-
if none_of_restricted_params_is_present
|
10
|
-
raise Grape::Exceptions::Validation, params: all_keys, message_key: :exactly_one
|
11
|
-
end
|
12
|
-
params
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def none_of_restricted_params_is_present
|
18
|
-
keys_in_common.length < 1
|
19
|
-
end
|
20
|
-
|
21
|
-
def all_keys
|
22
|
-
attrs.map(&:to_s)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module Grape
|
2
|
-
module Validations
|
3
|
-
class MutualExclusionValidator < Validator
|
4
|
-
attr_reader :params
|
5
|
-
|
6
|
-
def validate!(params)
|
7
|
-
@params = params
|
8
|
-
if two_or_more_exclusive_params_are_present
|
9
|
-
raise Grape::Exceptions::Validation, params: keys_in_common, message_key: :mutual_exclusion
|
10
|
-
end
|
11
|
-
params
|
12
|
-
end
|
13
|
-
|
14
|
-
private
|
15
|
-
|
16
|
-
def two_or_more_exclusive_params_are_present
|
17
|
-
keys_in_common.length > 1
|
18
|
-
end
|
19
|
-
|
20
|
-
def keys_in_common
|
21
|
-
(attrs.map(&:to_s) & params.stringify_keys.keys).map(&:to_s)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
module Grape
|
2
|
-
module Validations
|
3
|
-
class RegexpValidator < SingleOptionValidator
|
4
|
-
def validate_param!(attr_name, params)
|
5
|
-
if params.key?(attr_name) &&
|
6
|
-
(params[attr_name].nil? || !(params[attr_name].to_s =~ @option))
|
7
|
-
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :regexp
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module Grape
|
2
|
-
module Validations
|
3
|
-
class ValuesValidator < Validator
|
4
|
-
def initialize(attrs, options, required, scope)
|
5
|
-
@values = options
|
6
|
-
@required = required
|
7
|
-
super
|
8
|
-
end
|
9
|
-
|
10
|
-
def validate_param!(attr_name, params)
|
11
|
-
if (params[attr_name] || required_for_root_scope?) && !(@values.is_a?(Proc) ? @values.call : @values).include?(params[attr_name])
|
12
|
-
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :values
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
private
|
17
|
-
|
18
|
-
def required_for_root_scope?
|
19
|
-
@required && @scope.root?
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Grape::Util::HashStack do
|
4
|
-
|
5
|
-
describe '#get' do
|
6
|
-
it 'finds the first available key' do
|
7
|
-
subject[:abc] = 123
|
8
|
-
subject.push(abc: 345)
|
9
|
-
expect(subject.get(:abc)).to eq(345)
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'is nil if the key has not been set' do
|
13
|
-
expect(subject[:abc]).to be_nil
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe '#set' do
|
18
|
-
it 'sets a value on the highest frame' do
|
19
|
-
subject.push
|
20
|
-
subject.set(:abc, 123)
|
21
|
-
expect(subject.stack.last[:abc]).to eq(123)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '#imbue' do
|
26
|
-
it 'pushes a new value onto the end of an array' do
|
27
|
-
subject[:abc] = []
|
28
|
-
subject.imbue :abc, [123]
|
29
|
-
subject.imbue :abc, [456]
|
30
|
-
expect(subject[:abc]).to eq([123, 456])
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'merges a hash that is passed' do
|
34
|
-
subject[:abc] = { foo: 'bar' }
|
35
|
-
subject.imbue :abc, baz: 'wich'
|
36
|
-
expect(subject[:abc]).to eq(foo: 'bar', baz: 'wich')
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'sets the value if not a hash or array' do
|
40
|
-
subject.imbue :abc, 123
|
41
|
-
expect(subject[:abc]).to eq(123)
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'is able to imbue an array without explicit setting' do
|
45
|
-
subject.imbue :arr, [1]
|
46
|
-
subject.imbue :arr, [2]
|
47
|
-
expect(subject[:arr]).to eq([1, 2])
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'is able to imbue a hash without explicit setting' do
|
51
|
-
subject.imbue :hash, foo: 'bar'
|
52
|
-
subject.imbue :hash, baz: 'wich'
|
53
|
-
expect(subject[:hash]).to eq(foo: 'bar', baz: 'wich')
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
describe '#push' do
|
58
|
-
it 'returns a HashStack' do
|
59
|
-
expect(subject.push(Grape::Util::HashStack.new)).to be_kind_of(Grape::Util::HashStack)
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'places the passed value on the top of the stack' do
|
63
|
-
subject.push(abc: 123)
|
64
|
-
expect(subject.stack).to eq([{}, { abc: 123 }])
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'pushes an empty hash by default' do
|
68
|
-
subject[:abc] = 123
|
69
|
-
subject.push
|
70
|
-
expect(subject.stack).to eq([{ abc: 123 }, {}])
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe '#pop' do
|
75
|
-
it 'removes and return the top frame' do
|
76
|
-
subject.push(abc: 123)
|
77
|
-
expect(subject.pop).to eq(abc: 123)
|
78
|
-
expect(subject.stack.size).to eq(1)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe '#peek' do
|
83
|
-
it 'returns the top frame without removing it' do
|
84
|
-
subject.push(abc: 123)
|
85
|
-
expect(subject.peek).to eq(abc: 123)
|
86
|
-
expect(subject.stack.size).to eq(2)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe '#prepend' do
|
91
|
-
it 'returns a HashStack' do
|
92
|
-
expect(subject.prepend(Grape::Util::HashStack.new)).to be_kind_of(Grape::Util::HashStack)
|
93
|
-
end
|
94
|
-
|
95
|
-
it "prepends a HashStack's stack onto its own stack" do
|
96
|
-
other = Grape::Util::HashStack.new.push(abc: 123)
|
97
|
-
expect(subject.prepend(other).stack).to eq([{}, { abc: 123 }, {}])
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
describe '#concat' do
|
102
|
-
it 'returns a HashStack' do
|
103
|
-
expect(subject.concat(Grape::Util::HashStack.new)).to be_kind_of(Grape::Util::HashStack)
|
104
|
-
end
|
105
|
-
|
106
|
-
it "appends a HashStack's stack onto its own stack" do
|
107
|
-
other = Grape::Util::HashStack.new.push(abc: 123)
|
108
|
-
expect(subject.concat(other).stack).to eq([{}, {}, { abc: 123 }])
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
describe '#update' do
|
113
|
-
it 'merges! into the top frame' do
|
114
|
-
subject.update(abc: 123)
|
115
|
-
expect(subject.stack).to eq([{ abc: 123 }])
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'returns a HashStack' do
|
119
|
-
expect(subject.update(abc: 123)).to be_kind_of(Grape::Util::HashStack)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
describe '#clone' do
|
124
|
-
it 'performs a deep copy' do
|
125
|
-
subject[:abc] = 123
|
126
|
-
subject.push def: 234
|
127
|
-
clone = subject.clone
|
128
|
-
clone[:def] = 345
|
129
|
-
expect(subject[:def]).to eq(234)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|