Method: Precheck::CurseWordsRule#rule_block

Defined in:
precheck/lib/precheck/rules/curse_words_rule.rb

#rule_blockObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'precheck/lib/precheck/rules/curse_words_rule.rb', line 23

def rule_block
  return lambda { |text|
    return RuleReturn.new(validation_state: Precheck::VALIDATION_STATES[:passed]) if text.to_s.strip.empty?
    text = text.downcase
    split_words = text.split
    split_words_without_punctuation = text.gsub(/\W/, ' ').split

    # remove punctuation and add only unique words
     = (split_words + split_words_without_punctuation).uniq
     = .map { |word| Digest::SHA256.hexdigest(word) }
    curse_hashes_set = hashed_curse_word_set

    found_words = []
    .each_with_index do |word, index|
      if curse_hashes_set.include?(word)
        found_words << [index]
      end
    end

    if found_words.length > 0
      friendly_found_words = found_words.join(', ')
      UI.verbose("#{self.class.name.split('::').last ||= self.class.name} found potential curse words 😬")
      UI.verbose("Keep in mind, these words might be ok given the context they are used in")
      UI.verbose("Matched: \"#{friendly_found_words}\"")
      return RuleReturn.new(validation_state: VALIDATION_STATES[:failed], failure_data: "found: #{friendly_found_words}")
    else
      return RuleReturn.new(validation_state: VALIDATION_STATES[:passed])
    end
  }
end