Sha256: 430eaabad05f53cc1a990e477a7326147ee15b8575b2aa93654d9acf86b676b1

Contents?: true

Size: 1.71 KB

Versions: 68

Compression:

Stored size: 1.71 KB

Contents

module ActionDispatch
  module Http
    class ParameterFilter
      FILTERED = '[FILTERED]'.freeze # :nodoc:

      def initialize(filters = [])
        @filters = filters
      end

      def filter(params)
        compiled_filter.call(params)
      end

    private

      def compiled_filter
        @compiled_filter ||= CompiledFilter.compile(@filters)
      end

      class CompiledFilter # :nodoc:
        def self.compile(filters)
          return lambda { |params| params.dup } if filters.empty?

          strings, regexps, blocks = [], [], []

          filters.each do |item|
            case item
            when Proc
              blocks << item
            when Regexp
              regexps << item
            else
              strings << item.to_s
            end
          end

          regexps << Regexp.new(strings.join('|'), true) unless strings.empty?
          new regexps, blocks
        end

        attr_reader :regexps, :blocks

        def initialize(regexps, blocks)
          @regexps = regexps
          @blocks  = blocks
        end

        def call(original_params)
          filtered_params = {}

          original_params.each do |key, value|
            if regexps.any? { |r| key =~ r }
              value = FILTERED
            elsif value.is_a?(Hash)
              value = call(value)
            elsif value.is_a?(Array)
              value = value.map { |v| v.is_a?(Hash) ? call(v) : v }
            elsif blocks.any?
              key = key.dup if key.duplicable?
              value = value.dup if value.duplicable?
              blocks.each { |b| b.call(key, value) }
            end

            filtered_params[key] = value
          end

          filtered_params
        end
      end
    end
  end
end

Version data entries

68 entries across 64 versions & 8 rubygems

Version Path
actionpack-4.2.11.3 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.11.2 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.11.1 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.11 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.10 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.10.rc1 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.9 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.9.rc2 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.9.rc1 lib/action_dispatch/http/parameter_filter.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/actionpack-4.2.8/lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.8 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.8.rc1 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.7.1 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.7 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.1.16 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.1.16.rc1 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.7.rc1 lib/action_dispatch/http/parameter_filter.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.6/lib/action_dispatch/http/parameter_filter.rb
actionpack-4.1.15 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.6 lib/action_dispatch/http/parameter_filter.rb