<commit_before>module Gitrob
  module Observers
    class SensitiveFiles

      class InvalidPatternFileError < StandardError; end
      class InvalidPatternError < StandardError; end

      VALID_KEYS  = %w(part type pattern caption description)
      VALID_PARTS = %w(path filename extension)
      VALID_TYPES = %w(match regex)

      def self.observe(blob)
        patterns.each do |pattern|
          check_blob(blob, pattern)
        end
      end

      def self.load_patterns!
        patterns = read_pattern_file!
        validate_patterns!(patterns)
        @patterns = patterns
      end

      def self.patterns
        @patterns
      end

    private

      def self.read_pattern_file!
        JSON.parse(File.read("#{File.dirname(__FILE__)}/../../../patterns.json"))
      rescue JSON::ParserError => e
        raise InvalidPatternFileError.new("Cannot parse pattern file: #{e.message}")
      end
<commit_msg>Don't flag files if they are empty<commit_after>module Gitrob
  module Observers
    class SensitiveFiles

      class InvalidPatternFileError < StandardError; end
      class InvalidPatternError < StandardError; end

      VALID_KEYS  = %w(part type pattern caption description)
      VALID_PARTS = %w(path filename extension)
      VALID_TYPES = %w(match regex)

      def self.observe(blob)
        return if !blob.size || blob.size.zero?
        patterns.each do |pattern|
          check_blob(blob, pattern)
        end
      end

      def self.load_patterns!
        patterns = read_pattern_file!
        validate_patterns!(patterns)
        @patterns = patterns
      end

      def self.patterns
        @patterns
      end

    private

      def self.read_pattern_file!
        JSON.parse(File.read("#{File.dirname(__FILE__)}/../../../patterns.json"))
      rescue JSON::ParserError => e
        raise InvalidPatternFileError.new("Cannot parse pattern file: #{e.message}")
      end

{'fraction_non_alphanumeric': 0.08754559666492966, 'fraction_numerical': 0.0, 'mean_word_length': 2.4470377019748653, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2635409', 'n_tokens_mistral': 550, 'n_tokens_neox': 540, 'n_words': 134}