sinatra-params 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -3
- data/LICENSE +21 -21
- data/README.md +57 -57
- data/lib/sinatra/params.rb +57 -41
- data/lib/sinatra/params/version.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b739a97d63e9480304313235b59510e05286c05
|
4
|
+
data.tar.gz: 5fbce8cb5d98d1755fd100183db52043651b0221
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 970d2115fc6ddfdbec8783569799739d00e65455cded3f0d8b570022d59ee6edff44e4252f0e02d3391460fb770e197a69a7e540165c15e701265ffd8bedd9f8
|
7
|
+
data.tar.gz: 8d5886e159941cba17bb14d71e6d0c729dff1b98682f318881de86c9a9b74749b7ebae79ecd9e91c96708a64a3ab3fcdde879ddf47fa41976de5293a2f8c6b56
|
data/Gemfile
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
gemspec
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
data/LICENSE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2018 Felipe Cabrera
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Felipe Cabrera
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,57 +1,57 @@
|
|
1
|
-
# Sinatra::Params
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
Add this line to your application's Gemfile:
|
8
|
-
|
9
|
-
```ruby
|
10
|
-
gem 'sinatra-params'
|
11
|
-
```
|
12
|
-
|
13
|
-
And then execute:
|
14
|
-
|
15
|
-
$ bundle
|
16
|
-
|
17
|
-
Or install it yourself as:
|
18
|
-
|
19
|
-
$ gem install sinatra-params
|
20
|
-
|
21
|
-
## Usage
|
22
|
-
|
23
|
-
```ruby
|
24
|
-
require 'sinatra'
|
25
|
-
require 'sinatra/params'
|
26
|
-
|
27
|
-
helpers Sinatra::Params
|
28
|
-
|
29
|
-
get '/' do
|
30
|
-
param :id, Integer
|
31
|
-
param :msg, String
|
32
|
-
|
33
|
-
"#{params.id}: #{params.msg}"
|
34
|
-
end
|
35
|
-
```
|
36
|
-
|
37
|
-
Or if you are using Sinatra::Base
|
38
|
-
|
39
|
-
```ruby
|
40
|
-
require 'sinatra/base'
|
41
|
-
require 'sinatra/params'
|
42
|
-
|
43
|
-
class Application < Sinatra::Base
|
44
|
-
helpers Sinatra::Params
|
45
|
-
|
46
|
-
get '/' do
|
47
|
-
param :id, Integer
|
48
|
-
param :msg, String
|
49
|
-
|
50
|
-
"#{params.id}: #{params.msg}"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
```
|
54
|
-
|
55
|
-
## License
|
56
|
-
|
57
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
1
|
+
# Sinatra::Params
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sinatra-params'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sinatra-params
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'sinatra'
|
25
|
+
require 'sinatra/params'
|
26
|
+
|
27
|
+
helpers Sinatra::Params
|
28
|
+
|
29
|
+
get '/' do
|
30
|
+
param :id, Integer
|
31
|
+
param :msg, String
|
32
|
+
|
33
|
+
"#{params.id}: #{params.msg}"
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Or if you are using Sinatra::Base
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require 'sinatra/base'
|
41
|
+
require 'sinatra/params'
|
42
|
+
|
43
|
+
class Application < Sinatra::Base
|
44
|
+
helpers Sinatra::Params
|
45
|
+
|
46
|
+
get '/' do
|
47
|
+
param :id, Integer
|
48
|
+
param :msg, String
|
49
|
+
|
50
|
+
"#{params.id}: #{params.msg}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/sinatra/params.rb
CHANGED
@@ -1,41 +1,57 @@
|
|
1
|
-
require 'date'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
end
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class ParameterNotFoundError < StandardError
|
4
|
+
def initialize
|
5
|
+
super('Parameter not found')
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class ParameterOutOfRangeError < StandardError
|
10
|
+
def initialize
|
11
|
+
super('Parameter out of range')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Conversion
|
16
|
+
attr_accessor :respond_as, :convert
|
17
|
+
extend self
|
18
|
+
end
|
19
|
+
|
20
|
+
Conversion.respond_as = {
|
21
|
+
String => :to_s,
|
22
|
+
Integer => :to_i
|
23
|
+
}
|
24
|
+
|
25
|
+
Conversion.convert = {
|
26
|
+
String => {
|
27
|
+
Array => Proc.new { |input| input.split(%r{,\s*}) },
|
28
|
+
Date => Proc.new { |input| Date.parse(input) },
|
29
|
+
DateTime => Proc.new { |input| DateTime.parse(input) }
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
module Sinatra
|
34
|
+
module Params
|
35
|
+
def param (name, type, args = {})
|
36
|
+
raise ParameterNotFoundError unless params.key? name
|
37
|
+
|
38
|
+
params.define_singleton_method(name.to_sym) { self[name] }
|
39
|
+
|
40
|
+
p Conversion.respond_as
|
41
|
+
|
42
|
+
if params[name].class == type
|
43
|
+
# Do nothing
|
44
|
+
elsif params[name].respond_to? Conversion.respond_as[type]
|
45
|
+
params[name] = params[name].send(Conversion.respond_as[type])
|
46
|
+
elsif Conversion.convert[params[name].class] and Conversion.convert[params[name].class].key? type
|
47
|
+
params[name] = Conversion.convert[params[name].class][type].call(params[name])
|
48
|
+
else
|
49
|
+
raise ParameterNotFoundError unless args[:optional]
|
50
|
+
end
|
51
|
+
|
52
|
+
if args[:in] then raise ParameterOutOfRangeError unless args[:in].include? params[name] end
|
53
|
+
if args[:max] then raise ParameterOutOfRangeError unless args[:max] < params[name] end
|
54
|
+
if args[:min] then raise ParameterOutOfRangeError unless args[:min] > params[name] end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
module Sinatra
|
2
|
-
module Params
|
3
|
-
VERSION = '1.0.
|
4
|
-
end
|
5
|
-
end
|
1
|
+
module Sinatra
|
2
|
+
module Params
|
3
|
+
VERSION = '1.0.3'
|
4
|
+
end
|
5
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felipe Cabrera
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|