sinatra-params 1.0.3 → 1.0.4

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: 3b739a97d63e9480304313235b59510e05286c05
4
- data.tar.gz: 5fbce8cb5d98d1755fd100183db52043651b0221
3
+ metadata.gz: bda631e901b268d9cba511c671088dc909f06d69
4
+ data.tar.gz: 12d04e02ba4eb00bf0a263b300414d7efedd462e
5
5
  SHA512:
6
- metadata.gz: 970d2115fc6ddfdbec8783569799739d00e65455cded3f0d8b570022d59ee6edff44e4252f0e02d3391460fb770e197a69a7e540165c15e701265ffd8bedd9f8
7
- data.tar.gz: 8d5886e159941cba17bb14d71e6d0c729dff1b98682f318881de86c9a9b74749b7ebae79ecd9e91c96708a64a3ab3fcdde879ddf47fa41976de5293a2f8c6b56
6
+ metadata.gz: c3385f06b2b369f652073fcf806b607ab65ee6a05333260c500359ebf4e5c3ff938c228cb2cbc518ec3ff6a2153fd06d4902ea1d908433e4278a781a821a5f31
7
+ data.tar.gz: '07848feccca292e83e2ed724693adf9b39f9dd1317ef2178cfc2ad89f90c50f42cf3656e4dadea529efdec03cd5fe2dc26637949c1fcb7a2691e1c477bbe8ee4'
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .idea/
2
+ Gemfile.lock
3
+ *.gem
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
@@ -27,10 +27,9 @@ require 'sinatra/params'
27
27
  helpers Sinatra::Params
28
28
 
29
29
  get '/' do
30
- param :id, Integer
31
30
  param :msg, String
32
31
 
33
- "#{params.id}: #{params.msg}"
32
+ params.msg
34
33
  end
35
34
  ```
36
35
 
@@ -44,10 +43,9 @@ class Application < Sinatra::Base
44
43
  helpers Sinatra::Params
45
44
 
46
45
  get '/' do
47
- param :id, Integer
48
46
  param :msg, String
49
47
 
50
- "#{params.id}: #{params.msg}"
48
+ params.msg
51
49
  end
52
50
  end
53
51
  ```
@@ -1,16 +1,8 @@
1
- require 'date'
2
-
3
- class ParameterNotFoundError < StandardError
4
- def initialize
5
- super('Parameter not found')
6
- end
7
- end
1
+ require 'sinatra/params/configuration'
2
+ require 'sinatra/params/error'
3
+ require 'sinatra/params/version'
8
4
 
9
- class ParameterOutOfRangeError < StandardError
10
- def initialize
11
- super('Parameter out of range')
12
- end
13
- end
5
+ require 'date'
14
6
 
15
7
  module Conversion
16
8
  attr_accessor :respond_as, :convert
@@ -33,25 +25,23 @@ Conversion.convert = {
33
25
  module Sinatra
34
26
  module Params
35
27
  def param (name, type, args = {})
36
- raise ParameterNotFoundError unless params.key? name
28
+ raise ParameterNotFoundError(name) unless params.key? name
37
29
 
38
30
  params.define_singleton_method(name.to_sym) { self[name] }
39
31
 
40
- p Conversion.respond_as
41
-
42
32
  if params[name].class == type
43
33
  # Do nothing
44
- elsif params[name].respond_to? Conversion.respond_as[type]
34
+ elsif Conversion.respond_as[type] and params[name].respond_to? Conversion.respond_as[type]
45
35
  params[name] = params[name].send(Conversion.respond_as[type])
46
36
  elsif Conversion.convert[params[name].class] and Conversion.convert[params[name].class].key? type
47
37
  params[name] = Conversion.convert[params[name].class][type].call(params[name])
48
38
  else
49
- raise ParameterNotFoundError unless args[:optional]
39
+ raise ParameterNotFoundError(name) unless args[:optional]
50
40
  end
51
41
 
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
42
+ raise ParameterOutOfRangeError(name) if args[:in] and args[:in].include? params[name]
43
+ raise ParameterOutOfRangeError(name) if args[:max] and args[:max] < params[name]
44
+ raise ParameterOutOfRangeError(name) if args[:min] and args[:min] > params[name]
55
45
  end
56
46
  end
57
47
  end
@@ -0,0 +1,8 @@
1
+ module Sinatra
2
+ module Params
3
+ module Configuration
4
+ attr_accessor :lang
5
+ extend self
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,16 @@
1
+ class ParameterValidationError < StandardError
2
+ attr_reader :param, :message
3
+
4
+ def initialize(msg, param)
5
+ @param = param
6
+ @message = msg.sub('{param}', param)
7
+ end
8
+ end
9
+
10
+ def ParameterNotFoundError(param)
11
+ ParameterValidationError.new 'Parameter \'{param}\' not found', param.to_s
12
+ end
13
+
14
+ def ParameterOutOfRangeError(param)
15
+ ParameterValidationError.new 'Parameter \'{param}\' out of range', param.to_s
16
+ end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module Params
3
- VERSION = '1.0.3'
3
+ VERSION = '1.0.4'
4
4
  end
5
5
  end
@@ -0,0 +1,24 @@
1
+ require 'find'
2
+
3
+ lib = File.expand_path('lib')
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'sinatra/params/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'sinatra-params'
10
+ spec.version = Sinatra::Params::VERSION
11
+ spec.authors = ['Felipe Cabrera']
12
+ spec.email = ['fecabrera@ondasonora.cl']
13
+
14
+ spec.summary = %q{Parameter validation for Sinatra.}
15
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
16
+ spec.homepage = 'https://github.com/fecabrera/sinatra-params'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'sinatra'
23
+ spec.add_development_dependency 'sinatra-contrib'
24
+ end
@@ -0,0 +1,12 @@
1
+ require 'sinatra'
2
+ require 'sinatra/json'
3
+ require 'sinatra/params'
4
+
5
+ helpers Sinatra::Params
6
+
7
+ post '/' do
8
+ param :content, String
9
+ param :birthday, Date, :max => Date.today.prev_year(18)
10
+
11
+ json :content => params.content
12
+ 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.3
4
+ version: 1.0.4
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-12 00:00:00.000000000 Z
11
+ date: 2018-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,11 +59,16 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - ".gitignore"
62
63
  - Gemfile
63
64
  - LICENSE
64
65
  - README.md
65
66
  - lib/sinatra/params.rb
67
+ - lib/sinatra/params/configuration.rb
68
+ - lib/sinatra/params/error.rb
66
69
  - lib/sinatra/params/version.rb
70
+ - sinatra-params.gemspec
71
+ - test/server_test.rb
67
72
  homepage: https://github.com/fecabrera/sinatra-params
68
73
  licenses:
69
74
  - MIT