sinatra-param 1.0.3 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +30 -1
- data/lib/sinatra/param/version.rb +1 -1
- data/lib/sinatra/param.rb +19 -12
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b64e12a2936520c3207c50339213644d8610709c
|
4
|
+
data.tar.gz: 41d317945c4128feb98c69f096797fc380932d38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fef20f2728a516b7c19d85058d27119e9976e49f5f20d8a238de837c0637f7ad3fb2abc368ba48aa8a55ff8bcd8c93da48792bbba18c4ad47e48b8c95b79ea30
|
7
|
+
data.tar.gz: e1aa8c9c2f62bf7c44e349401aae77f880b27a40c7d1109e00a54256ac01ce489315aff0756f0214a4ea3fee11af7361b86186f45fe43d427be87568a999243c
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sinatra-param (1.0
|
4
|
+
sinatra-param (1.1.0)
|
5
5
|
sinatra (~> 1.3)
|
6
6
|
|
7
7
|
GEM
|
@@ -10,7 +10,7 @@ GEM
|
|
10
10
|
diff-lcs (1.2.4)
|
11
11
|
multi_json (1.7.2)
|
12
12
|
rack (1.5.2)
|
13
|
-
rack-protection (1.5.
|
13
|
+
rack-protection (1.5.2)
|
14
14
|
rack
|
15
15
|
rack-test (0.6.2)
|
16
16
|
rack (>= 1.0)
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@ _Parameter Contracts for Sinatra_
|
|
3
3
|
|
4
4
|
REST conventions take the guesswork out of designing and consuming web APIs. Simply `GET`, `POST`, `PATCH`, or `DELETE` resource endpoints, and you get what you'd expect.
|
5
5
|
|
6
|
-
However, when it comes to figuring out what parameters are expected... well, all bets are off.
|
6
|
+
However, when it comes to figuring out what parameters are expected... well, all bets are off.
|
7
7
|
|
8
8
|
This Sinatra extension takes a first step to solving this problem on the developer side
|
9
9
|
|
@@ -66,6 +66,35 @@ Passing a `default` option will provide a default value for a parameter if none
|
|
66
66
|
|
67
67
|
Use the `transform` option to take even more of the business logic of parameter I/O out of your code. Anything that responds to `to_proc` (including Procs and symbols) will do.
|
68
68
|
|
69
|
+
### Exceptions
|
70
|
+
|
71
|
+
By default, when a parameter precondition fails, `Sinatra::Param` will `halt 400` with an error message:
|
72
|
+
|
73
|
+
```json
|
74
|
+
{
|
75
|
+
"errors": {
|
76
|
+
"order": "Param must be within [\"ASC\", \"DESC\"]"
|
77
|
+
},
|
78
|
+
"message": "Invalid parameter, order"
|
79
|
+
}
|
80
|
+
```
|
81
|
+
|
82
|
+
To change this, you can set `:raise_sinatra_param_exceptions` to `true`, and intercept `Sinatra::Param::InvalidParameterError` with a Sinatra `error do...end` block. _(To make this work in development, set `:show_exceptions` to `false`)_:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
set :raise_sinatra_param_exceptions, true
|
86
|
+
|
87
|
+
error Sinatra::Param::InvalidParameterError do
|
88
|
+
{error: "#{env['sinatra.error'].param} is invalid"}.to_json
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
Custom exception handling can also be enabled on an individual parameter basis, by passing the `raise` option:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
param :order, String, in: ["ASC", "DESC"], raise: true
|
96
|
+
```
|
97
|
+
|
69
98
|
## Contact
|
70
99
|
|
71
100
|
Mattt Thompson
|
data/lib/sinatra/param.rb
CHANGED
@@ -7,7 +7,9 @@ module Sinatra
|
|
7
7
|
module Param
|
8
8
|
Boolean = :boolean
|
9
9
|
|
10
|
-
class InvalidParameterError < StandardError
|
10
|
+
class InvalidParameterError < StandardError
|
11
|
+
attr_accessor :param, :options
|
12
|
+
end
|
11
13
|
|
12
14
|
def param(name, type, options = {})
|
13
15
|
name = name.to_s
|
@@ -19,10 +21,15 @@ module Sinatra
|
|
19
21
|
params[name] = options[:default] if params[name].nil? and options[:default]
|
20
22
|
params[name] = options[:transform].to_proc.call(params[name]) if options[:transform]
|
21
23
|
validate!(params[name], options)
|
22
|
-
rescue
|
23
|
-
|
24
|
+
rescue InvalidParameterError => exception
|
25
|
+
if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)
|
26
|
+
exception.param, exception.options = name, options
|
27
|
+
raise exception
|
28
|
+
end
|
29
|
+
|
30
|
+
error = "Invalid Parameter: #{name}"
|
24
31
|
if content_type and content_type.match(mime_type(:json))
|
25
|
-
error = {message: error}.to_json
|
32
|
+
error = {message: error, errors: {name => exception.message}}.to_json
|
26
33
|
end
|
27
34
|
|
28
35
|
halt 400, error
|
@@ -67,9 +74,9 @@ module Sinatra
|
|
67
74
|
options.each do |key, value|
|
68
75
|
case key
|
69
76
|
when :required
|
70
|
-
raise InvalidParameterError if value && param.nil?
|
77
|
+
raise InvalidParameterError, "Parameter is required" if value && param.nil?
|
71
78
|
when :blank
|
72
|
-
raise InvalidParameterError if !value && case param
|
79
|
+
raise InvalidParameterError, "Parameter cannot be blank" if !value && case param
|
73
80
|
when String
|
74
81
|
!(/\S/ === param)
|
75
82
|
when Array, Hash
|
@@ -78,22 +85,22 @@ module Sinatra
|
|
78
85
|
param.nil?
|
79
86
|
end
|
80
87
|
when :is
|
81
|
-
raise InvalidParameterError unless value === param
|
88
|
+
raise InvalidParameterError, "Parameter must be #{value}" unless value === param
|
82
89
|
when :in, :within, :range
|
83
|
-
raise InvalidParameterError unless param.nil? || case value
|
90
|
+
raise InvalidParameterError, "Parameter must be within #{value}" unless param.nil? || case value
|
84
91
|
when Range
|
85
92
|
value.include?(param)
|
86
93
|
else
|
87
94
|
Array(value).include?(param)
|
88
95
|
end
|
89
96
|
when :min
|
90
|
-
raise InvalidParameterError unless param.nil? || value <= param
|
97
|
+
raise InvalidParameterError, "Parameter cannot be less than #{value}" unless param.nil? || value <= param
|
91
98
|
when :max
|
92
|
-
raise InvalidParameterError unless param.nil? || value >= param
|
99
|
+
raise InvalidParameterError, "Parameter cannot be greater than #{value}" unless param.nil? || value >= param
|
93
100
|
when :min_length
|
94
|
-
raise InvalidParameterError unless param.nil? || value <= param.length
|
101
|
+
raise InvalidParameterError, "Parameter cannot have length less than #{value}" unless param.nil? || value <= param.length
|
95
102
|
when :max_length
|
96
|
-
raise InvalidParameterError unless param.nil? || value >= param.length
|
103
|
+
raise InvalidParameterError, "Parameter cannot have length greater than #{value}" unless param.nil? || value >= param.length
|
97
104
|
end
|
98
105
|
end
|
99
106
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-param
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattt Thompson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -168,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
168
|
version: '0'
|
169
169
|
requirements: []
|
170
170
|
rubyforge_project:
|
171
|
-
rubygems_version: 2.
|
171
|
+
rubygems_version: 2.1.11
|
172
172
|
signing_key:
|
173
173
|
specification_version: 4
|
174
174
|
summary: Parameter Contracts for Sinatra
|