Class: Kiev::ParamFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/kiev/param_filter.rb

Constant Summary collapse

FILTERED =
"[FILTERED]"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filtered_params, ignored_params) ⇒ ParamFilter

Returns a new instance of ParamFilter.



11
12
13
14
# File 'lib/kiev/param_filter.rb', line 11

def initialize(filtered_params, ignored_params)
  @filtered_params = normalize(filtered_params)
  @ignored_params = normalize(ignored_params)
end

Class Method Details

.filter(params, filtered_params, ignored_params) ⇒ Object



7
8
9
# File 'lib/kiev/param_filter.rb', line 7

def self.filter(params, filtered_params, ignored_params)
  new(filtered_params, ignored_params).call(params)
end

Instance Method Details

#call(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kiev/param_filter.rb', line 16

def call(params)
  return params unless filterable?(params)

  params.each_with_object({}) do |(key, value), acc|
    next if ignored_params.include?(key.to_s)

    if defined?(ActionDispatch) && value.is_a?(ActionDispatch::Http::UploadedFile)
      value = {
        original_filename: value.original_filename,
        content_type: value.content_type,
        headers: value.headers
      }
    end

    acc[key] =
      if filtered_params.include?(key.to_s) && !value.is_a?(Hash)
        FILTERED
      elsif value.is_a?(Hash)
        call(value)
      else
        value
      end
  end
end