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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4220d11aae943a4e7b24bf403554d749836423a
4
- data.tar.gz: 050a991ab239f00dba247d3a3a577427889e7ac4
3
+ metadata.gz: 3b739a97d63e9480304313235b59510e05286c05
4
+ data.tar.gz: 5fbce8cb5d98d1755fd100183db52043651b0221
5
5
  SHA512:
6
- metadata.gz: d92342df34606dc20f5164395a8a737e24ace64697420ebdf308c0cea357464852fd8313666f7028c16bb0692f98fdeec8d57adae22b7479aca22bc12cf90288
7
- data.tar.gz: db4b8ce52284d5ad0b9db24edc622ed5e17ca409553132bb32b4ebe96c63222e1e70ecef3e81d93861d4a0bb193b9bd5890b7b46c9f5e80f61eeab6349a1ece3
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).
@@ -1,41 +1,57 @@
1
- require 'date'
2
-
3
- module Conversion
4
- attr_accessor :respond_as, :convert
5
- extend self
6
- end
7
-
8
- Conversion.respond_as = {
9
- String => :to_s,
10
- Integer => :to_i
11
- }
12
-
13
- Conversion.convert = {
14
- String => {
15
- Array => Proc.new { |input| input.split(%r{,\s*}) },
16
- Date => Proc.new { |input| Date.parse(input) },
17
- DateTime => Proc.new { |input| DateTime.parse(input) }
18
- }
19
- }
20
-
21
- module Sinatra
22
- module Params
23
- def param (name, type, args = {})
24
- raise 'Parameter {name} missing'.sub('{name}', name.to_s) unless params.key? name
25
-
26
- params.define_singleton_method(name.to_sym) { self[name] }
27
-
28
- p Conversion.respond_as
29
-
30
- if params[name].class == type
31
- # Do nothing
32
- elsif params[name].respond_to? Conversion.respond_as[type]
33
- params[name] = params[name].send(Conversion.respond_as[type])
34
- elsif Conversion.convert[params[name].class] and Conversion.convert[params[name].class].key? type
35
- params[name] = Conversion.convert[params[name].class][type].call(params[name])
36
- else
37
- raise 'Parameter {name} not found'.sub('{name}', name) unless args[:optional]
38
- end
39
- end
40
- end
41
- end
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.2'
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.2
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-07 00:00:00.000000000 Z
11
+ date: 2018-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler