Skip to content

Commit 6269b61

Browse files
committed
Implement actual bad word list download and caching
1 parent 814cd36 commit 6269b61

File tree

4 files changed

+120
-8
lines changed

4 files changed

+120
-8
lines changed

README.md

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
# 🗑`moderate` - Moderate and block bad words from your Rails app
1+
# 👮‍♂`moderate` - Moderate and block bad words from your Rails app
22

3-
`moderate` is a Ruby gem that moderates user-generated content by adding a simple validation to block bad words in any text field.
3+
`moderate` is a Ruby gem that moderates user-generated text content by adding a simple validation to block bad words in any text field.
44

55
Simply add this to your model:
66

77
```ruby
88
validates :text_field, moderate: true
99
```
1010

11-
That's it! You're done.
11+
That's it! You're done. `moderate` will work seamlessly with your existing validations and error messages.
12+
13+
> [!WARNING]
14+
> This gem is under development. It currently only supports a limited set of English profanity words. Word matching is very basic now, and it may be prone to false positives, and false negatives. I use it for very simple things like preventing new submissions if they contain bad words, but the gem can be improved for more complex use cases and sophisticated matching and content moderation. Please consider contributing if you have good ideas for additional features.
1215
1316
# Why
1417

15-
Any text field where users can input text may be a place where bad words can be used. This gem blocks records from being created if they contain bad words.
18+
Any text field where users can input text may be a place where bad words can be used. This gem blocks records from being created if they contain bad words, profanity, naughty / obscene words, etc.
1619

1720
It's good for Rails applications where you need to maintain a clean and respectful environment in comments, posts, or any other user input.
1821

22+
# How
23+
24+
`moderate` currently downloads a list of ~1k English profanity words from the [google-profanity-words](https://github.com/coffee-and-fun/google-profanity-words) repository and caches it in your Rails app's tmp directory.
25+
1926
## Installation
2027

2128
Add this line to your application's Gemfile:
@@ -30,6 +37,32 @@ And then execute:
3037
bundle install
3138
```
3239

40+
Then, just add the `moderate` validation to any model with a text field:
41+
42+
```ruby
43+
validates :text_field, moderate: true
44+
```
45+
46+
`moderate` will raise an error if a bad word is found in the text field, preventing the record from being saved.
47+
48+
It works seamlessly with your existing validations and error messages.
49+
50+
## Configuration
51+
52+
You can configure the `moderate` gem behavior by adding a `config/initializers/moderate.rb` file:
53+
```ruby
54+
Moderate.configure do |config|
55+
# Custom error message when bad words are found
56+
config.error_message = "contains inappropriate language"
57+
58+
# Add your own words to the blacklist
59+
config.additional_words = ["badword1", "badword2"]
60+
61+
# Exclude words from the default list (false positives)
62+
config.excluded_words = ["good"]
63+
end
64+
```
65+
3366
## Development
3467

3568
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

lib/moderate.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative "moderate/version"
44
require_relative "moderate/text"
55
require_relative "moderate/text_validator"
6+
require_relative "moderate/word_list"
67

78
module Moderate
89
class Error < StandardError; end

lib/moderate/text.rb

+15-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,26 @@ def bad_words?(text)
1212

1313
private
1414

15-
DEFAULT_BAD_WORDS = Set.new(["asdf"]).freeze
16-
1715
def compute_word_list
18-
(DEFAULT_BAD_WORDS + Moderate.configuration.additional_words -
19-
Moderate.configuration.excluded_words).to_set
16+
@default_words ||= begin
17+
words = WordList.load
18+
logger.info("[moderate gem] Loaded #{words.size} words from word list")
19+
words
20+
end
21+
22+
result = (@default_words + Moderate.configuration.additional_words -
23+
Moderate.configuration.excluded_words).to_set
24+
logger.debug("[moderate gem] Final word list size: #{result.size}")
25+
result
2026
end
2127

2228
def reset_word_list!
2329
@words_set = nil
30+
@default_words = nil
31+
end
32+
33+
def logger
34+
@logger ||= defined?(Rails) ? Rails.logger : Logger.new($stdout)
2435
end
2536
end
2637
end

lib/moderate/word_list.rb

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
3+
require 'net/http'
4+
require 'uri'
5+
require 'tmpdir'
6+
require 'logger'
7+
8+
module Moderate
9+
class WordList
10+
WORD_LIST_URL = 'https://raw.githubusercontent.com/coffee-and-fun/google-profanity-words/main/data/en.txt'
11+
12+
class << self
13+
def load
14+
cache_path = cache_file_path
15+
16+
begin
17+
if File.exist?(cache_path)
18+
words = File.read(cache_path, encoding: 'UTF-8').split("\n").to_set
19+
return words unless words.empty?
20+
end
21+
22+
download_and_cache(cache_path)
23+
rescue StandardError => e
24+
logger.error("[moderate gem] Error loading word list: #{e.message}")
25+
logger.debug("[moderate gem] #{e.backtrace.join("\n")}")
26+
raise Moderate::Error, "Failed to load bad words list: #{e.message}"
27+
end
28+
end
29+
30+
private
31+
32+
def cache_file_path
33+
if defined?(Rails)
34+
Rails.root.join('tmp', 'moderate_bad_words.txt')
35+
else
36+
File.join(Dir.tmpdir, 'moderate_bad_words.txt')
37+
end
38+
end
39+
40+
def download_and_cache(cache_path)
41+
uri = URI(WORD_LIST_URL)
42+
response = Net::HTTP.get_response(uri)
43+
44+
unless response.is_a?(Net::HTTPSuccess)
45+
raise Moderate::Error, "Failed to download word list. HTTP Status: #{response.code}"
46+
end
47+
48+
content = response.body.force_encoding('UTF-8')
49+
words = content.split("\n").map(&:strip).reject(&:empty?).to_set
50+
51+
if words.empty?
52+
raise Moderate::Error, "Downloaded word list is empty"
53+
end
54+
55+
logger.info("[moderate gem] Downloaded #{words.size} words from #{WORD_LIST_URL}")
56+
File.write(cache_path, content, encoding: 'UTF-8')
57+
logger.debug("[moderate gem] Cached word list to: #{cache_path}")
58+
59+
words
60+
end
61+
62+
def logger
63+
@logger ||= defined?(Rails) ? Rails.logger : Logger.new($stdout)
64+
end
65+
end
66+
end
67+
end

0 commit comments

Comments
 (0)