Sha256: b494b11372c9d1c79c075b98578a1ee43a66c0b471f836aa9e3053552f293768

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

class Logasm
  module Preprocessors
    class Blacklist

      DEFAULT_ACTION = 'prune'
      MASK_SYMBOL = '*'
      MASKED_VALUE = MASK_SYMBOL * 5

      class UnsupportedActionException < Exception
      end

      def initialize(config = {})
        @fields_to_process = config[:fields].inject({}) do |mem, field|
          key = field.delete(:key)
          options = {action: DEFAULT_ACTION}.merge(field)
          validate_action_supported(options[:action])
          mem.merge(key => options)
        end
      end

      def process(data)
        if data.is_a? Hash
          data.inject({}) do |mem, (key, val)|
            if (field = @fields_to_process[key.to_s])
              self.send(action_method(field[:action]), mem, key, val)
            else
              mem.merge(key => process(val))
            end
          end
        elsif data.is_a? Array
          data.inject([]) do |mem, val|
            mem + [process(val)]
          end
        else
          data
        end
      end

      private

      def action_method(action)
        "#{action}_field"
      end

      def validate_action_supported(action)
        unless self.respond_to?(action_method(action).to_sym, true)
          raise UnsupportedActionException.new("Action: #{action} is not supported")
        end
      end

      def mask_field(data, key, val)
        data.merge(key => MASKED_VALUE)
      end

      def prune_field(data, *)
        data
      end
      alias_method :exclude_field, :prune_field
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
logasm-jruby-1.2.1 lib/logasm/preprocessors/blacklist.rb
logasm-1.2.1 lib/logasm/preprocessors/blacklist.rb
logasm-jruby-1.2.0 lib/logasm/preprocessors/blacklist.rb
logasm-1.2.0 lib/logasm/preprocessors/blacklist.rb
logasm-1.1.0 lib/logasm/preprocessors/blacklist.rb
logasm-1.0.0 lib/logasm/preprocessors/blacklist.rb
logasm-0.9.1 lib/logasm/preprocessors/blacklist.rb