require_params 0.0.2 → 0.0.3

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: 75c998b48e9072ddd92bd225f4565accf8bfd4ac
4
- data.tar.gz: 9336380aa0a414434867642e389f381d8376bae7
3
+ metadata.gz: 0b3dfa28e4c406d3ad1841f6e712ba323127c4e0
4
+ data.tar.gz: b48abd42fbb806d276602b74e0d950c87172ad34
5
5
  SHA512:
6
- metadata.gz: d8818d675b8d04142b1fc600193d26cf94e262ae137832645a9ea59a6000575e1b4f761d6a8c3f65a11e490f3c644595813142baabe0db2c815829f9e67d1b25
7
- data.tar.gz: fdaafb00511f4699d1711db6ff89c5290b293344933e5505570d136c232e22891608de83b92fea6076515053103cc1a064b45888c86d23c6b7f6596e5c837e9e
6
+ metadata.gz: 479e8f2b3eebad8bacc71cebeac2e0089eac664966c0a5934186120d64fac3dce8ea385f6c0d614f4e35850cae0c6ac885ee113a81516d54fdbc03ff22d417f3
7
+ data.tar.gz: 36231c2d7765dbba05b9aed107c054861f80db66790a5b75af0912c6d423a903af0aa14f6eb7ae7627e664cd75768860fbfdd8f18a15b966e7a28e4f39265ab6
data/README.md CHANGED
@@ -22,17 +22,41 @@ class UsersController < ApplicationController
22
22
  end
23
23
  ```
24
24
 
25
+ `require_params` can be called multiple times for each actions:
26
+
27
+ ```ruby
28
+ class UsersController < ApplicationController
29
+ require_params [:username, :password]
30
+ require_params [:email], only: :create
31
+
32
+ # Equivalent to calling
33
+ # require_params [:username, :password, :email], only: :create
34
+ # require_params [:username, :password], except: :create
35
+
36
+ def create
37
+ # Make a new user
38
+ end
39
+
40
+ def sign_in
41
+ # Signs in user
42
+ end
43
+ end
44
+ ```
45
+
25
46
  If any one of the required parameters is missing, your action handler will not be called at all. Instead, an error hash that looks like this will be served with 400 Bad Request:
26
47
 
27
48
  ```ruby
28
49
  {
29
- errors: [{
50
+ errors: {
30
51
  username: ["is missing"],
31
52
  password: ["is missing"]
32
- }]
53
+ }
33
54
  }
34
55
  ```
35
56
 
57
+ ### Internals
58
+ `require_params` is backed by `prepend_before_action`. As a result, it can be guaranteed that by the time your `before_action` hooks and actions are executed, the required parameters are present.
59
+
36
60
  ## Installation
37
61
  Add this line to your application's Gemfile:
38
62
 
@@ -50,5 +74,13 @@ Or install it yourself as:
50
74
  $ gem install require_params
51
75
  ```
52
76
 
77
+ Include `RequireParams` to your controllers:
78
+
79
+ ```ruby
80
+ class ApplicationController < ActionController::API
81
+ include RequireParams
82
+ end
83
+ ```
84
+
53
85
  ## License
54
86
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -1,15 +1,30 @@
1
1
  module RequireParams
2
2
  extend ActiveSupport::Concern
3
3
 
4
+ included do
5
+ prepend_before_action :require_params_error_emitter
6
+ end
7
+
4
8
  module ClassMethods
5
9
  def require_params(required_params, options = {})
6
10
  prepend_before_action options do
7
- missing_params = required_params.reject { |x| params.has_key?(x) }
8
- if missing_params.any?
9
- render json: { errors: missing_params.map { |x| {x => ["is missing"]}} }, status: :bad_request
10
- false
11
+ required_params.each do |x|
12
+ missing_params << x unless params.has_key?(x)
11
13
  end
12
14
  end
13
15
  end
14
16
  end
17
+
18
+ private
19
+
20
+ def missing_params
21
+ @_missing_params ||= []
22
+ end
23
+
24
+ def require_params_error_emitter
25
+ if missing_params.any?
26
+ render json: { errors: Hash[@_missing_params.map { |x| [x, ["is missing"]] }] }, status: :bad_request
27
+ false
28
+ end
29
+ end
15
30
  end
@@ -1,3 +1,3 @@
1
1
  module RequireParams
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: require_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yihang Ho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-26 00:00:00.000000000 Z
11
+ date: 2016-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails