explicit-parameters 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6bd7f4d53421eeef0b59aef942588c4796600ccc
4
- data.tar.gz: df62f44750a434068d5ee6dc03d19805a4b736db
3
+ metadata.gz: ec2817ab57ba4b8b63d011702245fa8114de7a63
4
+ data.tar.gz: fead56598d7502cf39395af308febc40f148cd0d
5
5
  SHA512:
6
- metadata.gz: fab71e754354bfb120bd38a17e08f7d16af14e5b6dbc240a55ab3d95b785d83a86d81bf12f293d3ea5dd213d678f6bc1f82ba08d519a94deb32565b9df658836
7
- data.tar.gz: 6180cc1a0871ca98b71a40b1f5ab8ce317e558fbe48f28961213981e04f929b5e040dc5e08dc5b4c39d667c62b8637ca5e49b84e544d28ec1aac7b4048cfad28
6
+ metadata.gz: 7840be0a953f0ede081f10539d918f2486ee899f86d3075e919a8db0212731f586cf49352b6f5423012938d96b28ba33fe0bf6140a98d2b5a873eef7e2b406d4
7
+ data.tar.gz: 2f8cfc543b901b3c64c6f41080cbe18f7b76cab75464e11f8741f48af55059eeba1edd451df2c0459eca385d1b6c9351c7e5fc11d2b2f83461f2889758f0d4c0
data/README.md CHANGED
@@ -11,7 +11,7 @@ Explicit parameters validation and casting for Rails APIs.
11
11
  Add this line to your application's Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'explicit_parameters'
14
+ gem 'explicit-parameters'
15
15
  ```
16
16
 
17
17
  And then execute:
@@ -9,7 +9,6 @@ module ExplicitParameters
9
9
  end
10
10
 
11
11
  included do
12
- self.parameters = {}
13
12
  rescue_from ExplicitParameters::InvalidParameters, with: :render_parameters_error
14
13
  end
15
14
 
@@ -18,6 +17,7 @@ module ExplicitParameters
18
17
 
19
18
  def method_added(action)
20
19
  return unless Controller.last_parameters
20
+ self.parameters ||= {}
21
21
  parameters[action.to_s] = Controller.last_parameters
22
22
  const_set("#{action.to_s.camelize}Parameters", Controller.last_parameters)
23
23
  Controller.last_parameters = nil
@@ -28,7 +28,7 @@ module ExplicitParameters
28
28
  end
29
29
 
30
30
  def parse_parameters_for(action_name, params)
31
- if declaration = parameters[action_name]
31
+ if declaration = parameters.try!(:[], action_name)
32
32
  declaration.parse!(params)
33
33
  else
34
34
  params
@@ -36,12 +36,20 @@ module ExplicitParameters
36
36
  end
37
37
  end
38
38
 
39
- private
40
-
41
39
  def params
42
40
  @validated_params ||= self.class.parse_parameters_for(action_name, super)
43
41
  end
44
42
 
43
+ private
44
+
45
+ def param_error!(parameter, error)
46
+ raise ExplicitParameters::InvalidParameters.new({errors: {parameter => [error]}}.to_json)
47
+ end
48
+
49
+ def render_param_error(parameter, error)
50
+ render json: {errors: {parameter => [error]}}, status: :unprocessable_entity
51
+ end
52
+
45
53
  def render_parameters_error(error)
46
54
  render json: error.message, status: :unprocessable_entity
47
55
  end
@@ -1,3 +1,3 @@
1
1
  module ExplicitParameters
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -11,6 +11,11 @@ class DummyController < ActionController::Base
11
11
  render json: {value: params.page_size, type: params.page_size.class.name}
12
12
  end
13
13
 
14
+ def error
15
+ param_error!(:page_size, "Out of bound")
16
+ render text: 'OK'
17
+ end
18
+
14
19
  def no_declaration
15
20
  render text: 'OK'
16
21
  end
@@ -38,6 +43,11 @@ RSpec.describe DummyController do
38
43
  expect(json_response).to be == {errors: {page_size: ['"foobar" is not a valid Integer']}}
39
44
  end
40
45
 
46
+ it 'param_error! interupt the request immediately' do
47
+ get :error
48
+ expect(json_response).to be == {errors: {page_size: ['Out of bound']}}
49
+ end
50
+
41
51
  private
42
52
 
43
53
  attr_reader :response
@@ -9,7 +9,6 @@ RSpec.configure do |config|
9
9
  mocks.verify_partial_doubles = true
10
10
  end
11
11
  config.disable_monkey_patching!
12
- config.warnings = true
13
12
  config.order = :random
14
13
  Kernel.srand config.seed
15
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: explicit-parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-22 00:00:00.000000000 Z
11
+ date: 2015-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack