sinatra-params 1.0.7 → 1.1.0

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
  SHA256:
3
- metadata.gz: b7bb20c00551b7a793c77c6aae6eedb00e19cbc9de62af23bd0fadde9216ba6d
4
- data.tar.gz: f3afe879522a123c06ea543b73dfadaf07c702f8454099f40093571e3d1d37eb
3
+ metadata.gz: 391ad5968f903b4753d971effc77666adc10ca5b423f4096cd07ccd76b8c23ad
4
+ data.tar.gz: c49017b0823b83f03d63fecebf7a137487e2cbc099706dc7058c015aceaa7e43
5
5
  SHA512:
6
- metadata.gz: 5a5a49dfdb51ebdeb77cc86b04dfcf3c5d61b69dedf36e3ae396b2c8c713373a11493879ed1a357aadbafb994301f7db9331ad961e37c218e90b29fc195d54f1
7
- data.tar.gz: fed02db7066bc0cd4de9b9a7ba89b2168348810b28914190ecd383cf2d6c23e32f11203b4c41ec1bd79c7fc6373d551fdac16e891d8fee3a93c11a189240f322
6
+ metadata.gz: '0699f9646dea7dd0d82e83b1db54b42755df0f19e9b5e47a10145d4e977a4f3b6154e2b9ab21e7f405d2b3601068727ed87b1c8bb0160d8a14abf70eb0434dc7'
7
+ data.tar.gz: 18ca3601563210e9b071c486f8736889cbdd0c8f00242fc274b5025547ba1f748daf66845ec0a7462d2865a9a8eefa835101957e6e17eb5186233a5613de6109
@@ -1,51 +1,24 @@
1
- require 'sinatra/params/configuration'
2
- require 'sinatra/params/error'
3
- require 'sinatra/params/version'
4
-
5
- require 'date'
6
-
7
- module Conversion
8
- attr_accessor :respond_as, :convert
9
- extend self
10
- end
11
-
12
- Conversion.respond_as = {
13
- String => :to_s,
14
- Integer => :to_i
15
- }
16
-
17
- Conversion.convert = {
18
- String => {
19
- Array => Proc.new { |input| input.split(%r{,\s*}) },
20
- Date => Proc.new { |input| Date.parse(input) },
21
- DateTime => Proc.new { |input| DateTime.parse(input) }
22
- }
23
- }
24
-
25
- module Sinatra
26
- module Params
27
- def param (name, type, args = {})
28
- params.define_singleton_method(name.to_sym) { self[name] }
29
-
30
- present = !params[name].nil?
31
-
32
- raise ParameterNotFoundError(name) unless present or args[:optional]
33
-
34
- if params[name].class == type
35
- # Do nothing
36
- elsif Conversion.respond_as[type] and params[name].respond_to? Conversion.respond_as[type]
37
- params[name] = params[name].send(Conversion.respond_as[type])
38
- elsif Conversion.convert[params[name].class] and Conversion.convert[params[name].class].key? type
39
- params[name] = Conversion.convert[params[name].class][type].call(params[name])
40
- else
41
- raise ParameterNotFoundError(name) unless args[:optional]
42
- end
43
-
44
- return unless present
45
-
46
- raise ParameterOutOfRangeError(name) if args[:in] and not args[:in].include? params[name]
47
- raise ParameterOutOfRangeError(name) if args[:max] and args[:max] < params[name]
48
- raise ParameterOutOfRangeError(name) if args[:min] and params[name] < args[:min]
49
- end
50
- end
51
- end
1
+ require 'sinatra/params/string'
2
+ require 'sinatra/params/integer'
3
+ require 'sinatra/params/float'
4
+ require 'sinatra/params/date'
5
+ require 'sinatra/params/time'
6
+ require 'sinatra/params/datetime'
7
+ require 'sinatra/params/errors'
8
+
9
+ module Sinatra
10
+ module Params
11
+ def param(name, type, opts = {})
12
+ params.define_singleton_method(name) { self[name] }
13
+
14
+ raise ParameterNotPresentException.new(name) unless opts[:optional?] or not params[name].nil?
15
+
16
+ source_type = params[name].class
17
+ params[name] = params[name].send(source_type.convert_to(type))
18
+
19
+ raise ParameterOutOfRangeException.new(name, :min) unless opts[:min].nil? or params[name] >= opts[:min]
20
+ raise ParameterOutOfRangeException.new(name, :max) unless opts[:max].nil? or params[name] <= opts[:max]
21
+ raise ParameterOutOfRangeException.new(name, :in) unless opts[:in].nil? or opts[:in].include? params[name]
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ require 'date'
2
+
3
+ class Array
4
+ def self.convert_to(type)
5
+ table = {
6
+ Array => :to_a,
7
+ String => :to_s,
8
+ Date => :to_date,
9
+ DateTime => :to_datetime
10
+ }
11
+
12
+ table[type]
13
+ end
14
+
15
+ def to_date
16
+ Date.parse(self.join('-'))
17
+ end
18
+
19
+ def to_datetime
20
+ DateTime.parse(self.join('-'))
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'date'
2
+
3
+ class Date
4
+ def self.convert_to(type)
5
+ table = {
6
+ String => :to_s,
7
+ Integer => :to_i,
8
+ Float => :to_f,
9
+ Array => :to_a,
10
+ Date => :to_date,
11
+ DateTime => :to_datetime,
12
+ Time => :to_time
13
+ }
14
+
15
+ table[type]
16
+ end
17
+
18
+ def to_i
19
+ self.to_time.to_i
20
+ end
21
+
22
+ def to_f
23
+ self.to_time.to_f
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require 'date'
2
+
3
+ class DateTime
4
+ def self.convert_to(type)
5
+ table = {
6
+ String => :to_s,
7
+ Integer => :to_i,
8
+ Float => :to_f,
9
+ Array => :to_a,
10
+ Date => :to_date,
11
+ DateTime => :to_datetime,
12
+ Time => :to_time
13
+ }
14
+
15
+ table[type]
16
+ end
17
+
18
+ def to_i
19
+ self.to_time.to_i
20
+ end
21
+
22
+ def to_f
23
+ self.to_time.to_f
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ class ParameterOutOfRangeException < StandardError
2
+ attr_reader :message
3
+
4
+ def initialize(name, type)
5
+ case type
6
+ when :min
7
+ @message = "Parameter `#{name}` is less than the minimum permitted"
8
+ when :max
9
+ @message = "Parameter `#{name}` is more than the maximum permitted"
10
+ when :in
11
+ @message = "Parameter `#{name}` is not in the list of possible values"
12
+ end
13
+ end
14
+ end
15
+
16
+ class ParameterNotPresentException < StandardError
17
+ attr_reader :message
18
+
19
+ def initialize(name)
20
+ @message = "Parameter `#{name}` is not present"
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ require 'date'
2
+
3
+ class Float
4
+ def self.convert_to(type)
5
+ table = {
6
+ String => :to_s,
7
+ Integer => :to_i,
8
+ Float => :to_f,
9
+ Time => :to_time,
10
+ Date => :to_date,
11
+ DateTime => :to_datetime
12
+ }
13
+
14
+ table[type]
15
+ end
16
+
17
+ def to_time
18
+ Time.at(self)
19
+ end
20
+
21
+ def to_date
22
+ Time.at(self).to_date
23
+ end
24
+
25
+ def to_datetime
26
+ Time.at(self).to_datetime
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ require 'date'
2
+
3
+ class Integer
4
+ def self.convert_to(type)
5
+ table = {
6
+ String => :to_s,
7
+ Integer => :to_i,
8
+ Float => :to_f,
9
+ Time => :to_time,
10
+ Date => :to_date,
11
+ DateTime => :to_datetime
12
+ }
13
+
14
+ table[type]
15
+ end
16
+
17
+ def to_time
18
+ Time.at(self)
19
+ end
20
+
21
+ def to_date
22
+ Time.at(self).to_date
23
+ end
24
+
25
+ def to_datetime
26
+ Time.at(self).to_datetime
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ require 'date'
2
+
3
+ class String
4
+ def self.convert_to(type)
5
+ table = {
6
+ String => :to_s,
7
+ Symbol => :to_sym,
8
+ Integer => :to_i,
9
+ Float => :to_f,
10
+ Date => :to_date,
11
+ DateTime => :to_datetime,
12
+ Time => :to_time
13
+ }
14
+
15
+ table[type]
16
+ end
17
+
18
+ def to_date
19
+ Date.parse(self)
20
+ end
21
+
22
+ def to_time
23
+ self.to_datetime.to_time
24
+ end
25
+
26
+ def to_datetime
27
+ DateTime.parse(self)
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ require 'date'
2
+
3
+ class Time
4
+ def self.convert_to(type)
5
+ table = {
6
+ String => :to_s,
7
+ Integer => :to_i,
8
+ Float => :to_f,
9
+ Array => :to_a,
10
+ Date => :to_date,
11
+ DateTime => :to_datetime,
12
+ Time => :to_time
13
+ }
14
+
15
+ table[type]
16
+ end
17
+ end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-params
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.1.0
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-30 00:00:00.000000000 Z
11
+ date: 2018-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: sinatra
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: sinatra
28
+ name: sinatra-contrib
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,39 +39,37 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: sinatra-contrib
42
+ name: sinatra
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
- type: :development
48
+ type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description:
56
- email:
57
- - fecabrera@ondasonora.cl
55
+ description: ''
56
+ email: fecabrera0@outlook.com
58
57
  executables: []
59
58
  extensions: []
60
59
  extra_rdoc_files: []
61
60
  files:
62
- - ".gitignore"
63
- - Gemfile
64
- - LICENSE
65
- - README.md
66
61
  - lib/sinatra/params.rb
67
- - lib/sinatra/params/configuration.rb
68
- - lib/sinatra/params/error.rb
69
- - lib/sinatra/params/version.rb
70
- - sinatra-params.gemspec
71
- - test/server_test.rb
62
+ - lib/sinatra/params/array.rb
63
+ - lib/sinatra/params/date.rb
64
+ - lib/sinatra/params/datetime.rb
65
+ - lib/sinatra/params/errors.rb
66
+ - lib/sinatra/params/float.rb
67
+ - lib/sinatra/params/integer.rb
68
+ - lib/sinatra/params/string.rb
69
+ - lib/sinatra/params/time.rb
72
70
  homepage: https://github.com/fecabrera/sinatra-params
73
71
  licenses:
74
- - MIT
72
+ - BSD-2-Clause
75
73
  metadata: {}
76
74
  post_install_message:
77
75
  rdoc_options: []
@@ -89,8 +87,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
87
  version: '0'
90
88
  requirements: []
91
89
  rubyforge_project:
92
- rubygems_version: 2.7.6
90
+ rubygems_version: 2.7.7
93
91
  signing_key:
94
92
  specification_version: 4
95
- summary: Parameter validation for Sinatra.
93
+ summary: Parameter validation extension for Sinatra
96
94
  test_files: []
data/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- .idea/
2
- Gemfile.lock
3
- *.gem
data/Gemfile DELETED
@@ -1,3 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
data/LICENSE DELETED
@@ -1,21 +0,0 @@
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 DELETED
@@ -1,55 +0,0 @@
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 :msg, String
31
-
32
- params.msg
33
- end
34
- ```
35
-
36
- Or if you are using Sinatra::Base
37
-
38
- ```ruby
39
- require 'sinatra/base'
40
- require 'sinatra/params'
41
-
42
- class Application < Sinatra::Base
43
- helpers Sinatra::Params
44
-
45
- get '/' do
46
- param :msg, String
47
-
48
- params.msg
49
- end
50
- end
51
- ```
52
-
53
- ## License
54
-
55
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,8 +0,0 @@
1
- module Sinatra
2
- module Params
3
- module Configuration
4
- attr_accessor :lang
5
- extend self
6
- end
7
- end
8
- end
@@ -1,16 +0,0 @@
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 +0,0 @@
1
- module Sinatra
2
- module Params
3
- VERSION = '1.0.7'
4
- end
5
- end
@@ -1,24 +0,0 @@
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
data/test/server_test.rb DELETED
@@ -1,10 +0,0 @@
1
- require 'sinatra'
2
- require 'sinatra/json'
3
- require 'sinatra/params'
4
-
5
- helpers Sinatra::Params
6
-
7
- post '/' do
8
- param :type, String, :in => ['flex', 'fixed']
9
-
10
- end