Sha256: 38830a259775bf5553780b58b4dd20345254055e70a7cb3214eb9a5b434ad3f9

Contents?: true

Size: 1.7 KB

Versions: 58

Compression:

Stored size: 1.7 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
              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

58 entries across 58 versions & 3 rubygems

Version Path
actionpack-4.0.13 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.0.13.rc1 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.0 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.0.rc3 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.0.rc2 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.0.rc1 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.1.7.1 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.0.11.1 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.1.8 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.0.12 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.0.beta4 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.0.beta3 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.1.7 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.0.11 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.2.0.beta2 lib/action_dispatch/http/parameter_filter.rb
nanumfont-rails-0.1 vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.6/lib/action_dispatch/http/parameter_filter.rb
actionpack-4.0.10 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.1.6 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.1.6.rc2 lib/action_dispatch/http/parameter_filter.rb
actionpack-4.0.10.rc2 lib/action_dispatch/http/parameter_filter.rb