sinatra-param 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -2
- data/coverage/index.html +178 -136
- data/lib/sinatra/param.rb +2 -2
- data/lib/sinatra/param/version.rb +1 -1
- data/sinatra-param-1.2.0.gem +0 -0
- data/sinatra-param.gemspec +1 -1
- data/spec/dummy/app.rb +10 -1
- data/spec/parameter_raise_spec.rb +11 -0
- data/spec/parameter_type_coercion_spec.rb +18 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f56711fb7a3deeffa2be390d4653e939c0c0bd9
|
4
|
+
data.tar.gz: 4cee9c0eece1a97c04565ce6b3b9929921c94cda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 595865db457636cf6005697a18694de4609d4fe632812fc2cc45377e14711efa54ca540bdf67348171db01e7c34a3d0aaee8f36a20db87ba942cce66051ae564
|
7
|
+
data.tar.gz: 7a15f035edd7e388bc2a5fe65d07b55f4be43f8f7e2f82628bd54c77ddc3dbc252b87e49645830fc871fbfd6eb48c16f7bd4c0184f63192884d2c1b7e28cec88
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# sinatra-param
|
2
|
-
_Parameter
|
2
|
+
_Parameter Validation & Type Coercion 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
|
|
@@ -50,6 +50,7 @@ By declaring parameter types, incoming parameters will automatically be transfor
|
|
50
50
|
- `Boolean` _("1/0", "true/false", "t/f", "yes/no", "y/n")_
|
51
51
|
- `Array` _("1,2,3,4,5")_
|
52
52
|
- `Hash` _(key1:value1,key2:value2)_
|
53
|
+
- `Date`, `Time`, & `DateTime`
|
53
54
|
|
54
55
|
### Validations
|
55
56
|
|
@@ -91,7 +92,7 @@ By default, when a parameter precondition fails, `Sinatra::Param` will `halt 400
|
|
91
92
|
}
|
92
93
|
```
|
93
94
|
|
94
|
-
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`):
|
95
|
+
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` and `:raise_errors` to `true`):
|
95
96
|
|
96
97
|
```ruby
|
97
98
|
set :raise_sinatra_param_exceptions, true
|
data/coverage/index.html
CHANGED
@@ -14,27 +14,27 @@
|
|
14
14
|
<img src="./assets/0.7.1/loading.gif" alt="loading"/>
|
15
15
|
</div>
|
16
16
|
<div id="wrapper" style="display:none;">
|
17
|
-
<div class="timestamp">Generated <abbr class="timeago" title="2014-
|
17
|
+
<div class="timestamp">Generated <abbr class="timeago" title="2014-04-05T12:42:49-07:00">2014-04-05T12:42:49-07:00</abbr></div>
|
18
18
|
<ul class="group_tabs"></ul>
|
19
19
|
|
20
20
|
<div id="content">
|
21
21
|
<div class="file_list_container" id="AllFiles">
|
22
22
|
<h2>
|
23
23
|
<span class="group_name">All Files</span>
|
24
|
-
(<span class="covered_percent"><span class="green">
|
24
|
+
(<span class="covered_percent"><span class="green">100.0%</span></span>
|
25
25
|
covered at
|
26
26
|
<span class="covered_strength">
|
27
27
|
<span class="green">
|
28
|
-
|
28
|
+
19.9
|
29
29
|
</span>
|
30
30
|
</span> hits/line)
|
31
31
|
</h2>
|
32
32
|
<a name="AllFiles"></a>
|
33
33
|
<div>
|
34
34
|
<b>1</b> files in total.
|
35
|
-
<b>
|
36
|
-
<span class="green"><b>
|
37
|
-
<span class="red"><b>
|
35
|
+
<b>74</b> relevant lines.
|
36
|
+
<span class="green"><b>74</b> lines covered</span> and
|
37
|
+
<span class="red"><b>0</b> lines missed </span>
|
38
38
|
</div>
|
39
39
|
<table class="file_list">
|
40
40
|
<thead>
|
@@ -52,12 +52,12 @@
|
|
52
52
|
|
53
53
|
<tr>
|
54
54
|
<td class="strong"><a href="#0f7926ededdfd8418773c107837b6dc4182659b8" class="src_link" title="lib/sinatra/param.rb">lib/sinatra/param.rb</a></td>
|
55
|
-
<td class="green strong">
|
56
|
-
<td>
|
57
|
-
<td>
|
58
|
-
<td>
|
59
|
-
<td>
|
60
|
-
<td>
|
55
|
+
<td class="green strong">100.0 %</td>
|
56
|
+
<td>126</td>
|
57
|
+
<td>74</td>
|
58
|
+
<td>74</td>
|
59
|
+
<td>0</td>
|
60
|
+
<td>19.9</td>
|
61
61
|
</tr>
|
62
62
|
|
63
63
|
</tbody>
|
@@ -79,11 +79,11 @@
|
|
79
79
|
<div class="source_table" id="0f7926ededdfd8418773c107837b6dc4182659b8">
|
80
80
|
<div class="header">
|
81
81
|
<h3>lib/sinatra/param.rb</h3>
|
82
|
-
<h4><span class="green">
|
82
|
+
<h4><span class="green">100.0 %</span> covered</h4>
|
83
83
|
<div>
|
84
|
-
<b>
|
85
|
-
<span class="green"><b>
|
86
|
-
<span class="red"><b>
|
84
|
+
<b>74</b> relevant lines.
|
85
|
+
<span class="green"><b>74</b> lines covered</span> and
|
86
|
+
<span class="red"><b>0</b> lines missed.</span>
|
87
87
|
</div>
|
88
88
|
</div>
|
89
89
|
|
@@ -105,13 +105,13 @@
|
|
105
105
|
<li class="covered" data-hits="1" data-linenumber="3">
|
106
106
|
<span class="hits">1</span>
|
107
107
|
|
108
|
-
<code class="ruby">require '
|
108
|
+
<code class="ruby">require 'date'</code>
|
109
109
|
</li>
|
110
110
|
|
111
111
|
<li class="covered" data-hits="1" data-linenumber="4">
|
112
112
|
<span class="hits">1</span>
|
113
113
|
|
114
|
-
<code class="ruby">require '
|
114
|
+
<code class="ruby">require 'time'</code>
|
115
115
|
</li>
|
116
116
|
|
117
117
|
<li class="never" data-hits="" data-linenumber="5">
|
@@ -174,8 +174,8 @@
|
|
174
174
|
<code class="ruby"> def param(name, type, options = {})</code>
|
175
175
|
</li>
|
176
176
|
|
177
|
-
<li class="covered" data-hits="
|
178
|
-
<span class="hits">
|
177
|
+
<li class="covered" data-hits="88" data-linenumber="15">
|
178
|
+
<span class="hits">88</span>
|
179
179
|
|
180
180
|
<code class="ruby"> name = name.to_s</code>
|
181
181
|
</li>
|
@@ -186,8 +186,8 @@
|
|
186
186
|
<code class="ruby"></code>
|
187
187
|
</li>
|
188
188
|
|
189
|
-
<li class="covered" data-hits="
|
190
|
-
<span class="hits">
|
189
|
+
<li class="covered" data-hits="88" data-linenumber="17">
|
190
|
+
<span class="hits">88</span>
|
191
191
|
|
192
192
|
<code class="ruby"> return unless params.member?(name) or present?(options[:default]) or options[:required]</code>
|
193
193
|
</li>
|
@@ -198,56 +198,56 @@
|
|
198
198
|
<code class="ruby"></code>
|
199
199
|
</li>
|
200
200
|
|
201
|
-
<li class="covered" data-hits="
|
202
|
-
<span class="hits">
|
201
|
+
<li class="covered" data-hits="79" data-linenumber="19">
|
202
|
+
<span class="hits">79</span>
|
203
203
|
|
204
204
|
<code class="ruby"> begin</code>
|
205
205
|
</li>
|
206
206
|
|
207
|
-
<li class="covered" data-hits="
|
208
|
-
<span class="hits">
|
207
|
+
<li class="covered" data-hits="79" data-linenumber="20">
|
208
|
+
<span class="hits">79</span>
|
209
209
|
|
210
210
|
<code class="ruby"> params[name] = coerce(params[name], type, options)</code>
|
211
211
|
</li>
|
212
212
|
|
213
|
-
<li class="covered" data-hits="
|
214
|
-
<span class="hits">
|
213
|
+
<li class="covered" data-hits="74" data-linenumber="21">
|
214
|
+
<span class="hits">74</span>
|
215
215
|
|
216
216
|
<code class="ruby"> params[name] = (options[:default].call if options[:default].is_a?(Proc)) || options[:default] if params[name].nil? and options[:default]</code>
|
217
217
|
</li>
|
218
218
|
|
219
|
-
<li class="covered" data-hits="
|
220
|
-
<span class="hits">
|
219
|
+
<li class="covered" data-hits="74" data-linenumber="22">
|
220
|
+
<span class="hits">74</span>
|
221
221
|
|
222
222
|
<code class="ruby"> params[name] = options[:transform].to_proc.call(params[name]) if options[:transform]</code>
|
223
223
|
</li>
|
224
224
|
|
225
|
-
<li class="covered" data-hits="
|
226
|
-
<span class="hits">
|
225
|
+
<li class="covered" data-hits="74" data-linenumber="23">
|
226
|
+
<span class="hits">74</span>
|
227
227
|
|
228
228
|
<code class="ruby"> validate!(params[name], options)</code>
|
229
229
|
</li>
|
230
230
|
|
231
|
-
<li class="covered" data-hits="
|
232
|
-
<span class="hits">
|
231
|
+
<li class="covered" data-hits="21" data-linenumber="24">
|
232
|
+
<span class="hits">21</span>
|
233
233
|
|
234
234
|
<code class="ruby"> rescue InvalidParameterError => exception</code>
|
235
235
|
</li>
|
236
236
|
|
237
|
-
<li class="covered" data-hits="
|
238
|
-
<span class="hits">
|
237
|
+
<li class="covered" data-hits="21" data-linenumber="25">
|
238
|
+
<span class="hits">21</span>
|
239
239
|
|
240
240
|
<code class="ruby"> if options[:raise] or (settings.raise_sinatra_param_exceptions rescue false)</code>
|
241
241
|
</li>
|
242
242
|
|
243
|
-
<li class="
|
244
|
-
|
243
|
+
<li class="covered" data-hits="1" data-linenumber="26">
|
244
|
+
<span class="hits">1</span>
|
245
245
|
|
246
246
|
<code class="ruby"> exception.param, exception.options = name, options</code>
|
247
247
|
</li>
|
248
248
|
|
249
|
-
<li class="
|
250
|
-
|
249
|
+
<li class="covered" data-hits="1" data-linenumber="27">
|
250
|
+
<span class="hits">1</span>
|
251
251
|
|
252
252
|
<code class="ruby"> raise exception</code>
|
253
253
|
</li>
|
@@ -264,20 +264,20 @@
|
|
264
264
|
<code class="ruby"></code>
|
265
265
|
</li>
|
266
266
|
|
267
|
-
<li class="covered" data-hits="
|
268
|
-
<span class="hits">
|
267
|
+
<li class="covered" data-hits="20" data-linenumber="30">
|
268
|
+
<span class="hits">20</span>
|
269
269
|
|
270
270
|
<code class="ruby"> error = "Invalid Parameter: #{name}"</code>
|
271
271
|
</li>
|
272
272
|
|
273
|
-
<li class="covered" data-hits="
|
274
|
-
<span class="hits">
|
273
|
+
<li class="covered" data-hits="20" data-linenumber="31">
|
274
|
+
<span class="hits">20</span>
|
275
275
|
|
276
276
|
<code class="ruby"> if content_type and content_type.match(mime_type(:json))</code>
|
277
277
|
</li>
|
278
278
|
|
279
|
-
<li class="covered" data-hits="
|
280
|
-
<span class="hits">
|
279
|
+
<li class="covered" data-hits="20" data-linenumber="32">
|
280
|
+
<span class="hits">20</span>
|
281
281
|
|
282
282
|
<code class="ruby"> error = {message: error, errors: {name => exception.message}}.to_json</code>
|
283
283
|
</li>
|
@@ -294,8 +294,8 @@
|
|
294
294
|
<code class="ruby"></code>
|
295
295
|
</li>
|
296
296
|
|
297
|
-
<li class="covered" data-hits="
|
298
|
-
<span class="hits">
|
297
|
+
<li class="covered" data-hits="20" data-linenumber="35">
|
298
|
+
<span class="hits">20</span>
|
299
299
|
|
300
300
|
<code class="ruby"> halt 400, error</code>
|
301
301
|
</li>
|
@@ -438,367 +438,409 @@
|
|
438
438
|
<code class="ruby"> def coerce(param, type, options = {})</code>
|
439
439
|
</li>
|
440
440
|
|
441
|
-
<li class="covered" data-hits="
|
442
|
-
<span class="hits">
|
441
|
+
<li class="covered" data-hits="79" data-linenumber="59">
|
442
|
+
<span class="hits">79</span>
|
443
443
|
|
444
|
-
<code class="ruby">
|
444
|
+
<code class="ruby"> begin</code>
|
445
445
|
</li>
|
446
446
|
|
447
|
-
<li class="covered" data-hits="
|
448
|
-
<span class="hits">
|
447
|
+
<li class="covered" data-hits="79" data-linenumber="60">
|
448
|
+
<span class="hits">79</span>
|
449
449
|
|
450
|
-
<code class="ruby">
|
450
|
+
<code class="ruby"> return nil if param.nil?</code>
|
451
451
|
</li>
|
452
452
|
|
453
|
-
<li class="covered" data-hits="
|
454
|
-
<span class="hits">
|
453
|
+
<li class="covered" data-hits="74" data-linenumber="61">
|
454
|
+
<span class="hits">74</span>
|
455
455
|
|
456
|
-
<code class="ruby">
|
456
|
+
<code class="ruby"> return param if (param.is_a?(type) rescue false)</code>
|
457
457
|
</li>
|
458
458
|
|
459
|
-
<li class="covered" data-hits="
|
460
|
-
<span class="hits">
|
459
|
+
<li class="covered" data-hits="46" data-linenumber="62">
|
460
|
+
<span class="hits">46</span>
|
461
461
|
|
462
|
-
<code class="ruby">
|
462
|
+
<code class="ruby"> return Integer(param) if type == Integer</code>
|
463
463
|
</li>
|
464
464
|
|
465
|
-
<li class="covered" data-hits="
|
466
|
-
<span class="hits">
|
465
|
+
<li class="covered" data-hits="35" data-linenumber="63">
|
466
|
+
<span class="hits">35</span>
|
467
467
|
|
468
|
-
<code class="ruby">
|
468
|
+
<code class="ruby"> return Float(param) if type == Float</code>
|
469
469
|
</li>
|
470
470
|
|
471
|
-
<li class="covered" data-hits="
|
472
|
-
<span class="hits">
|
471
|
+
<li class="covered" data-hits="33" data-linenumber="64">
|
472
|
+
<span class="hits">33</span>
|
473
473
|
|
474
|
-
<code class="ruby">
|
474
|
+
<code class="ruby"> return String(param) if type == String</code>
|
475
475
|
</li>
|
476
476
|
|
477
|
-
<li class="covered" data-hits="
|
478
|
-
<span class="hits">
|
477
|
+
<li class="covered" data-hits="32" data-linenumber="65">
|
478
|
+
<span class="hits">32</span>
|
479
479
|
|
480
|
-
<code class="ruby">
|
480
|
+
<code class="ruby"> return Date.parse(param) if type == Date</code>
|
481
481
|
</li>
|
482
482
|
|
483
|
-
<li class="covered" data-hits="
|
484
|
-
<span class="hits">
|
483
|
+
<li class="covered" data-hits="30" data-linenumber="66">
|
484
|
+
<span class="hits">30</span>
|
485
485
|
|
486
|
-
<code class="ruby">
|
486
|
+
<code class="ruby"> return Time.parse(param) if type == Time</code>
|
487
487
|
</li>
|
488
488
|
|
489
|
-
<li class="covered" data-hits="
|
490
|
-
<span class="hits">
|
489
|
+
<li class="covered" data-hits="28" data-linenumber="67">
|
490
|
+
<span class="hits">28</span>
|
491
|
+
|
492
|
+
<code class="ruby"> return DateTime.parse(param) if type == DateTime</code>
|
493
|
+
</li>
|
494
|
+
|
495
|
+
<li class="covered" data-hits="26" data-linenumber="68">
|
496
|
+
<span class="hits">26</span>
|
491
497
|
|
492
|
-
<code class="ruby">
|
498
|
+
<code class="ruby"> return Array(param.split(options[:delimiter] || ",")) if type == Array</code>
|
493
499
|
</li>
|
494
500
|
|
495
|
-
<li class="covered" data-hits="25" data-linenumber="
|
501
|
+
<li class="covered" data-hits="25" data-linenumber="69">
|
496
502
|
<span class="hits">25</span>
|
497
503
|
|
498
|
-
<code class="ruby">
|
504
|
+
<code class="ruby"> return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash</code>
|
499
505
|
</li>
|
500
506
|
|
501
|
-
<li class="covered" data-hits="21" data-linenumber="
|
507
|
+
<li class="covered" data-hits="21" data-linenumber="70">
|
502
508
|
<span class="hits">21</span>
|
503
509
|
|
504
|
-
<code class="ruby">
|
510
|
+
<code class="ruby"> 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</code>
|
505
511
|
</li>
|
506
512
|
|
507
|
-
<li class="never" data-hits="" data-linenumber="
|
513
|
+
<li class="never" data-hits="" data-linenumber="71">
|
508
514
|
|
509
515
|
|
510
|
-
<code class="ruby">
|
516
|
+
<code class="ruby"> return nil</code>
|
511
517
|
</li>
|
512
518
|
|
513
|
-
<li class="
|
519
|
+
<li class="covered" data-hits="5" data-linenumber="72">
|
520
|
+
<span class="hits">5</span>
|
521
|
+
|
522
|
+
<code class="ruby"> rescue ArgumentError</code>
|
523
|
+
</li>
|
524
|
+
|
525
|
+
<li class="covered" data-hits="5" data-linenumber="73">
|
526
|
+
<span class="hits">5</span>
|
527
|
+
|
528
|
+
<code class="ruby"> raise InvalidParameterError, "'#{param}' is not a valid #{type}"</code>
|
529
|
+
</li>
|
530
|
+
|
531
|
+
<li class="never" data-hits="" data-linenumber="74">
|
532
|
+
|
533
|
+
|
534
|
+
<code class="ruby"> end</code>
|
535
|
+
</li>
|
536
|
+
|
537
|
+
<li class="never" data-hits="" data-linenumber="75">
|
514
538
|
|
515
539
|
|
516
540
|
<code class="ruby"> end</code>
|
517
541
|
</li>
|
518
542
|
|
519
|
-
<li class="never" data-hits="" data-linenumber="
|
543
|
+
<li class="never" data-hits="" data-linenumber="76">
|
520
544
|
|
521
545
|
|
522
546
|
<code class="ruby"></code>
|
523
547
|
</li>
|
524
548
|
|
525
|
-
<li class="covered" data-hits="1" data-linenumber="
|
549
|
+
<li class="covered" data-hits="1" data-linenumber="77">
|
526
550
|
<span class="hits">1</span>
|
527
551
|
|
528
552
|
<code class="ruby"> def validate!(param, options)</code>
|
529
553
|
</li>
|
530
554
|
|
531
|
-
<li class="covered" data-hits="
|
532
|
-
<span class="hits">
|
555
|
+
<li class="covered" data-hits="74" data-linenumber="78">
|
556
|
+
<span class="hits">74</span>
|
533
557
|
|
534
558
|
<code class="ruby"> options.each do |key, value|</code>
|
535
559
|
</li>
|
536
560
|
|
537
|
-
<li class="covered" data-hits="
|
538
|
-
<span class="hits">
|
561
|
+
<li class="covered" data-hits="43" data-linenumber="79">
|
562
|
+
<span class="hits">43</span>
|
539
563
|
|
540
564
|
<code class="ruby"> case key</code>
|
541
565
|
</li>
|
542
566
|
|
543
|
-
<li class="never" data-hits="" data-linenumber="
|
567
|
+
<li class="never" data-hits="" data-linenumber="80">
|
544
568
|
|
545
569
|
|
546
570
|
<code class="ruby"> when :required</code>
|
547
571
|
</li>
|
548
572
|
|
549
|
-
<li class="covered" data-hits="
|
550
|
-
<span class="hits">
|
573
|
+
<li class="covered" data-hits="4" data-linenumber="81">
|
574
|
+
<span class="hits">4</span>
|
551
575
|
|
552
576
|
<code class="ruby"> raise InvalidParameterError, "Parameter is required" if value && param.nil?</code>
|
553
577
|
</li>
|
554
578
|
|
555
|
-
<li class="never" data-hits="" data-linenumber="
|
579
|
+
<li class="never" data-hits="" data-linenumber="82">
|
556
580
|
|
557
581
|
|
558
582
|
<code class="ruby"> when :blank</code>
|
559
583
|
</li>
|
560
584
|
|
561
|
-
<li class="never" data-hits="" data-linenumber="
|
585
|
+
<li class="never" data-hits="" data-linenumber="83">
|
562
586
|
|
563
587
|
|
564
588
|
<code class="ruby"> raise InvalidParameterError, "Parameter cannot be blank" if !value && case param</code>
|
565
589
|
</li>
|
566
590
|
|
567
|
-
<li class="never" data-hits="" data-linenumber="
|
591
|
+
<li class="never" data-hits="" data-linenumber="84">
|
568
592
|
|
569
593
|
|
570
594
|
<code class="ruby"> when String</code>
|
571
595
|
</li>
|
572
596
|
|
573
|
-
<li class="covered" data-hits="1" data-linenumber="
|
597
|
+
<li class="covered" data-hits="1" data-linenumber="85">
|
574
598
|
<span class="hits">1</span>
|
575
599
|
|
576
600
|
<code class="ruby"> !(/\S/ === param)</code>
|
577
601
|
</li>
|
578
602
|
|
579
|
-
<li class="never" data-hits="" data-linenumber="
|
603
|
+
<li class="never" data-hits="" data-linenumber="86">
|
580
604
|
|
581
605
|
|
582
606
|
<code class="ruby"> when Array, Hash</code>
|
583
607
|
</li>
|
584
608
|
|
585
|
-
<li class="covered" data-hits="2" data-linenumber="
|
609
|
+
<li class="covered" data-hits="2" data-linenumber="87">
|
586
610
|
<span class="hits">2</span>
|
587
611
|
|
588
612
|
<code class="ruby"> param.empty?</code>
|
589
613
|
</li>
|
590
614
|
|
591
|
-
<li class="never" data-hits="" data-linenumber="
|
615
|
+
<li class="never" data-hits="" data-linenumber="88">
|
592
616
|
|
593
617
|
|
594
618
|
<code class="ruby"> else</code>
|
595
619
|
</li>
|
596
620
|
|
597
|
-
<li class="covered" data-hits="1" data-linenumber="
|
621
|
+
<li class="covered" data-hits="1" data-linenumber="89">
|
598
622
|
<span class="hits">1</span>
|
599
623
|
|
600
624
|
<code class="ruby"> param.nil?</code>
|
601
625
|
</li>
|
602
626
|
|
603
|
-
<li class="covered" data-hits="5" data-linenumber="
|
627
|
+
<li class="covered" data-hits="5" data-linenumber="90">
|
604
628
|
<span class="hits">5</span>
|
605
629
|
|
606
630
|
<code class="ruby"> end</code>
|
607
631
|
</li>
|
608
632
|
|
609
|
-
<li class="never" data-hits="" data-linenumber="
|
633
|
+
<li class="never" data-hits="" data-linenumber="91">
|
634
|
+
|
635
|
+
|
636
|
+
<code class="ruby"> when :format</code>
|
637
|
+
</li>
|
638
|
+
|
639
|
+
<li class="covered" data-hits="3" data-linenumber="92">
|
640
|
+
<span class="hits">3</span>
|
641
|
+
|
642
|
+
<code class="ruby"> raise InvalidParameterError, "Parameter must be a string if using the format validation" unless param.kind_of?(String)</code>
|
643
|
+
</li>
|
644
|
+
|
645
|
+
<li class="covered" data-hits="2" data-linenumber="93">
|
646
|
+
<span class="hits">2</span>
|
647
|
+
|
648
|
+
<code class="ruby"> raise InvalidParameterError, "Parameter must match format #{value}" unless param =~ value</code>
|
649
|
+
</li>
|
650
|
+
|
651
|
+
<li class="never" data-hits="" data-linenumber="94">
|
610
652
|
|
611
653
|
|
612
654
|
<code class="ruby"> when :is</code>
|
613
655
|
</li>
|
614
656
|
|
615
|
-
<li class="covered" data-hits="2" data-linenumber="
|
657
|
+
<li class="covered" data-hits="2" data-linenumber="95">
|
616
658
|
<span class="hits">2</span>
|
617
659
|
|
618
|
-
<code class="ruby"> raise InvalidParameterError, "Parameter must be #{value}" unless
|
660
|
+
<code class="ruby"> raise InvalidParameterError, "Parameter must be #{value}" unless param === value</code>
|
619
661
|
</li>
|
620
662
|
|
621
|
-
<li class="never" data-hits="" data-linenumber="
|
663
|
+
<li class="never" data-hits="" data-linenumber="96">
|
622
664
|
|
623
665
|
|
624
666
|
<code class="ruby"> when :in, :within, :range</code>
|
625
667
|
</li>
|
626
668
|
|
627
|
-
<li class="never" data-hits="" data-linenumber="
|
669
|
+
<li class="never" data-hits="" data-linenumber="97">
|
628
670
|
|
629
671
|
|
630
672
|
<code class="ruby"> raise InvalidParameterError, "Parameter must be within #{value}" unless param.nil? || case value</code>
|
631
673
|
</li>
|
632
674
|
|
633
|
-
<li class="never" data-hits="" data-linenumber="
|
675
|
+
<li class="never" data-hits="" data-linenumber="98">
|
634
676
|
|
635
677
|
|
636
678
|
<code class="ruby"> when Range</code>
|
637
679
|
</li>
|
638
680
|
|
639
|
-
<li class="covered" data-hits="4" data-linenumber="
|
681
|
+
<li class="covered" data-hits="4" data-linenumber="99">
|
640
682
|
<span class="hits">4</span>
|
641
683
|
|
642
684
|
<code class="ruby"> value.include?(param)</code>
|
643
685
|
</li>
|
644
686
|
|
645
|
-
<li class="never" data-hits="" data-linenumber="
|
687
|
+
<li class="never" data-hits="" data-linenumber="100">
|
646
688
|
|
647
689
|
|
648
690
|
<code class="ruby"> else</code>
|
649
691
|
</li>
|
650
692
|
|
651
|
-
<li class="covered" data-hits="2" data-linenumber="
|
693
|
+
<li class="covered" data-hits="2" data-linenumber="101">
|
652
694
|
<span class="hits">2</span>
|
653
695
|
|
654
696
|
<code class="ruby"> Array(value).include?(param)</code>
|
655
697
|
</li>
|
656
698
|
|
657
|
-
<li class="covered" data-hits="6" data-linenumber="
|
699
|
+
<li class="covered" data-hits="6" data-linenumber="102">
|
658
700
|
<span class="hits">6</span>
|
659
701
|
|
660
702
|
<code class="ruby"> end</code>
|
661
703
|
</li>
|
662
704
|
|
663
|
-
<li class="never" data-hits="" data-linenumber="
|
705
|
+
<li class="never" data-hits="" data-linenumber="103">
|
664
706
|
|
665
707
|
|
666
708
|
<code class="ruby"> when :min</code>
|
667
709
|
</li>
|
668
710
|
|
669
|
-
<li class="covered" data-hits="2" data-linenumber="
|
711
|
+
<li class="covered" data-hits="2" data-linenumber="104">
|
670
712
|
<span class="hits">2</span>
|
671
713
|
|
672
714
|
<code class="ruby"> raise InvalidParameterError, "Parameter cannot be less than #{value}" unless param.nil? || value <= param</code>
|
673
715
|
</li>
|
674
716
|
|
675
|
-
<li class="never" data-hits="" data-linenumber="
|
717
|
+
<li class="never" data-hits="" data-linenumber="105">
|
676
718
|
|
677
719
|
|
678
720
|
<code class="ruby"> when :max</code>
|
679
721
|
</li>
|
680
722
|
|
681
|
-
<li class="covered" data-hits="2" data-linenumber="
|
723
|
+
<li class="covered" data-hits="2" data-linenumber="106">
|
682
724
|
<span class="hits">2</span>
|
683
725
|
|
684
726
|
<code class="ruby"> raise InvalidParameterError, "Parameter cannot be greater than #{value}" unless param.nil? || value >= param</code>
|
685
727
|
</li>
|
686
728
|
|
687
|
-
<li class="never" data-hits="" data-linenumber="
|
729
|
+
<li class="never" data-hits="" data-linenumber="107">
|
688
730
|
|
689
731
|
|
690
732
|
<code class="ruby"> when :min_length</code>
|
691
733
|
</li>
|
692
734
|
|
693
|
-
<li class="covered" data-hits="1" data-linenumber="
|
735
|
+
<li class="covered" data-hits="1" data-linenumber="108">
|
694
736
|
<span class="hits">1</span>
|
695
737
|
|
696
738
|
<code class="ruby"> raise InvalidParameterError, "Parameter cannot have length less than #{value}" unless param.nil? || value <= param.length</code>
|
697
739
|
</li>
|
698
740
|
|
699
|
-
<li class="never" data-hits="" data-linenumber="
|
741
|
+
<li class="never" data-hits="" data-linenumber="109">
|
700
742
|
|
701
743
|
|
702
744
|
<code class="ruby"> when :max_length</code>
|
703
745
|
</li>
|
704
746
|
|
705
|
-
<li class="covered" data-hits="3" data-linenumber="
|
747
|
+
<li class="covered" data-hits="3" data-linenumber="110">
|
706
748
|
<span class="hits">3</span>
|
707
749
|
|
708
750
|
<code class="ruby"> raise InvalidParameterError, "Parameter cannot have length greater than #{value}" unless param.nil? || value >= param.length</code>
|
709
751
|
</li>
|
710
752
|
|
711
|
-
<li class="never" data-hits="" data-linenumber="
|
753
|
+
<li class="never" data-hits="" data-linenumber="111">
|
712
754
|
|
713
755
|
|
714
756
|
<code class="ruby"> end</code>
|
715
757
|
</li>
|
716
758
|
|
717
|
-
<li class="never" data-hits="" data-linenumber="
|
759
|
+
<li class="never" data-hits="" data-linenumber="112">
|
718
760
|
|
719
761
|
|
720
762
|
<code class="ruby"> end</code>
|
721
763
|
</li>
|
722
764
|
|
723
|
-
<li class="never" data-hits="" data-linenumber="
|
765
|
+
<li class="never" data-hits="" data-linenumber="113">
|
724
766
|
|
725
767
|
|
726
768
|
<code class="ruby"> end</code>
|
727
769
|
</li>
|
728
770
|
|
729
|
-
<li class="never" data-hits="" data-linenumber="
|
771
|
+
<li class="never" data-hits="" data-linenumber="114">
|
730
772
|
|
731
773
|
|
732
774
|
<code class="ruby"></code>
|
733
775
|
</li>
|
734
776
|
|
735
|
-
<li class="never" data-hits="" data-linenumber="
|
777
|
+
<li class="never" data-hits="" data-linenumber="115">
|
736
778
|
|
737
779
|
|
738
780
|
<code class="ruby"> # ActiveSupport #present? and #blank? without patching Object</code>
|
739
781
|
</li>
|
740
782
|
|
741
|
-
<li class="covered" data-hits="1" data-linenumber="
|
783
|
+
<li class="covered" data-hits="1" data-linenumber="116">
|
742
784
|
<span class="hits">1</span>
|
743
785
|
|
744
786
|
<code class="ruby"> def present?(object)</code>
|
745
787
|
</li>
|
746
788
|
|
747
|
-
<li class="covered" data-hits="
|
748
|
-
<span class="hits">
|
789
|
+
<li class="covered" data-hits="23" data-linenumber="117">
|
790
|
+
<span class="hits">23</span>
|
749
791
|
|
750
792
|
<code class="ruby"> !blank?(object)</code>
|
751
793
|
</li>
|
752
794
|
|
753
|
-
<li class="never" data-hits="" data-linenumber="
|
795
|
+
<li class="never" data-hits="" data-linenumber="118">
|
754
796
|
|
755
797
|
|
756
798
|
<code class="ruby"> end</code>
|
757
799
|
</li>
|
758
800
|
|
759
|
-
<li class="never" data-hits="" data-linenumber="
|
801
|
+
<li class="never" data-hits="" data-linenumber="119">
|
760
802
|
|
761
803
|
|
762
804
|
<code class="ruby"></code>
|
763
805
|
</li>
|
764
806
|
|
765
|
-
<li class="covered" data-hits="1" data-linenumber="
|
807
|
+
<li class="covered" data-hits="1" data-linenumber="120">
|
766
808
|
<span class="hits">1</span>
|
767
809
|
|
768
810
|
<code class="ruby"> def blank?(object)</code>
|
769
811
|
</li>
|
770
812
|
|
771
|
-
<li class="covered" data-hits="
|
772
|
-
<span class="hits">
|
813
|
+
<li class="covered" data-hits="23" data-linenumber="121">
|
814
|
+
<span class="hits">23</span>
|
773
815
|
|
774
816
|
<code class="ruby"> object.respond_to?(:empty?) ? object.empty? : !object</code>
|
775
817
|
</li>
|
776
818
|
|
777
|
-
<li class="never" data-hits="" data-linenumber="
|
819
|
+
<li class="never" data-hits="" data-linenumber="122">
|
778
820
|
|
779
821
|
|
780
822
|
<code class="ruby"> end</code>
|
781
823
|
</li>
|
782
824
|
|
783
|
-
<li class="never" data-hits="" data-linenumber="
|
825
|
+
<li class="never" data-hits="" data-linenumber="123">
|
784
826
|
|
785
827
|
|
786
828
|
<code class="ruby"> end</code>
|
787
829
|
</li>
|
788
830
|
|
789
|
-
<li class="never" data-hits="" data-linenumber="
|
831
|
+
<li class="never" data-hits="" data-linenumber="124">
|
790
832
|
|
791
833
|
|
792
834
|
<code class="ruby"></code>
|
793
835
|
</li>
|
794
836
|
|
795
|
-
<li class="covered" data-hits="1" data-linenumber="
|
837
|
+
<li class="covered" data-hits="1" data-linenumber="125">
|
796
838
|
<span class="hits">1</span>
|
797
839
|
|
798
840
|
<code class="ruby"> helpers Param</code>
|
799
841
|
</li>
|
800
842
|
|
801
|
-
<li class="never" data-hits="" data-linenumber="
|
843
|
+
<li class="never" data-hits="" data-linenumber="126">
|
802
844
|
|
803
845
|
|
804
846
|
<code class="ruby">end</code>
|
data/lib/sinatra/param.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
require 'sinatra/param/version'
|
3
|
-
require 'time'
|
4
3
|
require 'date'
|
4
|
+
require 'time'
|
5
5
|
|
6
6
|
module Sinatra
|
7
7
|
module Param
|
@@ -62,8 +62,8 @@ module Sinatra
|
|
62
62
|
return Integer(param) if type == Integer
|
63
63
|
return Float(param) if type == Float
|
64
64
|
return String(param) if type == String
|
65
|
-
return Time.parse(param) if type == Time
|
66
65
|
return Date.parse(param) if type == Date
|
66
|
+
return Time.parse(param) if type == Time
|
67
67
|
return DateTime.parse(param) if type == DateTime
|
68
68
|
return Array(param.split(options[:delimiter] || ",")) if type == Array
|
69
69
|
return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
|
Binary file
|
data/sinatra-param.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.homepage = "https://github.com/mattt/sinatra-param"
|
10
10
|
s.version = Sinatra::Param::VERSION
|
11
11
|
s.platform = Gem::Platform::RUBY
|
12
|
-
s.summary = "Parameter
|
12
|
+
s.summary = "Parameter Validation & Type Coercion for Sinatra."
|
13
13
|
s.description = "sinatra-param allows you to declare, validate, and transform endpoint parameters as you would in frameworks like ActiveModel or DataMapper."
|
14
14
|
|
15
15
|
s.add_dependency "sinatra", "~> 1.3"
|
data/spec/dummy/app.rb
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
require 'sinatra/param'
|
3
|
-
require '
|
3
|
+
require 'date'
|
4
4
|
require 'time'
|
5
|
+
require 'json'
|
5
6
|
|
6
7
|
class App < Sinatra::Base
|
7
8
|
helpers Sinatra::Param
|
8
9
|
|
10
|
+
set :show_exceptions, false
|
11
|
+
set :raise_errors, true
|
12
|
+
|
9
13
|
before do
|
10
14
|
content_type :json
|
11
15
|
end
|
@@ -182,4 +186,9 @@ class App < Sinatra::Base
|
|
182
186
|
message: 'OK'
|
183
187
|
}.to_json
|
184
188
|
end
|
189
|
+
|
190
|
+
get '/raise' do
|
191
|
+
param :arg, String, required: true, raise: true
|
192
|
+
params.to_json
|
193
|
+
end
|
185
194
|
end
|
@@ -99,6 +99,24 @@ describe 'Parameter Types' do
|
|
99
99
|
parsed_body['arg'].should eq(%w(1 2 3 4 5))
|
100
100
|
end
|
101
101
|
end
|
102
|
+
|
103
|
+
it 'coerces arrays of size 1' do
|
104
|
+
get('/coerce/array', arg: '1') do |response|
|
105
|
+
response.status.should == 200
|
106
|
+
parsed_body = JSON.parse(response.body)
|
107
|
+
parsed_body['arg'].should be_an(Array)
|
108
|
+
parsed_body['arg'].should eq(%w(1))
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'coerces arrays with arg[] style' do
|
113
|
+
get('/coerce/array', 'arg[]' => ['1','2','3','4','5']) do |response|
|
114
|
+
response.status.should == 200
|
115
|
+
parsed_body = JSON.parse(response.body)
|
116
|
+
parsed_body['arg'].should be_an(Array)
|
117
|
+
parsed_body['arg'].should eq(%w(1 2 3 4 5))
|
118
|
+
end
|
119
|
+
end
|
102
120
|
end
|
103
121
|
|
104
122
|
describe 'Hash' do
|
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.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattt Thompson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -143,8 +143,10 @@ files:
|
|
143
143
|
- ./sinatra-param-1.1.0.gem
|
144
144
|
- ./sinatra-param-1.1.1.gem
|
145
145
|
- ./sinatra-param-1.1.2.gem
|
146
|
+
- ./sinatra-param-1.2.0.gem
|
146
147
|
- ./sinatra-param.gemspec
|
147
148
|
- spec/dummy/app.rb
|
149
|
+
- spec/parameter_raise_spec.rb
|
148
150
|
- spec/parameter_sets_spec.rb
|
149
151
|
- spec/parameter_spec.rb
|
150
152
|
- spec/parameter_transformations_spec.rb
|
@@ -174,9 +176,10 @@ rubyforge_project:
|
|
174
176
|
rubygems_version: 2.1.11
|
175
177
|
signing_key:
|
176
178
|
specification_version: 4
|
177
|
-
summary: Parameter
|
179
|
+
summary: Parameter Validation & Type Coercion for Sinatra.
|
178
180
|
test_files:
|
179
181
|
- spec/dummy/app.rb
|
182
|
+
- spec/parameter_raise_spec.rb
|
180
183
|
- spec/parameter_sets_spec.rb
|
181
184
|
- spec/parameter_spec.rb
|
182
185
|
- spec/parameter_transformations_spec.rb
|