sinatra-strong-params 0.0.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/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +72 -0
- data/Rakefile +2 -0
- data/lib/sinatra/strong-params.rb +78 -0
- data/lib/sinatra/strong-params/version.rb +5 -0
- data/sinatra-strong-params.gemspec +24 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a0c3d5236e9fcbd2d878573d2e04dbdef15b7df3
|
4
|
+
data.tar.gz: 300ef2e3e4cf32ab6d55f0fbbfe839dc647110ca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0f99cc84b7b4145cffeafbc6510add4ada732befcb7d466c5155942b3af48afdd163b6fcc4ee0a44ccef0dd70d1bbc523326c03d16b094891ff341ca1c21ed85
|
7
|
+
data.tar.gz: 676ee9e458fff1d5b253c7102f9d8e2822d7dbdb4d661ef53a2aa61ee1004a5d11ef970977f34f4412349deb894215f86bb4f7e20865e9598117ce7d7c8092f9
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Evan Lecklider
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Sinatra::StrongParams
|
2
|
+
|
3
|
+
A really naive parameter filtering implementation for Sinatra.
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'sinatra-strong-params', :require => 'sinatra/strong-params'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sinatra-strong-params
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
This gem adds two filters to Sinatra routes: `allows` and `needs`.
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
### Allows
|
30
|
+
|
31
|
+
A way to whitelist parameters in the request scope.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
get '/', allows: [:id, :action] do
|
35
|
+
erb :index
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
`allows` modifies the parameters available in the request scope, so
|
40
|
+
beware, though it stashes unmodified params in @_params.
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
### Needs
|
45
|
+
|
46
|
+
A way to require parameters in the request scope.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
get '/', needs: [:id, :action] do
|
50
|
+
erb :index
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
`needs` does not modify the parameters available to the request scope
|
55
|
+
and raises a RequiredParamMissing error if a needed param is missing.
|
56
|
+
|
57
|
+
Catching a missing parameter:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
error RequiredParamMissing do
|
61
|
+
[400, env['sinatra.error'].message]
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
1. Fork it ( https://github.com/[my-github-username]/sinatra-strong-params/fork )
|
69
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
70
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
71
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
72
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'sinatra/strong-params/version'
|
3
|
+
|
4
|
+
class RequiredParamMissing < ArgumentError; end
|
5
|
+
|
6
|
+
module Sinatra
|
7
|
+
module StrongParams
|
8
|
+
def self.registered(app)
|
9
|
+
#
|
10
|
+
# A way to whitelist parameters.
|
11
|
+
#
|
12
|
+
# get '/', allows: [:id, :action] do
|
13
|
+
# erb :index
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# Modifies the parameters available in the request scope.
|
17
|
+
# Stashes unmodified params in @_params
|
18
|
+
#
|
19
|
+
app.set(:allows) do |*passable|
|
20
|
+
condition do
|
21
|
+
unless @params.blank?
|
22
|
+
@_params = @_params || @params # for safety
|
23
|
+
globals = settings.globally_allowed_parameters
|
24
|
+
passable = (globals | passable).map(&:to_sym) # make sure it's a symbol
|
25
|
+
|
26
|
+
# trim the params down
|
27
|
+
@params = @params.select do |param, value|
|
28
|
+
passable.include?(param.to_sym)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# A way to require parameters
|
36
|
+
#
|
37
|
+
# get '/', needs: [:id, :action] do
|
38
|
+
# erb :index
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# Does not modify the parameters available to the request scope.
|
42
|
+
# Raises a RequiredParamMissing error if a needed param is missing
|
43
|
+
#
|
44
|
+
app.set(:needs) do |*needed|
|
45
|
+
condition do
|
46
|
+
if @params.nil? || @params.empty? && !needed.empty?
|
47
|
+
raise RequiredParamMissing, 'One or more required parameters were missing.'
|
48
|
+
else
|
49
|
+
@_params = @_params || @params # for safety
|
50
|
+
needed = needed.map(&:to_sym) # make sure it's a symbol
|
51
|
+
sym_params = @params.dup
|
52
|
+
|
53
|
+
# symbolize the keys so we know what we're looking at
|
54
|
+
sym_params.keys.each do |key|
|
55
|
+
sym_params[(key.to_sym rescue key) || key] = sym_params.delete(key)
|
56
|
+
end
|
57
|
+
|
58
|
+
if needed.any? { |key| sym_params[key].nil? || sym_params[key].empty? }
|
59
|
+
raise RequiredParamMissing, 'One or more required parameters were missing.'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# these will always pass through the 'allows' method
|
66
|
+
# and will be mapped to symbols. I often use ['redirect_to', '_csrf'] here
|
67
|
+
# because I always want them to pass through for later processing
|
68
|
+
app.set :globally_allowed_parameters, []
|
69
|
+
|
70
|
+
# default error response
|
71
|
+
app.error RequiredParamMissing do
|
72
|
+
[400, env['sinatra.error'].message]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
register StrongParams
|
78
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
$:.unshift File.expand_path("../lib", __FILE__)
|
3
|
+
require 'sinatra/strong-params/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "sinatra-strong-params"
|
7
|
+
spec.version = Sinatra::StrongParams::VERSION
|
8
|
+
spec.authors = ["Evan Lecklider"]
|
9
|
+
spec.email = ["evan@lecklider.com"]
|
10
|
+
spec.summary = %q{Some super basic strong parameter filters for Sinatra.}
|
11
|
+
spec.description = spec.summary
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency 'sinatra', '>= 1.4.0'
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-strong-params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evan Lecklider
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.4.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.4.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Some super basic strong parameter filters for Sinatra.
|
56
|
+
email:
|
57
|
+
- evan@lecklider.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/sinatra/strong-params.rb
|
68
|
+
- lib/sinatra/strong-params/version.rb
|
69
|
+
- sinatra-strong-params.gemspec
|
70
|
+
homepage: ''
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.3.0
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Some super basic strong parameter filters for Sinatra.
|
94
|
+
test_files: []
|