Sha256: 7d2cff9b0316529397e6403633e6d6bca5476926dbb1d79dd5e6b15ca678ca77

Contents?: true

Size: 1.48 KB

Versions: 6

Compression:

Stored size: 1.48 KB

Contents

module Logster
  class IgnorePattern

    def initialize(message_pattern = nil, env_patterns = nil)
      @msg_match = message_pattern
      @env_match = env_patterns
    end

    def self.from_message_and_request_uri(msg, request)
      IgnorePattern.new(msg, REQUEST_URI: request)
    end

    def matches?(message)
      if @msg_match
        return false unless compare(message.message, @msg_match)
      end

      if @env_match
        return false unless compare(message.env, @env_match)
      end

      true
    end

    def to_s
      "<#Logster::IgnorePattern, msg_match: #{@msg_match.inspect}, env_match: #{@env_match.inspect}>"
    end

    private

    def compare(message, pattern)
      return false unless message && pattern

      case pattern
      when Regexp
        message.to_s =~ pattern
      when String
        message.to_s =~ Regexp.new(Regexp.escape(pattern), Regexp::IGNORECASE)
      when Hash
        if Hash === message
          compare_hash(message, pattern)
        else
          false
        end
        else
        false
      end
    end

    def compare_hash(message_hash, pattern_hash)
      return false unless message_hash
      pattern_hash.each do |key, value|
        return false unless compare(get_indifferent(message_hash, key), value)
      end
      true
    end

    def get_indifferent(hash, key)
      return hash[key] if hash[key]
      return hash[key.to_s] if hash[key.to_s]
      # no key.to_sym please, memory leak in Ruby < 2.2
      nil
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
logster-2.4.2 lib/logster/ignore_pattern.rb
logster-2.4.1 lib/logster/ignore_pattern.rb
logster-2.4.0 lib/logster/ignore_pattern.rb
logster-2.3.3 lib/logster/ignore_pattern.rb
logster-2.3.2 lib/logster/ignore_pattern.rb
logster-2.3.1 lib/logster/ignore_pattern.rb