Class: Censor::Base

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

Constant Summary collapse

GARBLED_STRING =
'$@!#%'.freeze
REPLACEMENT_PATTERNS =
{
  vowels: ->(str) { str.gsub(/[aeiou]/i, '*') },
  stars: ->(str) { '*' * str.size },
  nonconsonants: ->(word) { word.gsub(/[^bcdfghjklmnpqrstvwxyz]/i, '*') },
  default: ->(_) { GARBLED_STRING }
}.freeze

Class Method Summary collapse

Class Method Details

.censor(text, censorship_source, replacement_pattern = :default) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/censor/base.rb', line 13

def censor(text, censorship_source, replacement_pattern = :default)
  raise Censor::InvalidCensorshipSource, 'Censorship source can only be an array.' unless censorship_source.is_a? Array
  return text unless compromised?(text, censorship_source)
  censorship_source.each do |foul|
    text.gsub!(/\b#{foul}\b/i, REPLACEMENT_PATTERNS[replacement_pattern].call(foul))
  end
  text
end

.compromised?(text, censorship_source) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



22
23
24
25
26
27
28
29
# File 'lib/censor/base.rb', line 22

def compromised?(text, censorship_source)
  raise Censor::InvalidCensorshipSource, 'Censorship source can only be an array.' unless censorship_source.is_a? Array
  return false unless text.size >= 3
  censorship_source.each do |foul|
    return true if text =~ /\b#{foul}\b/i
  end
  false
end