rails_param 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -6
- data/lib/rails_param/param.rb +4 -12
- data/lib/rails_param/version.rb +1 -1
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 49836118ba306758e4157e7466e8ec67d79222b9
|
4
|
+
data.tar.gz: 48dde691b94c3e089b74e7f8d7279414b53ecf25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d92e097419c2abd3f19c000c39485ec59aba60f214b6266ea25d9f2241aa091250f1e11e4b64cbf8621c621f2c038909e9b7826f162fee1306a4aa6a5683e6d1
|
7
|
+
data.tar.gz: 20a00f69034b111898d91ba86da224e01637bdc13b6ce1a89e316f8bbfcd5364dfbc8238dfbf111c85c191857285fb58d3787c190defa939ab0cb10a741728ee
|
data/README.md
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
# rails-param
|
2
2
|
_Parameter Validation & Type Coercion for Rails_
|
3
3
|
|
4
|
+
[![Build Status](https://travis-ci.org/nicolasblanco/rails_param.svg?branch=master)](https://travis-ci.org/nicolasblanco/rails_param)
|
5
|
+
|
4
6
|
This is a port of the gem [sinatra-param](https://github.com/mattt/sinatra-param) for the Rails framework.
|
5
7
|
All the credits go to [@mattt](https://twitter.com/mattt).
|
6
|
-
It has all the features of the sinatra-param gem, I used bang methods (param!
|
8
|
+
It has all the features of the sinatra-param gem, I used bang methods (like param!) to indicate that they are destructive as they change the controller params object and may raise an exception.
|
7
9
|
|
8
10
|
REST conventions take the guesswork out of designing and consuming web APIs. Simply `GET`, `POST`, `PATCH`, or `DELETE` resource endpoints, and you get what you'd expect.
|
9
11
|
|
10
12
|
However, when it comes to figuring out what parameters are expected... well, all bets are off.
|
11
13
|
|
12
|
-
This Rails extension takes a first step to solving this problem on the developer side
|
14
|
+
This Rails extension takes a first step to solving this problem on the developer side.
|
13
15
|
|
14
16
|
**`rails-param` allows you to declare, validate, and transform endpoint parameters as you would in frameworks like [ActiveModel](http://rubydoc.info/gems/activemodel/3.2.3/frames) or [DataMapper](http://datamapper.org/).**
|
15
17
|
|
16
|
-
> Use `rails-param` in combination with [`Rack::PostBodyContentTypeParser` and `Rack::NestedParams`](https://github.com/rack/rack-contrib) to automatically parameterize JSON `POST` bodies and nested parameters.
|
17
|
-
|
18
18
|
## Example
|
19
19
|
|
20
20
|
``` ruby
|
@@ -25,7 +25,7 @@ This Rails extension takes a first step to solving this problem on the developer
|
|
25
25
|
param! :q, String, required: true
|
26
26
|
param! :categories, Array
|
27
27
|
param! :sort, String, default: "title"
|
28
|
-
param! :order, String, in:
|
28
|
+
param! :order, String, in: %w(asc desc), transform: :downcase, default: "asc"
|
29
29
|
param! :price, String, format: "[<\=>]\s*\$\d+"
|
30
30
|
|
31
31
|
{...}
|
@@ -47,7 +47,8 @@ By declaring parameter types, incoming parameters will automatically be transfor
|
|
47
47
|
|
48
48
|
### Validations
|
49
49
|
|
50
|
-
Encapsulate business logic in a consistent way with validations. If a parameter does not satisfy a particular condition,
|
50
|
+
Encapsulate business logic in a consistent way with validations. If a parameter does not satisfy a particular condition, an exception (RailsParam::Param::InvalidParameterError) is raised.
|
51
|
+
You may use the [rescue_from](http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html#method-i-rescue_from) method in your controller to catch this kind of exception.
|
51
52
|
|
52
53
|
- `required`
|
53
54
|
- `blank`
|
data/lib/rails_param/param.rb
CHANGED
@@ -15,21 +15,13 @@ module RailsParam
|
|
15
15
|
params[name] = (options[:default].call if options[:default].respond_to?(:call)) || options[:default] if params[name].nil? and options[:default]
|
16
16
|
params[name] = options[:transform].to_proc.call(params[name]) if params[name] and options[:transform]
|
17
17
|
validate!(params[name], options)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
# raise exception
|
22
|
-
# end
|
23
|
-
|
24
|
-
# error = "Invalid Parameter: #{name}"
|
25
|
-
# if content_type and content_type.match(mime_type(:json))
|
26
|
-
# error = {message: error, errors: {name => exception.message}}.to_json
|
27
|
-
# end
|
28
|
-
|
29
|
-
# # do something with error object
|
18
|
+
rescue InvalidParameterError => exception
|
19
|
+
exception.param, exception.options = name, options
|
20
|
+
raise exception
|
30
21
|
end
|
31
22
|
end
|
32
23
|
|
24
|
+
# TODO: should we reintegrate this method?
|
33
25
|
# def one_of!(*names)
|
34
26
|
# count = 0
|
35
27
|
# names.each do |name|
|
data/lib/rails_param/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_param
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Blanco
|
@@ -44,29 +44,29 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 3.2.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 3.2.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: activesupport
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 3.2.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
69
|
-
description: "\n Parameter Validation
|
68
|
+
version: 3.2.0
|
69
|
+
description: "\n Parameter Validation and Type Coercion for Rails\n "
|
70
70
|
email: nicolas@nicolasblanco.fr
|
71
71
|
executables: []
|
72
72
|
extensions: []
|
@@ -78,7 +78,7 @@ files:
|
|
78
78
|
- lib/rails_param/version.rb
|
79
79
|
homepage: http://github.com/nicolasblanco/rails_param
|
80
80
|
licenses:
|
81
|
-
-
|
81
|
+
- MIT
|
82
82
|
metadata: {}
|
83
83
|
post_install_message:
|
84
84
|
rdoc_options:
|
@@ -100,5 +100,5 @@ rubyforge_project:
|
|
100
100
|
rubygems_version: 2.2.2
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
|
-
summary: Parameter Validation
|
103
|
+
summary: Parameter Validation and Type Coercion for Rails
|
104
104
|
test_files: []
|