sinatra-strong-params 0.0.3 → 0.1.0

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
- SHA1:
3
- metadata.gz: 2a395072291663098a8c4e241b4488bacc8e25f1
4
- data.tar.gz: abb9249d19214474b81d7099e67ac6ba833260b9
2
+ SHA256:
3
+ metadata.gz: c3eb2d1f7ec3984115e65b658c4afa7d908dcd7fdae1db169aeb8cc069b0930c
4
+ data.tar.gz: 769138e7be535e8b77015bf981277d02dbad16df0bf113f105e506d097e5f2bf
5
5
  SHA512:
6
- metadata.gz: 4d6a12f59264e4c74e5580049d9ae7217ba9da4f40ca70d2c902fd50a34337729ad11bd6ded8be465c5518872869b60689f5bc7a36c217e5a9a4174d6580845a
7
- data.tar.gz: 70e7dc32a003c0f82f59a06d0e078173afb395490d9bcb03d1ce4d6c1a5926b5c8acecab639838625a9d3530a417a8c841031a8fc7f9282dac59745bc433f56f
6
+ metadata.gz: e4d7402165a277e6ee1cf7b95ee28c6f3d14c00b41637e3efb07e5facd90cb2289020e8349999d29e1cddb5abcd43b2d6547124b2c5db09cb79db50574e2e137
7
+ data.tar.gz: '09d99345d2d18af039df5a5f1935fcb78a682337b11b49bd5f5d76b2e536d3df346b8b300e51e6ff9ca0f4dde5f15c2f8b8580619e09748ac99b79995a826f9d'
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.10
4
+ - 2.3.7
5
+ - 2.4.4
@@ -0,0 +1,33 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2019-01-27
10
+
11
+ ### Added
12
+ - Now you don't need to explicitly allow the needed params anymore when using `needs` and `allows` combined.
13
+
14
+ ### Changed
15
+ - The error message for missing needed params now includes a list of the missing ones.
16
+
17
+ ### Fixed
18
+ - When using `allows`, the params hash keys was changing from string to symbol.
19
+
20
+ ## [0.0.3] - 2017-01-30
21
+
22
+ ### Added
23
+ - Gem test setup and basic tests.
24
+
25
+ ## [0.0.2] - 2015-11-12
26
+
27
+ ### Added
28
+ - Accept `missing_parameter_message` configuration for missing needed params error message.
29
+
30
+ ## [0.0.1] - 2014-07-12
31
+
32
+ ### Added
33
+ - Gem created.
data/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  # Sinatra::StrongParams
2
2
 
3
- A really naive parameter filtering implementation for Sinatra.
4
-
3
+ [![Gem Version](https://badge.fury.io/rb/sinatra-strong-params.svg)](https://badge.fury.io/rb/sinatra-strong-params)
4
+ [![Build Status](https://secure.travis-ci.org/evanleck/sinatra-strong-params.svg)](https://travis-ci.org/evanleck/sinatra-strong-params)
5
5
 
6
+ A really naive parameter filtering implementation for Sinatra.
6
7
 
7
8
  ## Installation
8
9
 
9
10
  Add this line to your application's Gemfile:
10
11
 
11
- gem 'sinatra-strong-params', :require => 'sinatra/strong-params'
12
+ gem 'sinatra-strong-params', require: 'sinatra/strong-params'
12
13
 
13
14
  And then execute:
14
15
 
@@ -18,7 +19,7 @@ Or install it yourself as:
18
19
 
19
20
  $ gem install sinatra-strong-params
20
21
 
21
- If you are using a Modular Sinatra application such as `class FooApp < Sinatra::Base` you must include any desired extensions explicitly within your Sinatra application
22
+ If you are using a Modular Sinatra application such as `class MyApp < Sinatra::Base` you must include any desired extensions explicitly within your Sinatra application:
22
23
 
23
24
  ```ruby
24
25
  register Sinatra::StrongParams
@@ -28,8 +29,6 @@ register Sinatra::StrongParams
28
29
 
29
30
  This gem adds two filters to Sinatra routes: `allows` and `needs`.
30
31
 
31
-
32
-
33
32
  ### Allows
34
33
 
35
34
  A way to whitelist parameters in the request scope.
@@ -40,10 +39,7 @@ get '/', allows: [:id, :action] do
40
39
  end
41
40
  ```
42
41
 
43
- `allows` modifies the parameters available in the request scope, so
44
- beware, though it stashes unmodified params in @_params.
45
-
46
-
42
+ `allows` modifies the parameters available in the request scope keeping just the allowed params.
47
43
 
48
44
  ### Needs
49
45
 
@@ -56,9 +52,9 @@ end
56
52
  ```
57
53
 
58
54
  `needs` does not modify the parameters available to the request scope
59
- and raises a RequiredParamMissing error if a needed param is missing.
55
+ but raises a `RequiredParamMissing` error if a needed param is missing.
60
56
 
61
- Catching a missing parameter:
57
+ Catching a missing parameter error:
62
58
 
63
59
  ```ruby
64
60
  error RequiredParamMissing do
@@ -66,23 +62,20 @@ error RequiredParamMissing do
66
62
  end
67
63
  ```
68
64
 
69
-
70
-
71
- ### Both
65
+ ### Allows and Needs
72
66
 
73
67
  Wanna get super restrictive? Can do.
74
68
 
75
69
  ```ruby
76
- post '/login', allows: [:email, :password], needs: [:email, :password] do
70
+ post '/login', needs: [:email, :password], allows: [:name] do
77
71
  # handle yo business
78
72
  end
79
73
  ```
80
74
 
81
-
82
75
  ## Contributing
83
76
 
84
77
  1. Fork it ( https://github.com/[my-github-username]/sinatra-strong-params/fork )
85
78
  2. Create your feature branch (`git checkout -b my-new-feature`)
86
- 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 3. Commit your changes with tests (`git commit -am 'Add some feature'`)
87
80
  4. Push to the branch (`git push origin my-new-feature`)
88
81
  5. Create a new Pull Request
@@ -23,16 +23,12 @@ module Sinatra
23
23
  unless @params.empty?
24
24
  @_params = @_params || @params # for safety
25
25
  globals = settings.globally_allowed_parameters
26
- passable = (globals | passable).map(&:to_sym) # make sure it's a symbol
26
+ needed = @_needed || []
27
+ passable = (globals | passable | needed).map(&:to_sym) # make sure it's a symbol
27
28
 
28
- # Select only the allowed parameters.
29
- @params = @params.select do |param, _value|
30
- passable.include?(param.to_sym)
31
- end
32
-
33
- # Copy Sinatra's default proc to allow indifferent access.
34
- @params.tap do |params|
35
- params.default_proc = @_params.default_proc.dup rescue nil
29
+ # Keep only the allowed parameters.
30
+ @params = @params.delete_if do |param, _value|
31
+ !passable.include?(param.to_sym)
36
32
  end
37
33
  end
38
34
  end
@@ -50,31 +46,29 @@ module Sinatra
50
46
  #
51
47
  app.set(:needs) do |*needed|
52
48
  condition do
53
- if @params.nil? || @params.empty? && !needed.empty?
54
- fail RequiredParamMissing, settings.missing_parameter_message
55
- else
56
- needed = needed.map(&:to_sym) # make sure it's a symbol
57
- sym_params = @params.dup
49
+ needed = needed.map(&:to_sym) # make sure it's a symbol
50
+ @_needed = needed
51
+ sym_params = @params.dup
58
52
 
59
- # symbolize the keys so we know what we're looking at
60
- sym_params.keys.each do |key|
61
- sym_params[(key.to_sym rescue key) || key] = sym_params.delete(key)
62
- end
53
+ # symbolize the keys so we know what we're looking at
54
+ sym_params.keys.each do |key|
55
+ sym_params[(key.to_sym rescue key) || key] = sym_params.delete(key)
56
+ end
63
57
 
64
- if needed.any? { |key| sym_params[key].nil? || sym_params[key].empty? }
65
- fail RequiredParamMissing, settings.missing_parameter_message
66
- end
58
+ missing_params = needed.select { |key| sym_params[key].nil? || sym_params[key].empty? }
59
+ if missing_params.any?
60
+ fail RequiredParamMissing, "#{settings.missing_parameter_message} #{missing_params.join(', ')}"
67
61
  end
68
62
  end
69
63
  end
70
64
 
71
65
  # These will always pass through the 'allows' method
72
- # and will be mapped to symbols. I often use [:redirect_to, :_csrf] here
73
- # because I always want them to pass through for later processing
66
+ # and will be mapped to symbols. I often use [:redirect_to, :_csrf] here
67
+ # because I always want them to pass through for later processing
74
68
  app.set :globally_allowed_parameters, []
75
69
 
76
70
  # The default message when RequiredParamMissing is raised.
77
- app.set :missing_parameter_message, 'One or more required parameters were missing.'
71
+ app.set :missing_parameter_message, 'One or more required parameters were missing:'
78
72
 
79
73
  # Change the default behavior for missing parameters by overriding this route.
80
74
  # For example...
@@ -2,6 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
  module Sinatra
4
4
  module StrongParams
5
- VERSION = '0.0.3'
5
+ VERSION = '0.1.0'
6
6
  end
7
7
  end
@@ -7,10 +7,9 @@ require 'sinatra/strong-params/version'
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = 'sinatra-strong-params'
9
9
  spec.version = Sinatra::StrongParams::VERSION
10
- spec.authors = ['Evan Lecklider']
11
- spec.email = ['evan@lecklider.com']
12
- spec.summary = 'Some super basic strong parameter filters for Sinatra.'
13
- spec.description = spec.summary
10
+ spec.authors = ['Evan Lecklider', 'Gustavo Sobral']
11
+ spec.email = ['evan@lecklider.com', 'ghsobral@gmail.com']
12
+ spec.summary = 'Basic strong parameter filters for Sinatra.'
14
13
  spec.homepage = 'https://github.com/evanleck/sinatra-strong-params'
15
14
  spec.license = 'MIT'
16
15
 
@@ -3,19 +3,19 @@
3
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
5
  require_relative 'spec_helper'
6
- require 'json'
7
6
  require 'sinatra/strong-params'
8
7
 
9
8
  describe Sinatra::StrongParams do
10
- context "using allows filter" do
11
- context "with no nested params" do
9
+ context 'using allows filter' do
10
+ context 'with no nested params' do
12
11
  let(:request_params) { { id: 'id', action: 'action', not_allows: 'not_allows' } }
13
12
 
14
13
  it 'supports accessing params with string keys' do
15
14
  actual_params = nil
16
- mock_app do
17
- register Sinatra::StrongParams
18
- get '/', allows: [:id, :action] { actual_params = params }
15
+ mock_registerd_app do
16
+ get '/', allows: [:id, :action] do
17
+ actual_params = params
18
+ end
19
19
  end
20
20
 
21
21
  get '/', request_params
@@ -26,9 +26,10 @@ describe Sinatra::StrongParams do
26
26
 
27
27
  it 'supports accessing params with symbol keys' do
28
28
  actual_params = nil
29
- mock_app do
30
- register Sinatra::StrongParams
31
- get '/', allows: [:id, :action] { actual_params = params }
29
+ mock_registerd_app do
30
+ get '/', allows: [:id, :action] do
31
+ actual_params = params
32
+ end
32
33
  end
33
34
 
34
35
  get '/', request_params
@@ -38,14 +39,15 @@ describe Sinatra::StrongParams do
38
39
  end
39
40
  end
40
41
 
41
- context "with nested params" do
42
+ context 'with nested params' do
42
43
  let(:request_params) { { id: [ { in_array: 'in_array'} ], action: { nested_hash: 'nested_hash'} }}
43
44
 
44
45
  it 'supports accessing params with string keys' do
45
46
  actual_params = nil
46
- mock_app do
47
- register Sinatra::StrongParams
48
- get '/', allows: [:id, :action] { actual_params = params }
47
+ mock_registerd_app do
48
+ get '/', allows: [:id, :action] do
49
+ actual_params = params
50
+ end
49
51
  end
50
52
 
51
53
  get '/', request_params
@@ -55,9 +57,10 @@ describe Sinatra::StrongParams do
55
57
 
56
58
  it 'supports accessing params with symbol keys' do
57
59
  actual_params = nil
58
- mock_app do
59
- register Sinatra::StrongParams
60
- get '/', allows: [:id, :action] { actual_params = params }
60
+ mock_registerd_app do
61
+ get '/', allows: [:id, :action] do
62
+ actual_params = params
63
+ end
61
64
  end
62
65
 
63
66
  get '/', request_params
@@ -67,31 +70,84 @@ describe Sinatra::StrongParams do
67
70
  end
68
71
  end
69
72
 
70
- context "using needs filter" do
73
+ context 'using needs filter' do
74
+ let(:request_params) { { id: 'id', action: 'action' } }
75
+
71
76
  it 'supports accessing params with string keys' do
72
77
  actual_params = nil
73
- mock_app do
74
- register Sinatra::StrongParams
75
- get '/', needs: [:id, :action] { actual_params = params }
78
+ mock_registerd_app do
79
+ get '/', needs: [:id, :action] do
80
+ actual_params = params
81
+ end
76
82
  end
77
- params = { id: 'id', action: 'action' }
78
83
 
79
- get '/', params
80
- expect(actual_params['id']).to eq params[:id]
81
- expect(actual_params['action']).to eq params[:action]
84
+ get '/', request_params
85
+ expect(actual_params['id']).to eq request_params[:id]
86
+ expect(actual_params['action']).to eq request_params[:action]
82
87
  end
83
88
 
84
89
  it 'supports accessing params with symbol keys' do
85
90
  actual_params = nil
86
- mock_app do
87
- register Sinatra::StrongParams
88
- get '/', needs: [:id, :action] { actual_params = params }
91
+ mock_registerd_app do
92
+ get '/', needs: [:id, :action] do
93
+ actual_params = params
94
+ end
95
+ end
96
+
97
+ get '/', request_params
98
+ expect(actual_params[:id]).to eq request_params[:id]
99
+ expect(actual_params[:action]).to eq request_params[:action]
100
+ end
101
+
102
+ context 'with missing params' do
103
+ context 'and empty request' do
104
+ let(:request_params) { nil }
105
+
106
+ it 'return an error message with the missing keys on it' do
107
+ mock_registerd_app do
108
+ get '/', needs: [:id, :name, :action] do
109
+ end
110
+ end
111
+
112
+ get '/', request_params
113
+ expect(last_response.status).to eq 400
114
+ expect(last_response.body).to eq('One or more required parameters were missing: id, name, action')
115
+ end
116
+ end
117
+
118
+ context 'and some params are present' do
119
+ let(:request_params) { { id: 'id', name: '' } }
120
+
121
+ it 'return an error message with the missing keys on it' do
122
+ mock_registerd_app do
123
+ get '/', needs: [:id, :name, :action] do
124
+ end
125
+ end
126
+
127
+ get '/', request_params
128
+ expect(last_response.status).to eq 400
129
+ expect(last_response.body).to eq('One or more required parameters were missing: name, action')
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ context 'using allows and needs filter' do
136
+ let(:request_params) { { id: 'id', action: 'action', resource: 'resource', not_allows: 'not_allows' } }
137
+
138
+ it 'supports accessing params with string keys' do
139
+ actual_params = nil
140
+ mock_registerd_app do
141
+ get '/', needs: [:id, :action], allows: [:resource] do
142
+ actual_params = params
143
+ end
89
144
  end
90
- params = { id: 'id', action: 'action' }
91
145
 
92
- get '/', params
93
- expect(actual_params[:id]).to eq params[:id]
94
- expect(actual_params[:action]).to eq params[:action]
146
+ get '/', request_params
147
+ expect(actual_params['id']).to eq request_params[:id]
148
+ expect(actual_params['action']).to eq request_params[:action]
149
+ expect(actual_params['resource']).to eq request_params[:resource]
150
+ expect(actual_params['not_allows']).to eq nil
95
151
  end
96
152
  end
97
153
  end
@@ -11,6 +11,13 @@ module TestHelper
11
11
  end
12
12
  end
13
13
 
14
+ def mock_registerd_app(&block)
15
+ @app = mock_app do
16
+ register Sinatra::StrongParams
17
+ class_eval(&block)
18
+ end
19
+ end
20
+
14
21
  def app
15
22
  @app
16
23
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-strong-params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Lecklider
8
+ - Gustavo Sobral
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2017-01-30 00:00:00.000000000 Z
12
+ date: 2019-01-30 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: sinatra
@@ -80,14 +81,17 @@ dependencies:
80
81
  - - ">="
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
- description: Some super basic strong parameter filters for Sinatra.
84
+ description:
84
85
  email:
85
86
  - evan@lecklider.com
87
+ - ghsobral@gmail.com
86
88
  executables: []
87
89
  extensions: []
88
90
  extra_rdoc_files: []
89
91
  files:
90
92
  - ".gitignore"
93
+ - ".travis.yml"
94
+ - CHANGELOG.md
91
95
  - Gemfile
92
96
  - LICENSE.txt
93
97
  - README.md
@@ -117,11 +121,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
121
  - !ruby/object:Gem::Version
118
122
  version: '0'
119
123
  requirements: []
120
- rubyforge_project:
121
- rubygems_version: 2.6.10
124
+ rubygems_version: 3.0.2
122
125
  signing_key:
123
126
  specification_version: 4
124
- summary: Some super basic strong parameter filters for Sinatra.
127
+ summary: Basic strong parameter filters for Sinatra.
125
128
  test_files:
126
129
  - spec/spec_helper.rb
127
130
  - spec/strong-params_spec.rb