Class: Obscenity::Base
- Inherits:
-
Object
- Object
- Obscenity::Base
- Defined in:
- lib/obscenity/base.rb
Class Method Summary collapse
- .blacklist ⇒ Object
- .blacklist=(value) ⇒ Object
- .offensive(text) ⇒ Object
- .profane?(text) ⇒ Boolean
- .replace(word) ⇒ Object
- .replacement(chars) ⇒ Object
- .sanitize(text) ⇒ Object
- .whitelist ⇒ Object
- .whitelist=(value) ⇒ Object
Class Method Details
.blacklist ⇒ Object
6 7 8 |
# File 'lib/obscenity/base.rb', line 6 def blacklist @blacklist ||= set_list_content(Obscenity.config.blacklist) end |
.blacklist=(value) ⇒ Object
10 11 12 |
# File 'lib/obscenity/base.rb', line 10 def blacklist=(value) @blacklist = value == :default ? set_list_content(Obscenity::Config.new.blacklist) : value end |
.offensive(text) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/obscenity/base.rb', line 44 def offensive(text) words = [] return(words) unless text.to_s.size >= 3 blacklist.each do |foul| words << foul if text =~ /\b#{foul}\b/i && !whitelist.include?(foul) end words.uniq end |
.profane?(text) ⇒ Boolean
22 23 24 25 26 27 28 |
# File 'lib/obscenity/base.rb', line 22 def profane?(text) return(false) unless text.to_s.size >= 3 blacklist.each do |foul| return(true) if text =~ /\b#{foul}(?!\w)/i && !whitelist.include?(foul) end false end |
.replace(word) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/obscenity/base.rb', line 53 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
39 40 41 42 |
# File 'lib/obscenity/base.rb', line 39 def replacement(chars) @scoped_replacement = chars self end |
.sanitize(text) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/obscenity/base.rb', line 30 def sanitize(text) return(text) unless text.to_s.size >= 3 blacklist.each do |foul| text.gsub!(/\b#{foul}\b/i, replace(foul)) unless whitelist.include?(foul) end @scoped_replacement = nil text end |
.whitelist ⇒ Object
14 15 16 |
# File 'lib/obscenity/base.rb', line 14 def whitelist @whitelist ||= set_list_content(Obscenity.config.whitelist) end |
.whitelist=(value) ⇒ Object
18 19 20 |
# File 'lib/obscenity/base.rb', line 18 def whitelist=(value) @whitelist = value == :default ? set_list_content(Obscenity::Config.new.whitelist) : value end |