Class: NaughtyWords::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/naughty_words/base.rb

Class Method Summary collapse

Class Method Details

.filter(string:, replacement: "*") ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/naughty_words/base.rb', line 24

def filter(string:, replacement: "*")
  validate_input!(string)
  validate_replacement!(replacement)
  result = string.dup

  denied_words = Config.deny_overrides.dup
  denied_words += deny_list_from_files if Config.use_built_in_lists
  denied_words += deny_list_from_db
  denied_words -= Config.allow_overrides # Remove any allowed overrides
  denied_words = denied_words.sort_by(&:length).reverse

  denied_words.each do |word|
    next if word.empty?
    result.gsub!(/#{Regexp.escape(word)}/i, replacement * word.length)
  end

  result
end

.profanity?(string:) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/naughty_words/base.rb', line 8

def profanity?(string:)
  validate_input!(string)
  normalized_string = normalize_string(string)

  return false if word_in_list?(normalized_string, Config.allow_overrides)
  return true if word_in_list?(normalized_string, Config.deny_overrides)

  return false if Config.use_built_in_lists && word_in_list?(normalized_string, allow_list_from_files)
  return false if word_in_list?(normalized_string, allow_list_from_db)

  return true if Config.use_built_in_lists && word_in_list?(normalized_string, deny_list_from_files)
  return true if word_in_list?(normalized_string, deny_list_from_db)

  false
end

.show_list(list:, include_metadata: false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/naughty_words/base.rb', line 43

def show_list(list:, include_metadata: false)
  validate_list!(list)
  
  if  && defined?(WordList)
    WordList.where(list_type: list)
  else
    words = []
    words += (list == "deny" ? deny_list_from_files : allow_list_from_files) if Config.use_built_in_lists
    words += (list == "deny" ? deny_list_from_db : allow_list_from_db)
    words
  end
end