sinatra-param 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param (0.0.1)
4
+ sinatra-param (0.0.2)
5
5
  sinatra (~> 1.3)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # sinatra-param
2
- _Parameter Validation and Typecasting for Sinatra_
2
+ _Parameter Contracts for Sinatra_
3
3
 
4
4
  REST conventions takes the guesswork out of designing and consuming web APIs. `GET` / `POST` / `PATCH` / `DELETE` resource endpoints and you get what you'd expect.
5
5
 
@@ -36,12 +36,12 @@ end
36
36
 
37
37
  By declaring parameter types, incoming parameters will automatically be transformed into an object of that type. For instance, if a param is `Boolean`, values of `'1'`, `'true'`, `'t'`, `'yes'`, and `'y'` will be automatically transformed into `true`.
38
38
 
39
- - String
40
- - Integer
41
- - Float
42
- - Boolean _("1/0", "true/false", "t/f", "yes/no", "y/n")_
43
- - Array _("1,2,3,4,5")_
44
- - Hash _(key1:value1,key2:value2)_
39
+ - `String`
40
+ - `Integer`
41
+ - `Float`
42
+ - `Boolean` _("1/0", "true/false", "t/f", "yes/no", "y/n")_
43
+ - `Array` _("1,2,3,4,5")_
44
+ - `Hash` _(key1:value1,key2:value2)_
45
45
 
46
46
  ### Validations
47
47
 
@@ -65,6 +65,8 @@ Use the `transform` option to take even more of the business logic of parameter
65
65
 
66
66
  - Another pain point is the awkward way parameters are passed as JSON in HTTP bodies. I'd love to see an elegant, unobtrusive way to do this automatically.
67
67
 
68
+ - Support for Rails-style Arrays (`'key[]=value1&key[]=value2'`) and Hashes (`'key[a]=value1&key[b]=value2`). /via [@manton](https://twitter.com/#!/manton)
69
+
68
70
  - Testing. This will happen soon.
69
71
 
70
72
  ## Contact
data/lib/sinatra/param.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  require 'sinatra/base'
2
+ require 'time'
3
+ require 'date'
2
4
 
3
5
  module Sinatra
4
6
  module Param
5
- VERSION = "0.0.1"
7
+ VERSION = "0.0.2"
6
8
 
7
9
  class InvalidParameterError < StandardError; end
8
10
 
@@ -13,7 +15,7 @@ module Sinatra
13
15
  validate!(params[name], options)
14
16
  rescue
15
17
  error = "Invalid parameter, #{name}"
16
- if content_type.match(mime_type(:json))
18
+ if content_type && content_type.match(mime_type(:json))
17
19
  error = {message: error}.to_json
18
20
  end
19
21
 
@@ -23,48 +25,51 @@ module Sinatra
23
25
 
24
26
  private
25
27
 
26
- def coerce(param, type, options = {})
27
- return nil if param.nil?
28
- return Integer(param) if type == Integer
29
- return Float(param) if type == Float
30
- return String(param) if type == String
31
- return Array(param.split(options[:delimiter] || ",")) if type == Array
32
- return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
33
- return ((/(false|f|no|n|0)$/i === param) ? false : (/(true|t|yes|y|1)$/i === param) ? true : nil) if type == Boolean
34
- return nil
35
- end
28
+ def coerce(param, type, options = {})
29
+ return nil if param.nil?
30
+ return Integer(param) if type == Integer
31
+ return Float(param) if type == Float
32
+ return String(param) if type == String
33
+ return Time.parse(param) if type == Time
34
+ return Date.parse(param) if type == Date
35
+ return DateTime.parse(param) if type == DateTime
36
+ return Array(param.split(options[:delimiter] || ",")) if type == Array
37
+ return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
38
+ return ((/(false|f|no|n|0)$/i === param) ? false : (/(true|t|yes|y|1)$/i === param) ? true : nil) if type == TrueClass || type == FalseClass || type == :boolean
39
+ return nil
40
+ end
36
41
 
37
- def validate!(param, options)
38
- options.each do |key, value|
39
- case key
40
- when :required
41
- raise InvalidParameterError if value && param.nil?
42
- when :blank
43
- raise InvalidParameterError if !value && case param
44
- when String
45
- !(/\S/ === param)
46
- when Array, Hash
47
- param.empty?
48
- else
49
- param.nil?
42
+ def validate!(param, options)
43
+ options.each do |key, value|
44
+ case key
45
+ when :required
46
+ raise InvalidParameterError if value && param.nil?
47
+ when :blank
48
+ raise InvalidParameterError if !value && case param
49
+ when String
50
+ !(/\S/ === param)
51
+ when Array, Hash
52
+ param.empty?
53
+ else
54
+ param.nil?
55
+ end
56
+ when :is
57
+ raise InvalidParameterError unless value === param
58
+ when :in, :within, :range
59
+ raise InvalidParameterError unless param.nil? || case value
60
+ when Range
61
+ value.include?(param)
62
+ else
63
+ Array(value).include?(param)
50
64
  end
51
- when :is
52
- raise InvalidParameterError unless value === param
53
- when :in, :within, :range
54
- raise InvalidParameterError unless case value
55
- when Range
56
- value.include?(param)
57
- else
58
- Array(value).include?(param)
59
- end
60
- when :min
61
- raise InvalidParameterError unless value <= param
62
- when :max
63
- raise InvalidParameterError unless value >= param
64
- end
65
+ when :min
66
+ raise InvalidParameterError unless param.nil? || value <= param
67
+ when :max
68
+ raise InvalidParameterError unless param.nil? || value >= param
65
69
  end
66
70
  end
67
71
  end
72
+ end
68
73
 
69
74
  helpers Param
70
75
  end
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-param
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-30 00:00:00.000000000Z
12
+ date: 2012-06-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
16
- requirement: &70189725114120 !ruby/object:Gem::Requirement
16
+ requirement: &70267098070800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70189725114120
24
+ version_requirements: *70267098070800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70189725113620 !ruby/object:Gem::Requirement
27
+ requirement: &70267098069940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.6.1
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70189725113620
35
+ version_requirements: *70267098069940
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70189725113160 !ruby/object:Gem::Requirement
38
+ requirement: &70267098067980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 0.9.2
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70189725113160
46
+ version_requirements: *70267098067980
47
47
  description: Parameter Contracts for Sinatra
48
48
  email: m@mattt.me
49
49
  executables: []
@@ -61,6 +61,7 @@ files:
61
61
  - ./LICENSE
62
62
  - ./Rakefile
63
63
  - ./README.md
64
+ - ./sinatra-param-0.0.1.gem
64
65
  - ./sinatra-param.gemspec
65
66
  homepage: http://github.com/mattt/sinatra-param
66
67
  licenses: []
@@ -76,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
77
  version: '0'
77
78
  segments:
78
79
  - 0
79
- hash: 4481802987950945093
80
+ hash: 567877916880817740
80
81
  required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  none: false
82
83
  requirements:
@@ -85,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
86
  version: '0'
86
87
  segments:
87
88
  - 0
88
- hash: 4481802987950945093
89
+ hash: 567877916880817740
89
90
  requirements: []
90
91
  rubyforge_project:
91
92
  rubygems_version: 1.8.15