sinatra-params 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/lib/sinatra/params.rb +42 -0
- data/lib/sinatra/params/version.rb +5 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d3eb33a310c2c526d9946d9c35fec30bdde4ccee
|
4
|
+
data.tar.gz: d229877f2ed8a12e63bbc810b8b190e6c0562196
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 38be8425ff75f30a7f3ff814ebfec1b2a3d16813d6038ee97f32aebb501df54e4174e4cf9964f9bd9069e6e5e50684e98f0cf1bcdb364b531a20b987795a446b
|
7
|
+
data.tar.gz: 8c3e4fa987d68d6119e5e07f51f1ca2aeb852447cc0ba162c5d90b0d412207217f22d0a152d0916e410742c07c1372265c9a4b2ae407a43373e0fd9759733317
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +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.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Sinatra::Params
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sinatra/params`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'sinatra-params'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install sinatra-params
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'sinatra'
|
27
|
+
require 'sinatra-params'
|
28
|
+
|
29
|
+
helper Sinatra::Params
|
30
|
+
|
31
|
+
get '/' do
|
32
|
+
param :id, Integer
|
33
|
+
param :msg, String
|
34
|
+
|
35
|
+
"#{params.id}: #{params.msg}"
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Or if you are using Sinatra::Base
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'sinatra/base'
|
43
|
+
require 'sinatra/params'
|
44
|
+
|
45
|
+
class Application < Sinatra::Base
|
46
|
+
helper Sinatra::Params
|
47
|
+
|
48
|
+
get '/' do
|
49
|
+
param :id, Integer
|
50
|
+
param :msg, String
|
51
|
+
|
52
|
+
"#{params.id}: #{params.msg}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## License
|
58
|
+
|
59
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module Params
|
5
|
+
@respond_as_table = {
|
6
|
+
String => :to_s,
|
7
|
+
Integer => :to_i
|
8
|
+
}
|
9
|
+
|
10
|
+
@convert_table = {
|
11
|
+
String => {
|
12
|
+
Array => Proc.new { |input| input.split(%r{,\s*}) },
|
13
|
+
Date => Proc.new { |input| Date.parse(input) },
|
14
|
+
DateTime => Proc.new { |input| DateTime.parse(input) }
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
def respond_as
|
19
|
+
@respond_as_table
|
20
|
+
end
|
21
|
+
|
22
|
+
def convert
|
23
|
+
@convert_table
|
24
|
+
end
|
25
|
+
|
26
|
+
def param (name, type, args = {})
|
27
|
+
raise "Parameter '#{name}' missing" unless params.key? name
|
28
|
+
|
29
|
+
define_singleton_method(name) { params[name] }
|
30
|
+
|
31
|
+
if params[name].class == type
|
32
|
+
# Do nothing
|
33
|
+
elsif params[name].respond_to? respond_as[type]
|
34
|
+
params[name] = params[name].send(respond_as[type])
|
35
|
+
elsif convert[params[name].class] and convert[params[name].class].key? type
|
36
|
+
params[name] = convert[params[name].class][type].call(params[name])
|
37
|
+
else
|
38
|
+
raise "Parameter '#{name}' not found"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Felipe Cabrera
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- fecabrera@ondasonora.cl
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE.txt
|
36
|
+
- README.md
|
37
|
+
- lib/sinatra/params.rb
|
38
|
+
- lib/sinatra/params/version.rb
|
39
|
+
homepage:
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.6.14.1
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Parameter validation for Sinatra.
|
63
|
+
test_files: []
|