Class: Precheck::CurseWordsRule

Inherits:
TextRule show all
Defined in:
precheck/lib/precheck/rules/curse_words_rule.rb

Instance Attribute Summary

Attributes inherited from FastlaneCore::ConfigItem

#allow_shell_conversion, #code_gen_default_value, #code_gen_sensitive, #conflict_block, #conflicting_options, #default_value, #default_value_dynamic, #deprecated, #description, #display_in_shell, #env_name, #key, #optional, #sensitive, #short_option, #skip_type_validation, #verify_block

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TextRule

#handle_item?

Methods inherited from Rule

#check_item, #customize_with_data, default_value, #friendly_name, #handle_item?, #initialize, #inspect, #item_field_supported?, #needs_customization?, #perform_check, #skip_item_not_meant_for_this_rule, #supported_fields_symbol_set, #to_s

Methods inherited from FastlaneCore::ConfigItem

#auto_convert_value, #data_type, #deprecated_description, #doc_default_value, #ensure_boolean_type_passes_validation, #ensure_generic_type_passes_validation, #help_default_value, #initialize, #is_string, #string?, #to_s, #update_code_gen_default_value_if_able!, #valid?, #verify!

Constructor Details

This class inherits a constructor from Precheck::Rule

Class Method Details

.descriptionObject

[View source]

19
20
21
# File 'precheck/lib/precheck/rules/curse_words_rule.rb', line 19

def self.description
  "including words that might be considered objectionable"
end

.env_nameObject

[View source]

11
12
13
# File 'precheck/lib/precheck/rules/curse_words_rule.rb', line 11

def self.env_name
  "RULE_CURSE_WORDS"
end

.friendly_nameObject

[View source]

15
16
17
# File 'precheck/lib/precheck/rules/curse_words_rule.rb', line 15

def self.friendly_name
  "No curse words"
end

.keyObject

[View source]

7
8
9
# File 'precheck/lib/precheck/rules/curse_words_rule.rb', line 7

def self.key
  :curse_words
end

Instance Method Details

#hashed_curse_word_setObject

[View source]

54
55
56
57
58
59
60
# File 'precheck/lib/precheck/rules/curse_words_rule.rb', line 54

def hashed_curse_word_set
  curse_hashes = []
  File.open(File.dirname(__FILE__) + '/rules_data/curse_word_hashes/en_us.txt').each do |line|
    curse_hashes << line.to_s.strip
  end
  return curse_hashes.to_set
end

#rule_blockObject

[View source]

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