moderate 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c7c7f6fd1cfa1c2ba6b28e2d0b75ce803d0ae2eec002011b18a9a25b2b98ce6f
4
+ data.tar.gz: 71bab2651af6c3ff8520360281811b53d21a711496a9ef392334e8b2a59646b5
5
+ SHA512:
6
+ metadata.gz: adfb034df21d5ecfa8fec498f34df2d6595d33033f05e7fb2832ec52f7c0012fa43806950b9c9d723cc0508563159972e57ceb051e3263ca0cfff11583bb9959
7
+ data.tar.gz: 2dd06af75442e89d39e18f44c7dc61e37030dfbee6406f5be096903418489f9061dea5262aeaa874431ec4c3ac82f3c93933036d7715e3255e6431715c6923ff
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2024-11-03
2
+
3
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Javi R
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # 👮‍♂️ `moderate` - Moderate and block bad words from your Rails app
2
+
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.
4
+
5
+ Simply add this to your model:
6
+
7
+ ```ruby
8
+ validates :text_field, moderate: true
9
+ ```
10
+
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 can improve the gem, or have good ideas for additional features.
15
+
16
+ # Why
17
+
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.
19
+
20
+ It's good for Rails applications where you need to maintain a clean and respectful environment in comments, posts, or any other user input.
21
+
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
+
26
+ ## Installation
27
+
28
+ Add this line to your application's Gemfile:
29
+
30
+ ```ruby
31
+ gem 'moderate'
32
+ ```
33
+
34
+ And then execute:
35
+
36
+ ```bash
37
+ bundle install
38
+ ```
39
+
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
+
66
+ ## Development
67
+
68
+ 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.
69
+
70
+ To install this gem onto your local machine, run `bundle exec rake install`.
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rameerez/moderate. Our code of conduct is: just be nice and make your mom proud of what you do and post online.
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moderate
4
+ class Text
5
+ class << self
6
+ def bad_words?(text)
7
+ return false if text.blank?
8
+
9
+ @words_set ||= Set.new(compute_word_list)
10
+ text.downcase.split(/\W+/).any? { |word| @words_set.include?(word) }
11
+ end
12
+
13
+ private
14
+
15
+ def compute_word_list
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
26
+ end
27
+
28
+ def reset_word_list!
29
+ @words_set = nil
30
+ @default_words = nil
31
+ end
32
+
33
+ def logger
34
+ @logger ||= defined?(Rails) ? Rails.logger : Logger.new($stdout)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModel
4
+ module Validations
5
+ class ModerateValidator < EachValidator
6
+ def validate_each(record, attribute, value)
7
+ return if value.blank?
8
+
9
+ begin
10
+ text = value.to_s
11
+ return if text.nil?
12
+
13
+ if Moderate::Text.bad_words?(text)
14
+ record.errors.add(attribute, options[:message] || Moderate.configuration.error_message)
15
+ end
16
+ rescue Moderate::Error => e
17
+ Rails.logger.error "Moderate validation error: #{e.message}"
18
+ record.errors.add(attribute, "Moderation check failed: #{e.message}")
19
+ rescue StandardError => e
20
+ Rails.logger.error "Unexpected error in moderate validation: #{e.message}"
21
+ record.errors.add(attribute, "Moderation check failed")
22
+ end
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moderate
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,90 @@
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
+ CACHE_TTL = 30 * 24 * 60 * 60 # 30 days in seconds
12
+
13
+ class << self
14
+ def load
15
+ cache_path = cache_file_path
16
+
17
+ begin
18
+ if File.exist?(cache_path)
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
26
+ end
27
+
28
+ logger.info("[moderate gem] No cache found. Downloading word list for the first time...")
29
+ download_and_cache(cache_path)
30
+ rescue StandardError => e
31
+ logger.error("[moderate gem] Error loading word list: #{e.message}")
32
+ logger.debug("[moderate gem] #{e.backtrace.join("\n")}")
33
+ raise Moderate::Error, "Failed to load bad words list: #{e.message}"
34
+ end
35
+ end
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
+
44
+ private
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
+
53
+ def cache_file_path
54
+ if defined?(Rails)
55
+ Rails.root.join('tmp', 'moderate_bad_words.txt')
56
+ else
57
+ File.join(Dir.tmpdir, 'moderate_bad_words.txt')
58
+ end
59
+ end
60
+
61
+ def download_and_cache(cache_path)
62
+ uri = URI(WORD_LIST_URL)
63
+ response = Net::HTTP.get_response(uri)
64
+
65
+ unless response.is_a?(Net::HTTPSuccess)
66
+ raise Moderate::Error, "Failed to download word list. HTTP Status: #{response.code}"
67
+ end
68
+
69
+ content = response.body.force_encoding('UTF-8')
70
+ words = content.split("\n").map(&:strip).reject(&:empty?).to_set
71
+
72
+ if words.empty?
73
+ raise Moderate::Error, "Downloaded word list is empty"
74
+ end
75
+
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
+
79
+ File.write(cache_path, content, encoding: 'UTF-8')
80
+ logger.debug("[moderate gem] Cached word list to: #{cache_path}")
81
+
82
+ words
83
+ end
84
+
85
+ def logger
86
+ @logger ||= defined?(Rails) ? Rails.logger : Logger.new($stdout)
87
+ end
88
+ end
89
+ end
90
+ end
data/lib/moderate.rb ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "moderate/version"
4
+ require_relative "moderate/text"
5
+ require_relative "moderate/text_validator"
6
+ require_relative "moderate/word_list"
7
+
8
+ module Moderate
9
+ class Error < StandardError; end
10
+
11
+ class << self
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def configuration=(config)
17
+ @configuration = config
18
+ end
19
+
20
+ def configure
21
+ yield(configuration)
22
+ end
23
+ end
24
+
25
+ class Configuration
26
+ attr_accessor :error_message, :additional_words, :excluded_words
27
+
28
+ def initialize
29
+ @error_message = "contains moderatable content (bad words)"
30
+ @additional_words = []
31
+ @excluded_words = []
32
+ end
33
+ end
34
+ end
data/sig/moderate.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Moderate
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moderate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rameerez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0
27
+ description: Moderate user-generated content by adding a simple validation to block
28
+ bad words in any text field. Good for applications where you need to maintain a
29
+ clean and respectful environment in comments, posts, or any other user input.
30
+ email:
31
+ - rubygems@rameerez.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - CHANGELOG.md
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/moderate.rb
41
+ - lib/moderate/text.rb
42
+ - lib/moderate/text_validator.rb
43
+ - lib/moderate/version.rb
44
+ - lib/moderate/word_list.rb
45
+ - sig/moderate.rbs
46
+ homepage: https://github.com/rameerez/moderate
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ allowed_push_host: https://rubygems.org
51
+ homepage_uri: https://github.com/rameerez/moderate
52
+ source_code_uri: https://github.com/rameerez/moderate
53
+ changelog_uri: https://github.com/rameerez/moderate/blob/main/CHANGELOG.md
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.0.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.5.22
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Moderate and block bad words from your Rails app
73
+ test_files: []