sinatra-param 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +14 -3
- data/README.md +1 -0
- data/{example → lib/example}/Gemfile +0 -0
- data/{example → lib/example}/Gemfile.lock +0 -0
- data/{example → lib/example}/Procfile +0 -0
- data/{example → lib/example}/app.rb +16 -1
- data/{example → lib/example}/config.ru +0 -0
- data/lib/sinatra/param.rb +32 -6
- data/lib/sinatra/param/version.rb +5 -0
- data/sinatra-param.gemspec +3 -2
- data/spec/exclude_spec.rb +36 -0
- data/spec/spec_helper.rb +3 -0
- metadata +33 -18
- data/sinatra-param-0.0.1.gem +0 -0
data/Gemfile.lock
CHANGED
@@ -1,17 +1,27 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sinatra-param (0.0
|
4
|
+
sinatra-param (0.1.0)
|
5
5
|
sinatra (~> 1.3)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
+
diff-lcs (1.1.3)
|
10
11
|
rack (1.4.1)
|
11
12
|
rack-protection (1.2.0)
|
12
13
|
rack
|
14
|
+
rack-test (0.6.1)
|
15
|
+
rack (>= 1.0)
|
13
16
|
rake (0.9.2.2)
|
14
|
-
rspec (
|
17
|
+
rspec (2.10.0)
|
18
|
+
rspec-core (~> 2.10.0)
|
19
|
+
rspec-expectations (~> 2.10.0)
|
20
|
+
rspec-mocks (~> 2.10.0)
|
21
|
+
rspec-core (2.10.1)
|
22
|
+
rspec-expectations (2.10.0)
|
23
|
+
diff-lcs (~> 1.1.3)
|
24
|
+
rspec-mocks (2.10.1)
|
15
25
|
sinatra (1.3.2)
|
16
26
|
rack (~> 1.3, >= 1.3.6)
|
17
27
|
rack-protection (~> 1.2)
|
@@ -22,7 +32,8 @@ PLATFORMS
|
|
22
32
|
ruby
|
23
33
|
|
24
34
|
DEPENDENCIES
|
35
|
+
rack-test
|
25
36
|
rake (~> 0.9.2)
|
26
|
-
rspec (~>
|
37
|
+
rspec (~> 2.10)
|
27
38
|
sinatra (~> 1.3)
|
28
39
|
sinatra-param!
|
data/README.md
CHANGED
File without changes
|
File without changes
|
File without changes
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
require 'json'
|
3
3
|
|
4
|
-
|
4
|
+
require_relative '../sinatra/param'
|
5
5
|
|
6
6
|
class App < Sinatra::Base
|
7
7
|
helpers Sinatra::Param
|
@@ -39,5 +39,20 @@ class App < Sinatra::Base
|
|
39
39
|
message: params[:message]
|
40
40
|
}.to_json
|
41
41
|
end
|
42
|
+
|
43
|
+
# GET /choice?a=foo
|
44
|
+
# GET /choice?b=bar
|
45
|
+
# GET /choice?c=baz
|
46
|
+
get '/choice' do
|
47
|
+
param :a, String
|
48
|
+
param :b, String
|
49
|
+
param :c, String
|
50
|
+
|
51
|
+
one_of(:a, :b, :c)
|
52
|
+
|
53
|
+
{
|
54
|
+
message: 'OK'
|
55
|
+
}.to_json
|
56
|
+
end
|
42
57
|
end
|
43
58
|
|
File without changes
|
data/lib/sinatra/param.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
require 'sinatra/base'
|
2
|
+
require 'sinatra/param/version'
|
2
3
|
require 'time'
|
3
4
|
require 'date'
|
4
5
|
|
5
6
|
module Sinatra
|
6
7
|
module Param
|
7
|
-
VERSION = "0.0.2"
|
8
|
-
|
9
8
|
class InvalidParameterError < StandardError; end
|
10
9
|
|
11
10
|
def param(name, type, options = {})
|
@@ -23,10 +22,25 @@ module Sinatra
|
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
25
|
+
def one_of(*names)
|
26
|
+
found = 0
|
27
|
+
names.each do |name|
|
28
|
+
found += 1 if params[name] && present?(params[name])
|
29
|
+
if found > 1
|
30
|
+
error = "Parameters #{names.join(', ')} are mutually exclusive"
|
31
|
+
if content_type.match(mime_type(:json))
|
32
|
+
error = {message: error}.to_json
|
33
|
+
end
|
34
|
+
halt 406, error
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
26
39
|
private
|
27
40
|
|
28
41
|
def coerce(param, type, options = {})
|
29
42
|
return nil if param.nil?
|
43
|
+
return param if param.is_a?(type)
|
30
44
|
return Integer(param) if type == Integer
|
31
45
|
return Float(param) if type == Float
|
32
46
|
return String(param) if type == String
|
@@ -35,15 +49,15 @@ module Sinatra
|
|
35
49
|
return DateTime.parse(param) if type == DateTime
|
36
50
|
return Array(param.split(options[:delimiter] || ",")) if type == Array
|
37
51
|
return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
|
38
|
-
return (
|
52
|
+
return (/(false|f|no|n|0)$/i === param.to_s ? false : (/(true|t|yes|y|1)$/i === param.to_s ? true : nil)) if type == TrueClass || type == FalseClass || type == :boolean
|
39
53
|
return nil
|
40
54
|
end
|
41
55
|
|
42
56
|
def validate!(param, options)
|
43
57
|
options.each do |key, value|
|
44
58
|
case key
|
45
|
-
|
46
|
-
|
59
|
+
when :required
|
60
|
+
raise InvalidParameterError if value && param.nil?
|
47
61
|
when :blank
|
48
62
|
raise InvalidParameterError if !value && case param
|
49
63
|
when String
|
@@ -66,10 +80,22 @@ module Sinatra
|
|
66
80
|
raise InvalidParameterError unless param.nil? || value <= param
|
67
81
|
when :max
|
68
82
|
raise InvalidParameterError unless param.nil? || value >= param
|
83
|
+
when :min_length
|
84
|
+
raise InvalidParameterError unless param.nil? || value <= param.length
|
85
|
+
when :max_length
|
86
|
+
raise InvalidParameterError unless param.nil? || value >= param.length
|
69
87
|
end
|
70
88
|
end
|
71
89
|
end
|
90
|
+
|
91
|
+
# ActiveSupport #present? and #blank? without patching Object
|
92
|
+
def present?(object)
|
93
|
+
!blank?(object)
|
94
|
+
end
|
95
|
+
|
96
|
+
def blank?(object)
|
97
|
+
object.respond_to?(:empty?) ? object.empty? : !object
|
98
|
+
end
|
72
99
|
end
|
73
|
-
|
74
100
|
helpers Param
|
75
101
|
end
|
data/sinatra-param.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "sinatra/param"
|
3
|
+
require "sinatra/param/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "sinatra-param"
|
@@ -14,7 +14,8 @@ Gem::Specification.new do |s|
|
|
14
14
|
|
15
15
|
s.add_dependency "sinatra", "~> 1.3"
|
16
16
|
|
17
|
-
s.add_development_dependency "rspec", "~>
|
17
|
+
s.add_development_dependency "rspec", "~> 2.10"
|
18
|
+
s.add_development_dependency "rack-test"
|
18
19
|
s.add_development_dependency "rake", "~> 0.9.2"
|
19
20
|
|
20
21
|
s.files = Dir["./**/*"].reject { |file| file =~ /\.\/(bin|log|pkg|script|spec|test|vendor)/ }
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rack/test'
|
3
|
+
|
4
|
+
describe 'parameter exclusion' do
|
5
|
+
include Rack::Test::Methods
|
6
|
+
def app; App; end
|
7
|
+
|
8
|
+
it 'returns 406 on requests that contain more than one mutually exclusive parameter' do
|
9
|
+
params = [
|
10
|
+
{a: 1, b: 2},
|
11
|
+
{b: 2, c: 3},
|
12
|
+
{a: 1, b: 2, c: 3}
|
13
|
+
]
|
14
|
+
params.each do |param|
|
15
|
+
get('/exclude', param) do |response|
|
16
|
+
response.status.should == 406
|
17
|
+
JSON.parse(response.body)['message'].should =~ /mutually exclusive/
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns successfully for requests that only have one parameter' do
|
23
|
+
params = [
|
24
|
+
{a: 1},
|
25
|
+
{b: 2},
|
26
|
+
{c: 3},
|
27
|
+
{a: 1, c: 3},
|
28
|
+
]
|
29
|
+
params.each do |param|
|
30
|
+
get('/exclude', param) do |response|
|
31
|
+
response.status.should == 200
|
32
|
+
JSON.parse(response.body)['message'].should =~ /OK/
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-param
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-01 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sinatra
|
16
|
-
requirement: &
|
16
|
+
requirement: &70343992738160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,21 +21,32 @@ dependencies:
|
|
21
21
|
version: '1.3'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70343992738160
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70343992737380 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
32
|
+
version: '2.10'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70343992737380
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rack-test
|
38
|
+
requirement: &70343992736800 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70343992736800
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rake
|
38
|
-
requirement: &
|
49
|
+
requirement: &70343992736040 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ~>
|
@@ -43,26 +54,28 @@ dependencies:
|
|
43
54
|
version: 0.9.2
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *70343992736040
|
47
58
|
description: Parameter Contracts for Sinatra
|
48
59
|
email: m@mattt.me
|
49
60
|
executables: []
|
50
61
|
extensions: []
|
51
62
|
extra_rdoc_files: []
|
52
63
|
files:
|
53
|
-
- ./example/app.rb
|
54
|
-
- ./example/config.ru
|
55
|
-
- ./example/Gemfile
|
56
|
-
- ./example/Gemfile.lock
|
57
|
-
- ./example/Procfile
|
58
64
|
- ./Gemfile
|
59
65
|
- ./Gemfile.lock
|
66
|
+
- ./lib/example/app.rb
|
67
|
+
- ./lib/example/config.ru
|
68
|
+
- ./lib/example/Gemfile
|
69
|
+
- ./lib/example/Gemfile.lock
|
70
|
+
- ./lib/example/Procfile
|
71
|
+
- ./lib/sinatra/param/version.rb
|
60
72
|
- ./lib/sinatra/param.rb
|
61
73
|
- ./LICENSE
|
62
74
|
- ./Rakefile
|
63
75
|
- ./README.md
|
64
|
-
- ./sinatra-param-0.0.1.gem
|
65
76
|
- ./sinatra-param.gemspec
|
77
|
+
- spec/exclude_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
66
79
|
homepage: http://github.com/mattt/sinatra-param
|
67
80
|
licenses: []
|
68
81
|
post_install_message:
|
@@ -77,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
90
|
version: '0'
|
78
91
|
segments:
|
79
92
|
- 0
|
80
|
-
hash:
|
93
|
+
hash: -1546291879647992050
|
81
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
95
|
none: false
|
83
96
|
requirements:
|
@@ -86,11 +99,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
99
|
version: '0'
|
87
100
|
segments:
|
88
101
|
- 0
|
89
|
-
hash:
|
102
|
+
hash: -1546291879647992050
|
90
103
|
requirements: []
|
91
104
|
rubyforge_project:
|
92
105
|
rubygems_version: 1.8.15
|
93
106
|
signing_key:
|
94
107
|
specification_version: 3
|
95
108
|
summary: sinatra-param
|
96
|
-
test_files:
|
109
|
+
test_files:
|
110
|
+
- spec/exclude_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
data/sinatra-param-0.0.1.gem
DELETED
Binary file
|