Sha256: 5e569a319e69768c6cd0061aa60dd9f2e2e8c3c7ca1d327677de1581858a1f90

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module Censor
  class Base
    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<<self
      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

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
censor-0.0.2 lib/censor/base.rb