Sha256: f20f918626f6ac951a5adf2ace545fd94332bed30118d7446445a483d2ea70a0

Contents?: true

Size: 1.31 KB

Versions: 24

Compression:

Stored size: 1.31 KB

Contents

module Loga
  class ParameterFilter
    FILTERED = '[FILTERED]'.freeze

    attr_accessor :filters

    def initialize(filters)
      @filters = filters
    end

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

    private

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

    class CompiledFilter
      def self.compile(filters)
        ->(params) { params.dup } if filters.empty?

        regexps = []
        strings = []

        filters.each do |item|
          if item.is_a?(Regexp)
            regexps << item
          else
            strings << Regexp.escape(item.to_s)
          end
        end

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

      attr_reader :regexps

      def initialize(regexps)
        @regexps = regexps
      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 }
          end

          filtered_params[key] = value
        end

        filtered_params
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
loga-2.8.1 lib/loga/parameter_filter.rb
loga-2.8.0 lib/loga/parameter_filter.rb
loga-2.7.0 lib/loga/parameter_filter.rb
loga-2.6.1 lib/loga/parameter_filter.rb
loga-2.6.0 lib/loga/parameter_filter.rb
loga-2.5.4 lib/loga/parameter_filter.rb
loga-2.5.3 lib/loga/parameter_filter.rb
loga-2.5.2 lib/loga/parameter_filter.rb
loga-2.5.1 lib/loga/parameter_filter.rb
loga-2.5.0 lib/loga/parameter_filter.rb
loga-2.4.0 lib/loga/parameter_filter.rb
loga-2.3.1 lib/loga/parameter_filter.rb
loga-2.3.0 lib/loga/parameter_filter.rb
loga-2.2.0 lib/loga/parameter_filter.rb
loga-2.1.2 lib/loga/parameter_filter.rb
loga-2.1.1 lib/loga/parameter_filter.rb
loga-2.0.0 lib/loga/parameter_filter.rb
loga-1.4.0 lib/loga/parameter_filter.rb
loga-1.3.0 lib/loga/parameter_filter.rb
loga-1.2.1 lib/loga/parameter_filter.rb