Sha256: 733f2c6c1be0864acffb37c86cb307ca22fdb4eaa8778d753d9fb42c3d1bf15a

Contents?: true

Size: 1.61 KB

Versions: 153

Compression:

Stored size: 1.61 KB

Contents

module ActionDispatch
  module Http
    class ParameterFilter

      def initialize(filters)
        @filters = filters
      end

      def filter(params)
        if enabled?
          compiled_filter.call(params)
        else
          params.dup
        end
      end

    private

      def enabled?
        @filters.present?
      end

      FILTERED = '[FILTERED]'.freeze

      def compiled_filter
        @compiled_filter ||= begin
          regexps, blocks = compile_filter

          lambda do |original_params|
            filtered_params = {}

            original_params.each do |key, value|
              if regexps.find { |r| key =~ r }
                value = FILTERED
              elsif value.is_a?(Hash)
                value = filter(value)
              elsif value.is_a?(Array)
                value = value.map { |v| v.is_a?(Hash) ? filter(v) : v }
              elsif blocks.present?
                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

      def compile_filter
        strings, regexps, blocks = [], [], []

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

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

    end
  end
end

Version data entries

153 entries across 114 versions & 18 rubygems

Version Path
actionpack-3.2.22.5 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.22.4 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.22.3 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.22.2 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.22.1 lib/action_dispatch/http/parameter_filter.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.22 lib/action_dispatch/http/parameter_filter.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/actionpack-3.2.12/lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.21 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.20 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.19 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.18 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.17 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.16 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.15 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.15.rc3 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.15.rc2 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.15.rc1 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.14 lib/action_dispatch/http/parameter_filter.rb
actionpack-3.2.14.rc2 lib/action_dispatch/http/parameter_filter.rb