Class: Obscenity::Base

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

Class Method Summary collapse

Class Method Details

.blacklistObject



5
6
7
# File 'lib/obscenity/base.rb', line 5

def blacklist
  @blacklist ||= set_list_content(Obscenity.config.blacklist)
end

.blacklist=(value) ⇒ Object



9
10
11
# File 'lib/obscenity/base.rb', line 9

def blacklist=(value)
  @blacklist = value == :default ? set_list_content(Obscenity::Config.new.blacklist) : value
end

.offensive(text) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/obscenity/base.rb', line 40

def offensive(text)
  words = []
  blacklist.each do |foul|
    words << foul if text =~ /\b#{foul}\b/i && !whitelist.include?(foul)
  end
  words.uniq
end

.profane?(text) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/obscenity/base.rb', line 21

def profane?(text)
  blacklist.any? do |foul|
    text =~ /\b#{foul}\b/i && !whitelist.include?(foul)
  end
end

.replace(word) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/obscenity/base.rb', line 48

def replace(word)
  content = @scoped_replacement || Obscenity.config.replacement
  case content
  when :vowels then word.gsub(/[aeiou]/i, '*')
  when :stars  then '*' * word.size
  when :nonconsonants then word.gsub(/[^bcdfghjklmnpqrstvwxyz]/i, '*')
  when :default, :garbled then '$@!#%'
  else content
  end
end

.replacement(chars) ⇒ Object



35
36
37
38
# File 'lib/obscenity/base.rb', line 35

def replacement(chars)
  @scoped_replacement = chars
  self
end

.sanitize(text) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/obscenity/base.rb', line 27

def sanitize(text)
  blacklist.each do |foul|
    text.gsub!(/\b#{foul}\b/i, replace(foul)) unless whitelist.include?(foul)
  end
  @scoped_replacement = nil
  text
end

.whitelistObject



13
14
15
# File 'lib/obscenity/base.rb', line 13

def whitelist
  @whitelist ||= set_list_content(Obscenity.config.whitelist)
end

.whitelist=(value) ⇒ Object



17
18
19
# File 'lib/obscenity/base.rb', line 17

def whitelist=(value)
  @whitelist = value == :default ? set_list_content(Obscenity::Config.new.whitelist) : value
end