sinatra-filtering_parameters 0.1.0 → 0.1.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.
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
1
  html/
2
2
  pkg/
3
+ tmp/
3
4
  Guardfile
5
+ Gemfile.lock
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
- _params = params.dup
10
- params.clear
9
+ original = @params.dup
10
+ @params.clear
11
11
  %w[ splat captures ].each do |name|
12
- params[name] = _params.delete(name) if _params.include?(name)
12
+ @params[name] = original.delete(name) if original.include?(name)
13
13
  end
14
- hoge = Sinatra::FilteringParameters.allow(_params, filters)
15
- params.merge! hoge
14
+ allow_params = Sinatra::FilteringParameters.allow(original, filters)
15
+ @params.merge! indifferent_params(allow_params)
16
16
  end
17
17
  end
18
18
  end
@@ -1,6 +1,6 @@
1
1
  module Sinatra
2
2
  module FilteringParameters
3
3
  # sinatra-filtering_parameters version
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
6
6
  end
@@ -2,202 +2,170 @@ require 'spec_helper'
2
2
 
3
3
  describe Sinatra::FilteringParameters do
4
4
 
5
- def post_with_filter(args)
6
- mock_app do
7
- register Sinatra::FilteringParameters
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
- post_with_filter(
15
- :pass_params => {
16
- :book => {
17
- :title => "Romeo and Juliet",
18
- :authors => [{
19
- :name => "William Shakespeare",
20
- :born => "1564-04-26"
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
- "details" => {
48
- "pages" => "200"
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
- :allow => [
61
- :book => :genres
25
+ :magazine => "Mjallo!"
26
+ }
27
+ keys = {
28
+ :book => [
29
+ :title,
30
+ :authors => :name,
31
+ :details => :pages
62
32
  ]
63
- )
64
- result_should_be_equal({
65
- "book" => {
66
- "genres" => ["Tragedy"]
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
- post_with_filter(
73
- :pass_params => {
74
- :book => {
75
- :title => "Romeo and Juliet",
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
- :allow => [{
81
- :book => [
82
- "title",
83
- :author
84
- ]},
85
- "magazine"
86
- ]
87
- )
88
- result_should_be_equal({
89
- "book" => {
90
- "title" => "Romeo and Juliet",
91
- "author" => "William Shakespeare",
92
- },
93
- "magazine" => "Shakespeare Today"
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
- post_with_filter(
99
- :pass_params => {
100
- :book => {
101
- :genres => ["Tragedy"]
102
- }
103
- },
104
- :allow => [
105
- :book => [
106
- :genres => :type
107
- ]
82
+ parameters = {
83
+ :book => {
84
+ :genres => ["Tragedy"]
85
+ }
86
+ }
87
+ keys = {
88
+ :book => [
89
+ :genres => :type
108
90
  ]
109
- )
110
- result_should_be_equal({})
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
- post_with_filter(
115
- :pass_params => {
116
- :book => {
117
- :title => "Romeo and Juliet",
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
- post_with_filter(
137
- :pass_params => {
138
- :book => {
139
- :genre => "Tragedy"
140
- }
141
- },
142
- :allow => [
143
- :book => [
144
- :genres => :type
145
- ]
146
- ]
147
- )
148
- result_should_be_equal({})
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
- post_with_filter(
153
- :pass_params => {
154
- :book => {
155
- :authors_attributes => {
156
- :'0' => { :name => 'William Shakespeare', :age_of_death => '52' },
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
- post_with_filter(
179
- :pass_params => {
180
- :book => {
181
- :authors_attributes => {
182
- :'-1' => { :name => 'William Shakespeare', :age_of_death => '52' },
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
- def post_with_filter(args)
6
- mock_app do
7
- register Sinatra::FilteringParameters
8
- if args[:allow].nil?
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
- describe 'permitted parameters nothing' do
18
- it "when success" do
19
- post_with_filter(
20
- :pass_params => { :a => 1, :b => 2, :c => 3 }
21
- )
22
- result_should_be_equal({ "a"=>"1", "b"=>"2", "c"=>"3", "splat"=>[], "captures"=>["foo"], "name"=>"foo" })
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 'permitted parameters specified in' do
27
- it 'symbol' do
28
- post_with_filter(
29
- :pass_params => { :a => 1, :b => 2, :c => 3 },
30
- :allow => :name
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
- it "string" do
35
- post_with_filter(
36
- :pass_params => { :a => 1, :b => 2, :c => 3 },
37
- :allow => 'name'
38
- )
39
- result_should_be_equal({ "splat"=>[], "captures"=>["foo"], "name"=>"foo" })
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
- it "array" do
42
- post_with_filter(
43
- :pass_params => { :a => 1, :b => 2, :c => 3 },
44
- :allow => [:name, :a]
45
- )
46
- result_should_be_equal({ "splat"=>[], "captures"=>["foo"], "name"=>"foo", "a"=>"1" })
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
- context "empty" do
49
- it "string" do
50
- post_with_filter(
51
- :pass_params => { :a => 1, :b => 2, :c => 3 },
52
- :allow => ''
53
- )
54
- result_should_be_equal({ "splat"=>[], "captures"=>["foo"] })
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
- it "array" do
57
- post_with_filter(
58
- :pass_params => { :a => 1, :b => 2, :c => 3 },
59
- :allow => []
60
- )
61
- result_should_be_equal({ "splat"=>[], "captures"=>["foo"] })
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
@@ -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 result_should_be_equal(filterd_params)
16
- last_response.body.should == filterd_params.to_json
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.0
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: 2012-11-26 00:00:00.000000000 Z
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