sinatra-filtering_parameters 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +10 -0
- data/lib/sinatra/filtering_parameters.rb +5 -5
- data/lib/sinatra/filtering_parameters/version.rb +1 -1
- data/spec/nested_parameters_spec.rb +131 -163
- data/spec/permitted_parameters_spec.rb +66 -48
- data/spec/spec_helper.rb +8 -3
- metadata +3 -2
data/Gemfile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
gemspec
|
3
|
+
|
4
|
+
gem 'sinatra', :git => 'git://github.com/sinatra/sinatra'
|
5
|
+
gem 'sinatra-contrib', :git => 'git://github.com/sinatra/sinatra-contrib.git'
|
6
|
+
|
7
|
+
group :development do
|
8
|
+
gem 'guard-rspec'
|
9
|
+
gem 'rb-fsevent', '~> 0.9.1'
|
10
|
+
end
|
@@ -6,13 +6,13 @@ module Sinatra
|
|
6
6
|
def registered(app)
|
7
7
|
app.set(:allow) do |*filters|
|
8
8
|
condition do
|
9
|
-
|
10
|
-
params.clear
|
9
|
+
original = @params.dup
|
10
|
+
@params.clear
|
11
11
|
%w[ splat captures ].each do |name|
|
12
|
-
params[name] =
|
12
|
+
@params[name] = original.delete(name) if original.include?(name)
|
13
13
|
end
|
14
|
-
|
15
|
-
params.merge!
|
14
|
+
allow_params = Sinatra::FilteringParameters.allow(original, filters)
|
15
|
+
@params.merge! indifferent_params(allow_params)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -2,202 +2,170 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Sinatra::FilteringParameters do
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
post('/', allow: args[:allow]){ params.to_json }
|
9
|
-
end
|
10
|
-
post '/', args[:pass_params]
|
5
|
+
def expect_with(parameters, keys, &block)
|
6
|
+
mock_route '/', :allow => keys, &block
|
7
|
+
post '/', parameters
|
11
8
|
end
|
12
9
|
|
13
10
|
it "permitted nested parameters" do
|
14
|
-
|
15
|
-
:
|
16
|
-
:
|
17
|
-
|
18
|
-
:
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
:name => "Christopher Marlowe"
|
23
|
-
}],
|
24
|
-
:details => {
|
25
|
-
:pages => 200,
|
26
|
-
:genre => "Tragedy"
|
27
|
-
}
|
28
|
-
},
|
29
|
-
:magazine => "Mjallo!"
|
30
|
-
},
|
31
|
-
:allow => [
|
32
|
-
:book => [
|
33
|
-
:title,
|
34
|
-
:authors => :name,
|
35
|
-
:details => :pages
|
36
|
-
]
|
37
|
-
]
|
38
|
-
)
|
39
|
-
result_should_be_equal({
|
40
|
-
"book" => {
|
41
|
-
"title" => "Romeo and Juliet",
|
42
|
-
"authors" => [{
|
43
|
-
"name" => "William Shakespeare"
|
44
|
-
},{
|
45
|
-
"name" => "Christopher Marlowe"
|
11
|
+
parameters = {
|
12
|
+
:book => {
|
13
|
+
:title => "Romeo and Juliet",
|
14
|
+
:authors => [{
|
15
|
+
:name => "William Shakespeare",
|
16
|
+
:born => "1564-04-26"
|
17
|
+
}, {
|
18
|
+
:name => "Christopher Marlowe"
|
46
19
|
}],
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
}
|
51
|
-
})
|
52
|
-
end
|
53
|
-
it "nested arrays with strings" do
|
54
|
-
post_with_filter(
|
55
|
-
:pass_params => {
|
56
|
-
:book => {
|
57
|
-
:genres => ["Tragedy"]
|
20
|
+
:details => {
|
21
|
+
:pages => 200,
|
22
|
+
:genre => "Tragedy"
|
58
23
|
}
|
59
24
|
},
|
60
|
-
:
|
61
|
-
|
25
|
+
:magazine => "Mjallo!"
|
26
|
+
}
|
27
|
+
keys = {
|
28
|
+
:book => [
|
29
|
+
:title,
|
30
|
+
:authors => :name,
|
31
|
+
:details => :pages
|
62
32
|
]
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
33
|
+
}
|
34
|
+
expect_with parameters, keys do
|
35
|
+
params[:book][:title].should == 'Romeo and Juliet'
|
36
|
+
params[:book][:authors][0][:name].should == 'William Shakespeare'
|
37
|
+
params[:book][:authors][1][:name].should == 'Christopher Marlowe'
|
38
|
+
params[:book][:details][:pages].should == '200'
|
39
|
+
params[:book][:details][:genre].should be_nil
|
40
|
+
params[:book][:authors][1][:born].should be_nil
|
41
|
+
params[:magazine].should be_nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "nested arrays with strings" do
|
46
|
+
parameters = {
|
47
|
+
:book => {
|
48
|
+
:genres => ["Tragedy"]
|
67
49
|
}
|
68
|
-
}
|
50
|
+
}
|
51
|
+
keys = {
|
52
|
+
:book => :genres
|
53
|
+
}
|
54
|
+
expect_with parameters, keys do
|
55
|
+
params[:book][:genres].should == ['Tragedy']
|
56
|
+
end
|
69
57
|
end
|
70
58
|
|
71
59
|
it "permit may specify symbols or strings" do
|
72
|
-
|
73
|
-
:
|
74
|
-
:
|
75
|
-
|
76
|
-
:author => "William Shakespeare"
|
77
|
-
},
|
78
|
-
:magazine => "Shakespeare Today"
|
60
|
+
parameters = {
|
61
|
+
:book => {
|
62
|
+
:title => "Romeo and Juliet",
|
63
|
+
:author => "William Shakespeare"
|
79
64
|
},
|
80
|
-
:
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
]
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
})
|
65
|
+
:magazine => "Shakespeare Today"
|
66
|
+
}
|
67
|
+
keys = [{
|
68
|
+
:book => [
|
69
|
+
"title",
|
70
|
+
:author
|
71
|
+
]},
|
72
|
+
"magazine"
|
73
|
+
]
|
74
|
+
expect_with parameters, keys do
|
75
|
+
params[:book][:title].should == 'Romeo and Juliet'
|
76
|
+
params[:book][:author].should == 'William Shakespeare'
|
77
|
+
params[:magazine].should == 'Shakespeare Today'
|
78
|
+
end
|
95
79
|
end
|
96
80
|
|
97
81
|
it "nested array with strings that should be hashes" do
|
98
|
-
|
99
|
-
:
|
100
|
-
:
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
:
|
105
|
-
:
|
106
|
-
:genres => :type
|
107
|
-
]
|
82
|
+
parameters = {
|
83
|
+
:book => {
|
84
|
+
:genres => ["Tragedy"]
|
85
|
+
}
|
86
|
+
}
|
87
|
+
keys = {
|
88
|
+
:book => [
|
89
|
+
:genres => :type
|
108
90
|
]
|
109
|
-
|
110
|
-
|
91
|
+
}
|
92
|
+
expect_with parameters, keys do
|
93
|
+
params.should be_empty
|
94
|
+
end
|
111
95
|
end
|
112
96
|
|
113
97
|
it "nested array with strings that should be hashes and additional values" do
|
114
|
-
|
115
|
-
:
|
116
|
-
:
|
117
|
-
|
118
|
-
:genres => ["Tragedy"]
|
119
|
-
}
|
120
|
-
},
|
121
|
-
:allow => [
|
122
|
-
:book => [
|
123
|
-
:title,
|
124
|
-
:genres => :type
|
125
|
-
]
|
126
|
-
]
|
127
|
-
)
|
128
|
-
result_should_be_equal({
|
129
|
-
"book" => {
|
130
|
-
"title" => "Romeo and Juliet"
|
98
|
+
parameters = {
|
99
|
+
:book => {
|
100
|
+
:title => "Romeo and Juliet",
|
101
|
+
:genres => ["Tragedy"]
|
131
102
|
}
|
132
|
-
}
|
103
|
+
}
|
104
|
+
keys = [
|
105
|
+
:book => [
|
106
|
+
:title,
|
107
|
+
:genres => :type
|
108
|
+
]
|
109
|
+
]
|
110
|
+
expect_with parameters, keys do
|
111
|
+
params[:book][:title].should == 'Romeo and Juliet'
|
112
|
+
end
|
133
113
|
end
|
134
114
|
|
135
115
|
it "nested string that should be a hash" do
|
136
|
-
|
137
|
-
:
|
138
|
-
:
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
:
|
143
|
-
:
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
116
|
+
parameters = {
|
117
|
+
:book => {
|
118
|
+
:genre => "Tragedy"
|
119
|
+
}
|
120
|
+
}
|
121
|
+
keys = [
|
122
|
+
:book => [
|
123
|
+
:genres => :type
|
124
|
+
]
|
125
|
+
]
|
126
|
+
expect_with parameters, keys do
|
127
|
+
params.should be_empty
|
128
|
+
end
|
149
129
|
end
|
150
130
|
|
151
131
|
it "fields_for_style_nested_params" do
|
152
|
-
|
153
|
-
:
|
154
|
-
:
|
155
|
-
:
|
156
|
-
|
157
|
-
:'1' => { :name => 'Unattributed Assistant' }
|
158
|
-
}
|
159
|
-
}
|
160
|
-
},
|
161
|
-
:allow => [
|
162
|
-
:book => [
|
163
|
-
:authors_attributes => :name
|
164
|
-
]
|
165
|
-
]
|
166
|
-
)
|
167
|
-
result_should_be_equal({
|
168
|
-
"book" => {
|
169
|
-
"authors_attributes" => {
|
170
|
-
"0" => { "name" => "William Shakespeare" },
|
171
|
-
"1" => { "name" => "Unattributed Assistant" }
|
132
|
+
parameters = {
|
133
|
+
:book => {
|
134
|
+
:authors_attributes => {
|
135
|
+
:'0' => { :name => 'William Shakespeare', :age_of_death => '52' },
|
136
|
+
:'1' => { :name => 'Unattributed Assistant' }
|
172
137
|
}
|
173
138
|
}
|
174
|
-
}
|
139
|
+
}
|
140
|
+
keys = [
|
141
|
+
:book => [
|
142
|
+
:authors_attributes => :name
|
143
|
+
]
|
144
|
+
]
|
145
|
+
expect_with parameters, keys do
|
146
|
+
params[:book][:authors_attributes]['0'][:name].should == 'William Shakespeare'
|
147
|
+
params[:book][:authors_attributes]['1'][:name].should == 'Unattributed Assistant'
|
148
|
+
end
|
175
149
|
end
|
176
150
|
|
177
151
|
it "fields_for_style_nested_params with negative numbers" do
|
178
|
-
|
179
|
-
:
|
180
|
-
:
|
181
|
-
:
|
182
|
-
|
183
|
-
:'-2' => { :name => 'Unattributed Assistant' }
|
184
|
-
}
|
185
|
-
}
|
186
|
-
},
|
187
|
-
:allow => [
|
188
|
-
:book => [
|
189
|
-
:authors_attributes => :name
|
190
|
-
]
|
191
|
-
]
|
192
|
-
)
|
193
|
-
result_should_be_equal({
|
194
|
-
"book" => {
|
195
|
-
"authors_attributes" => {
|
196
|
-
"-1" => { "name" => "William Shakespeare" },
|
197
|
-
"-2" => { "name" => "Unattributed Assistant" }
|
152
|
+
parameters = {
|
153
|
+
:book => {
|
154
|
+
:authors_attributes => {
|
155
|
+
:'-1' => { :name => 'William Shakespeare', :age_of_death => '52' },
|
156
|
+
:'-2' => { :name => 'Unattributed Assistant' }
|
198
157
|
}
|
199
158
|
}
|
200
|
-
}
|
159
|
+
}
|
160
|
+
keys = [
|
161
|
+
:book => [
|
162
|
+
:authors_attributes => :name
|
163
|
+
]
|
164
|
+
]
|
165
|
+
expect_with parameters, keys do
|
166
|
+
params[:book][:authors_attributes]['-1'][:name].should == 'William Shakespeare'
|
167
|
+
params[:book][:authors_attributes]['-2'][:name].should == 'Unattributed Assistant'
|
168
|
+
end
|
201
169
|
end
|
202
170
|
|
203
171
|
end
|
@@ -2,65 +2,83 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Sinatra::FilteringParameters do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
post('/sample/:name'){ params.to_json }
|
10
|
-
else
|
11
|
-
post('/sample/:name', allow: args[:allow]){ params.to_json }
|
12
|
-
end
|
5
|
+
describe 'Enable string or symbol key access to the nested params hash' do
|
6
|
+
def expect_with(parameters, keys, &block)
|
7
|
+
mock_route '/', &block
|
8
|
+
post '/', parameters
|
13
9
|
end
|
14
|
-
post '/sample/foo', args[:pass_params]
|
15
|
-
end
|
16
10
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
11
|
+
it 'with filter of hash' do
|
12
|
+
parameters = {
|
13
|
+
:book => {
|
14
|
+
:title => "Romeo and Juliet"
|
15
|
+
}
|
16
|
+
}
|
17
|
+
expect_with parameters, :allow => { :book => :title } do
|
18
|
+
params['book']['title'].should == 'Romeo and Juliet'
|
19
|
+
params[:book]['title'].should == 'Romeo and Juliet'
|
20
|
+
params[:book][:title].should == 'Romeo and Juliet'
|
21
|
+
params['book'][:title].should == 'Romeo and Juliet'
|
22
|
+
end
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
describe '
|
27
|
-
it '
|
28
|
-
|
29
|
-
:
|
30
|
-
|
31
|
-
|
32
|
-
result_should_be_equal({ "splat"=>[], "captures"=>["foo"], "name"=>"foo" })
|
26
|
+
describe 'support matching parameters' do
|
27
|
+
it 'for splat' do
|
28
|
+
mock_route '/say/*/to/*', :allow => :hoo do
|
29
|
+
params[:splat].should == ['hello','world']
|
30
|
+
end
|
31
|
+
get '/say/hello/to/world'
|
33
32
|
end
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
:
|
38
|
-
|
39
|
-
|
33
|
+
|
34
|
+
it 'for captures' do
|
35
|
+
mock_route %r{/hello/([\w]+)}, :allow => :hoo do
|
36
|
+
params[:captures].should == ['world']
|
37
|
+
end
|
38
|
+
get '/hello/world'
|
40
39
|
end
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'do not specify ilter' do
|
43
|
+
it "does not remove anything from the parameter" do
|
44
|
+
mock_route '/pages/:id' do
|
45
|
+
params[:id].should == '1'
|
46
|
+
params[:foo].should == 'foo'
|
47
|
+
params[:bar].should == 'bar'
|
48
|
+
params[:baz].should == 'baz'
|
49
|
+
params[:captures].should == ['1']
|
50
|
+
params[:splat].should be_empty
|
51
|
+
end
|
52
|
+
post '/pages/1', :foo => 'foo', :bar => 'bar', :baz => 'baz'
|
47
53
|
end
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
)
|
54
|
-
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'will be only parameters that allowed in' do
|
57
|
+
def should_with(filter={})
|
58
|
+
mock_route '/pages', filter do
|
59
|
+
params.delete('foo').should == 'foo'
|
60
|
+
params.each { |param| param.should be_nil }
|
55
61
|
end
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
+
post '/pages', :foo => 'foo', :bar => 'bar', :baz => 'baz'
|
63
|
+
end
|
64
|
+
|
65
|
+
it('symbol'){ should_with :allow => :foo }
|
66
|
+
it("string"){ should_with :allow => 'foo' }
|
67
|
+
it("array") { should_with :allow => [:foo] }
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'parameter will be empty when specify in' do
|
71
|
+
def should_with(filter={})
|
72
|
+
mock_route '/pages', filter do
|
73
|
+
params.each { |param| param.should be_nil }
|
62
74
|
end
|
75
|
+
post '/pages', :foo => 'foo', :bar => 'bar', :baz => 'baz'
|
63
76
|
end
|
77
|
+
|
78
|
+
it("blank string"){ should_with :allow => '' }
|
79
|
+
it("empty array") { should_with :allow => [] }
|
80
|
+
it("empty hash") { should_with :allow => {} }
|
81
|
+
it("nil") { should_with :allow => nil }
|
64
82
|
end
|
65
83
|
|
66
84
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -5,14 +5,19 @@ require 'rack/test'
|
|
5
5
|
require 'sinatra'
|
6
6
|
require 'sinatra/contrib'
|
7
7
|
require 'sinatra/filtering_parameters'
|
8
|
-
require 'json'
|
9
8
|
|
10
9
|
RSpec.configure do |config|
|
11
10
|
config.include Rack::Test::Methods
|
12
11
|
config.include Sinatra::TestHelpers
|
13
12
|
end
|
14
13
|
|
15
|
-
def
|
16
|
-
|
14
|
+
def mock_route(path, opts={}, &block)
|
15
|
+
mock_app do
|
16
|
+
include RSpec::Matchers
|
17
|
+
register Sinatra::FilteringParameters
|
18
|
+
%w(get post put delete head options patch).each do |verb|
|
19
|
+
send(verb.to_sym, path, opts, &block)
|
20
|
+
end
|
21
|
+
end
|
17
22
|
end
|
18
23
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-filtering_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
@@ -133,6 +133,7 @@ files:
|
|
133
133
|
- .document
|
134
134
|
- .gitignore
|
135
135
|
- .rspec
|
136
|
+
- Gemfile
|
136
137
|
- LICENSE.txt
|
137
138
|
- README.md
|
138
139
|
- Rakefile
|