8
8
module Moderate
9
9
class WordList
10
10
WORD_LIST_URL = 'https://raw.githubusercontent.com/coffee-and-fun/google-profanity-words/main/data/en.txt'
11
+ CACHE_TTL = 30 * 24 * 60 * 60 # 30 days in seconds
11
12
12
13
class << self
13
14
def load
14
15
cache_path = cache_file_path
15
16
16
17
begin
17
18
if File . exist? ( cache_path )
18
- words = File . read ( cache_path , encoding : 'UTF-8' ) . split ( "\n " ) . to_set
19
- return words unless words . empty?
19
+ if cache_valid? ( cache_path )
20
+ words = File . read ( cache_path , encoding : 'UTF-8' ) . split ( "\n " ) . to_set
21
+ return words unless words . empty?
22
+ else
23
+ logger . info ( "[moderate gem] Cache expired (older than #{ CACHE_TTL / 86400 } days). Refreshing word list..." )
24
+ return download_and_cache ( cache_path )
25
+ end
20
26
end
21
27
28
+ logger . info ( "[moderate gem] No cache found. Downloading word list for the first time..." )
22
29
download_and_cache ( cache_path )
23
30
rescue StandardError => e
24
31
logger . error ( "[moderate gem] Error loading word list: #{ e . message } " )
@@ -27,8 +34,22 @@ def load
27
34
end
28
35
end
29
36
37
+ def refresh_word_list!
38
+ logger . info ( "[moderate gem] Manually refreshing word list" )
39
+ cache_path = cache_file_path
40
+ File . delete ( cache_path ) if File . exist? ( cache_path )
41
+ download_and_cache ( cache_path )
42
+ end
43
+
30
44
private
31
45
46
+ def cache_valid? ( cache_path )
47
+ return false unless File . exist? ( cache_path )
48
+
49
+ cache_age = Time . now - File . mtime ( cache_path )
50
+ cache_age <= CACHE_TTL
51
+ end
52
+
32
53
def cache_file_path
33
54
if defined? ( Rails )
34
55
Rails . root . join ( 'tmp' , 'moderate_bad_words.txt' )
@@ -52,7 +73,9 @@ def download_and_cache(cache_path)
52
73
raise Moderate ::Error , "Downloaded word list is empty"
53
74
end
54
75
55
- logger . info ( "[moderate gem] Downloaded #{ words . size } words from #{ WORD_LIST_URL } " )
76
+ logger . info ( "[moderate gem] Successfully downloaded and cached #{ words . size } words to #{ cache_path } " )
77
+ logger . info ( "[moderate gem] Next cache refresh will occur after: #{ Time . now + CACHE_TTL } " )
78
+
56
79
File . write ( cache_path , content , encoding : 'UTF-8' )
57
80
logger . debug ( "[moderate gem] Cached word list to: #{ cache_path } " )
58
81
0 commit comments