Class: Moderate::WordList

Inherits:
Object
  • Object
show all
Defined in:
lib/moderate/word_list.rb

Constant Summary collapse

WORD_LIST_URL =
'https://raw.githubusercontent.com/coffee-and-fun/google-profanity-words/main/data/en.txt'
CACHE_TTL =

30 days in seconds

30 * 24 * 60 * 60

Class Method Summary collapse

Class Method Details

.loadObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/moderate/word_list.rb', line 14

def load
  cache_path = cache_file_path

  begin
    if File.exist?(cache_path)
      if cache_valid?(cache_path)
        words = File.read(cache_path, encoding: 'UTF-8').split("\n").to_set
        return words unless words.empty?
      else
        logger.info("[moderate gem] Cache expired (older than #{CACHE_TTL / 86400} days). Refreshing word list...")
        return download_and_cache(cache_path)
      end
    end

    logger.info("[moderate gem] No cache found. Downloading word list for the first time...")
    download_and_cache(cache_path)
  rescue StandardError => e
    logger.error("[moderate gem] Error loading word list: #{e.message}")
    logger.debug("[moderate gem] #{e.backtrace.join("\n")}")
    raise Moderate::Error, "Failed to load bad words list: #{e.message}"
  end
end

.refresh_word_list!Object



37
38
39
40
41
42
# File 'lib/moderate/word_list.rb', line 37

def refresh_word_list!
  logger.info("[moderate gem] Manually refreshing word list")
  cache_path = cache_file_path
  File.delete(cache_path) if File.exist?(cache_path)
  download_and_cache(cache_path)
end