class BigBrotha::Censor
Public Class Methods
censor_taboos_in_text(text, taboo_words)
click to toggle source
# File lib/bigbrotha/censor.rb, line 52 def self.censor_taboos_in_text(text, taboo_words) taboo_words.each do |taboo| taboo_chars = taboo.scan(/./) regex = "" taboo_chars.each { |c| regex+= "[#{c.downcase}|#{c.upcase}]" } #build the regex to match upcase and downcase letters r = Regexp.new(regex) text = text.gsub(r, "*" * taboo.size) end text end
censor_text!(user, post, content_column)
click to toggle source
Class methods #
# File lib/bigbrotha/censor.rb, line 7 def self.censor_text!(user, post, content_column) raise MissingParam.new("Missing parameter: :user") if user.blank? raise MissingParam.new("Missing parameter: :content_column") if content_column.blank? return post if post.nil? censored_post = post taboos = Taboo.all.to_a words = post.split(/[\s]/).delete_if(&:blank?) #split by spaces new_taboo_post = nil all_appeared_taboos = [] words.each do |word| appeared_taboos = find_taboos_in_text(word, taboos) unless appeared_taboos.blank? new_taboo_post = TabooPost.create(content: post, content_column: content_column, user_id: user.id) if new_taboo_post.blank? all_appeared_taboos += appeared_taboos #censor all taboos appearing in one word censored_word = censor_taboos_in_text(word, appeared_taboos.map{|t| t.keyword}) #replace the word with taboos in the original post censored_post = censored_post.gsub(word, censored_word) end end new_taboo_post.set_taboos(all_appeared_taboos) unless new_taboo_post.blank? censored_post end
find_taboos_in_text(text, taboos)
click to toggle source
# File lib/bigbrotha/censor.rb, line 39 def self.find_taboos_in_text(text, taboos) return nil if text.blank? or taboos.blank? appeared_taboos = [] taboos.each do |taboo| taboo_chars = taboo.keyword.scan(/./) #split the taboos word on chars regex = "" taboo_chars.each { |c| regex+="[#{c.downcase}|#{c.upcase}]" } appeared_taboos << taboo unless (text =~ Regexp.new(regex)).blank? end return appeared_taboos end