Module: Core

Included in:
BannedWords
Defined in:
lib/banned_words/core.rb

Constant Summary collapse

BW_REGEX =
"[^a-zA-Z0-9]*"

Instance Method Summary collapse

Instance Method Details

#clearObject

Removes all banned words from the storage fild. If the storage file isn’t found an error is raised.



90
91
92
# File 'lib/banned_words/core.rb', line 90

def clear
  Storage::FileStore.empty_storage!
end

#create!(words) ⇒ Object

Create banned words. The supplied words are transformed into a banned words and stored into the storage file. An array of banned words is returned.

Parameters

words<String> or <Array>

Contains a word or an array of words. The words should not contain spaces.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/banned_words/core.rb', line 16

def create!(words)

  Storage::FileStore.ensure_storage_file
  words = words_to_array(words)

  if words.present?
    bw_file       = Storage::FileStore.load_storage
    regexed_words = words.map do |word|
      if regexed_word = word_to_regex(word)
        bw_file[word] = regexed_word
      end
    end
    Storage::FileStore.write_to_storage(bw_file)
  end

  regexed_words || []
end

#detect(text) ⇒ Object

Detects the banned words in the supplied text. It returnes an array containing the found banned words. An empty array is returned if no banned words are found.

Parameters

text<String>

The text which is checked for banned words.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/banned_words/core.rb', line 74

def detect(text)
  # Don't bother verifying if the text isn't present
  return [] unless text.present?

  if banned_words = Storage::FileStore.load_storage
    bw = banned_words.values.join("|")
    return text.scan(/#{bw}/i)
  end

  []
end

#listObject

List the banned words. If the storage file isn’t found an error is raised.



60
61
62
# File 'lib/banned_words/core.rb', line 60

def list
  Storage::FileStore.list_contents!
end

#mask(text, replace_with = "*Buzz*") ⇒ Object

Masks the banned words within supplied text. Returns the changed text. If no banned words are found then the initial text is returned.

Parameters

text<String>

The text which is checked for banned words.

replace_with<String>

The word which replaces the banned words. It defaults to Buzz.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/banned_words/core.rb', line 45

def mask(text, replace_with = "*Buzz*")
  # Don't bother verifying if the text isn't present
  return nil unless text.present?

  if banned_words = Storage::FileStore.load_storage
    bw = banned_words.values.join("|")
    text.gsub!(/#{bw}/i, replace_with)
  end

  text
end

#remove(words) ⇒ Object

Removes the supplied banned words from the list.

Parameters

words<String> or <Array>

Contains a word or an array of words.



102
103
104
105
106
107
108
109
# File 'lib/banned_words/core.rb', line 102

def remove(words)
  words = words_to_array(words)

  if (bw_list = Storage::FileStore.load_storage).present?
    new_bw = bw_list.reject { |name, regexed_name| words.include?(name) }
    Storage::FileStore.write_to_storage(new_bw) if new_bw != bw_list
  end
end