rack-filter-param 0.2.1 → 0.3
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.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/rack/filter_param.rb +7 -79
- data/lib/rack/filter_param/apply_filter.rb +73 -0
- data/lib/rack/filter_param/filter.rb +21 -0
- data/lib/rack/filter_param/middleware.rb +17 -0
- data/lib/rack/filter_param/version.rb +2 -2
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1dff13e084ee634e79836638befe1cb562254b75
|
4
|
+
data.tar.gz: cd5962bb0fb1adb14a47c5810cf3a69ab8e82309
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c78c5b5c06bab366d0664a87515448b532e42ead19bdb8352cd7d45451136751613ef29e57e7e510fb173821a4e581412266b6ef0191055b44e11322f410a299
|
7
|
+
data.tar.gz: a3b9a06802b3f7de047f9cabe01801f939fa94adb339132ea9b72c1818f7b4c6ead9e68148dd40c59d99adb54de6264d7c6b24f46fa7cd140bb49c9030a9c536
|
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Refactoring something behind an API? Plagued by extraneous HTTP params? `rack-filter-param` might be for you.
|
4
4
|
|
5
|
+
[](https://travis-ci.org/rfwatson/rack-filter-param)
|
6
|
+
|
5
7
|
## What is it?
|
6
8
|
|
7
9
|
[Rack](https://github.com/rack/rack) middleware to remove specific params from HTTP requests.
|
data/lib/rack/filter_param.rb
CHANGED
@@ -1,86 +1,14 @@
|
|
1
|
-
require
|
1
|
+
require 'rack/filter_param/version'
|
2
|
+
require 'rack/filter_param/middleware'
|
3
|
+
require 'rack/filter_param/filter'
|
4
|
+
require 'rack/filter_param/apply_filter'
|
2
5
|
|
3
6
|
module Rack
|
4
|
-
|
7
|
+
module FilterParam
|
5
8
|
ACTION_DISPATCH_KEY = 'action_dispatch.request.request_parameters'.freeze
|
6
9
|
FILTERED_PARAMS_KEY = 'rack.filtered_params'.freeze
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
@params = params.flatten
|
11
|
-
end
|
12
|
-
|
13
|
-
def call(env)
|
14
|
-
@request = Rack::Request.new(env)
|
15
|
-
@params.each { |param| process_param(param) }
|
16
|
-
|
17
|
-
@app.call(env)
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
attr_reader :request
|
23
|
-
|
24
|
-
def process_param(param)
|
25
|
-
return unless path_matches?(param)
|
26
|
-
return unless param_exists?(param)
|
27
|
-
return unless affirmative_conditional?(param)
|
28
|
-
|
29
|
-
param = param[:param] if param.is_a?(Hash)
|
30
|
-
|
31
|
-
if delete_from_action_dispatch(param) || delete_from_request(param)
|
32
|
-
filtered_params << [ param.to_s, nil ]
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def path_matches?(param)
|
37
|
-
return true unless param.is_a?(Hash)
|
38
|
-
|
39
|
-
path = param[:path]
|
40
|
-
return true unless path = param[:path]
|
41
|
-
|
42
|
-
return request.env['PATH_INFO'] == path if path.is_a?(String)
|
43
|
-
return request.env['PATH_INFO'] =~ path if path.is_a?(Regexp)
|
44
|
-
|
45
|
-
false
|
46
|
-
end
|
47
|
-
|
48
|
-
def param_exists?(param)
|
49
|
-
param = param.is_a?(Hash) ? param[:param] : param
|
50
|
-
params.has_key?(param.to_s)
|
51
|
-
end
|
52
|
-
|
53
|
-
def params
|
54
|
-
action_dispatch_parsed? ? action_dispatch_params : request.params
|
55
|
-
end
|
56
|
-
|
57
|
-
def affirmative_conditional?(param)
|
58
|
-
return true unless param.is_a?(Hash)
|
59
|
-
|
60
|
-
callable, param = param[:if], param[:param]
|
61
|
-
return true if callable.nil?
|
62
|
-
|
63
|
-
callable.call(params[param.to_s])
|
64
|
-
end
|
65
|
-
|
66
|
-
def delete_from_action_dispatch(param)
|
67
|
-
action_dispatch_parsed? && !!action_dispatch_params.delete(param.to_s)
|
68
|
-
end
|
69
|
-
|
70
|
-
def delete_from_request(param)
|
71
|
-
!!request.delete_param(param.to_s)
|
72
|
-
end
|
73
|
-
|
74
|
-
def action_dispatch_params
|
75
|
-
request.env[ACTION_DISPATCH_KEY]
|
76
|
-
end
|
77
|
-
|
78
|
-
def action_dispatch_parsed?
|
79
|
-
!action_dispatch_params.nil?
|
80
|
-
end
|
81
|
-
|
82
|
-
def filtered_params
|
83
|
-
request.env[FILTERED_PARAMS_KEY] ||= []
|
84
|
-
end
|
11
|
+
extend SingleForwardable
|
12
|
+
def_delegators :'Rack::FilterParam::Middleware', :new
|
85
13
|
end
|
86
14
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Rack
|
2
|
+
class ApplyFilter
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
def initialize(filter, request)
|
6
|
+
@filter = filter
|
7
|
+
@request = request
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
return unless param_exists?
|
12
|
+
return unless path_matches?
|
13
|
+
return unless if_proc_affirmative?
|
14
|
+
|
15
|
+
if delete_from_action_dispatch || delete_from_request
|
16
|
+
filtered_params << [ param, nil ]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
attr_reader :filter, :request
|
22
|
+
|
23
|
+
def_delegators :@filter, :param, :path, :if_proc
|
24
|
+
def_delegators :@request, :env
|
25
|
+
|
26
|
+
def params
|
27
|
+
action_dispatch_parsed? ? action_dispatch_params : request.params
|
28
|
+
end
|
29
|
+
|
30
|
+
def param_exists?
|
31
|
+
params.has_key?(param)
|
32
|
+
end
|
33
|
+
|
34
|
+
def param_value
|
35
|
+
params[param]
|
36
|
+
end
|
37
|
+
|
38
|
+
def action_dispatch_params
|
39
|
+
env[FilterParam::ACTION_DISPATCH_KEY]
|
40
|
+
end
|
41
|
+
|
42
|
+
def action_dispatch_parsed?
|
43
|
+
!action_dispatch_params.nil?
|
44
|
+
end
|
45
|
+
|
46
|
+
def path_matches?
|
47
|
+
return true if path.nil?
|
48
|
+
|
49
|
+
return env['PATH_INFO'] == path if path.is_a?(String)
|
50
|
+
return env['PATH_INFO'] =~ path if path.is_a?(Regexp)
|
51
|
+
|
52
|
+
false
|
53
|
+
end
|
54
|
+
|
55
|
+
def if_proc_affirmative?
|
56
|
+
return true if if_proc.nil?
|
57
|
+
|
58
|
+
if_proc.call(param_value)
|
59
|
+
end
|
60
|
+
|
61
|
+
def delete_from_action_dispatch
|
62
|
+
action_dispatch_parsed? && !!action_dispatch_params.delete(param)
|
63
|
+
end
|
64
|
+
|
65
|
+
def delete_from_request
|
66
|
+
!!request.delete_param(param)
|
67
|
+
end
|
68
|
+
|
69
|
+
def filtered_params
|
70
|
+
env[FilterParam::FILTERED_PARAMS_KEY] ||= []
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Rack
|
2
|
+
class Filter
|
3
|
+
attr_reader :param, :path, :if_proc
|
4
|
+
|
5
|
+
def initialize(options)
|
6
|
+
if options.is_a?(Hash)
|
7
|
+
@param = parse_param(options[:param])
|
8
|
+
@path = options[:path]
|
9
|
+
@if_proc = options[:if]
|
10
|
+
else
|
11
|
+
@param = parse_param(options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def parse_param(param)
|
18
|
+
param.is_a?(Symbol) ? param.to_s : param
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rack
|
2
|
+
module FilterParam
|
3
|
+
class Middleware
|
4
|
+
def initialize(app, *filters)
|
5
|
+
@app = app
|
6
|
+
@filters = filters.flatten.map(&Filter.public_method(:new))
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
request = Request.new(env)
|
11
|
+
|
12
|
+
@filters.each { |filter| ApplyFilter.new(filter, request).call }
|
13
|
+
@app.call(env)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-filter-param
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob Watson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -111,6 +111,9 @@ files:
|
|
111
111
|
- bin/console
|
112
112
|
- bin/setup
|
113
113
|
- lib/rack/filter_param.rb
|
114
|
+
- lib/rack/filter_param/apply_filter.rb
|
115
|
+
- lib/rack/filter_param/filter.rb
|
116
|
+
- lib/rack/filter_param/middleware.rb
|
114
117
|
- lib/rack/filter_param/version.rb
|
115
118
|
- rack-filter-param.gemspec
|
116
119
|
homepage: https://github.com/rfwatson
|