|
| 1 | +# Obscenity |
| 2 | + |
| 3 | +Obscenity is a profanity filter gem for Ruby, Rails (through ActiveModel), and Rack middleware. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add this line to your application's Gemfile: |
| 8 | + |
| 9 | + ``` ruby |
| 10 | + gem 'obscenity' |
| 11 | + ``` |
| 12 | + |
| 13 | +And then execute: |
| 14 | + |
| 15 | + ``` ruby |
| 16 | + bundle install |
| 17 | + ``` |
| 18 | + |
| 19 | +Or install it yourself as: |
| 20 | + |
| 21 | + ``` ruby |
| 22 | + gem install obscenity |
| 23 | + ``` |
| 24 | + |
| 25 | +## Compatibility |
| 26 | + |
| 27 | +Obscenity is compatible with Ruby 1.9.X, Rails 3.X, and Rack as a middleware. Starting with Rails 3, the profanity validation works with any ORM supported by ActiveModel, e.g: ActiveRecord, MongoMapper, Mongoid, etc. |
| 28 | + |
| 29 | +## Using Obscenity |
| 30 | + |
| 31 | +The following methods are available to use with Obscenity: |
| 32 | + |
| 33 | +### Configuration |
| 34 | + |
| 35 | +`Obscenity.configure(&block)` allows you to set custom global configuration options. Available options are: |
| 36 | + |
| 37 | +`config.blacklist` accepts the following values: |
| 38 | + |
| 39 | +- An array with words |
| 40 | +- A string representing a path to a yml file |
| 41 | +- A Pathname object with a path to a yml file |
| 42 | + |
| 43 | +`config.whitelist` accepts the following values: |
| 44 | + |
| 45 | +- An array with words |
| 46 | +- A string representing a path to a yml file |
| 47 | +- A Pathname object with a path to a yml file |
| 48 | + |
| 49 | +`config.replacement` accepts the following values: |
| 50 | + |
| 51 | +- :default : Uses the :garbled method |
| 52 | +- :garbled : Replaces profane words with $@!#% |
| 53 | +- :stars : Replaces profane words with '*' up to the word's length |
| 54 | +- :vowels : Replaces the vowels in the profane word with '*' |
| 55 | +- "custom string" : Replaces the profane word with the custom string |
| 56 | + |
| 57 | +Example: |
| 58 | + |
| 59 | + ``` ruby |
| 60 | + Obscenity.configure do |config| |
| 61 | + config.blacklist = "path/to/blacklist/file.yml" |
| 62 | + config.whitelist = ["safe", "word"] |
| 63 | + config.replacement = :stars |
| 64 | + end |
| 65 | + ``` |
| 66 | + |
| 67 | +### Basic Usage |
| 68 | + |
| 69 | +`Obscenity.profane?(text)` analyses the content and returns `true` or `false` based on its profanity: |
| 70 | + |
| 71 | + ``` ruby |
| 72 | + Obscenity.profane?("simple text") |
| 73 | + => false |
| 74 | + |
| 75 | + Obscenity.profane?("text with shit") |
| 76 | + => true |
| 77 | + ``` |
| 78 | + |
| 79 | +`Obscenity.sanitize(text)` sanities the content and returns a new sanitized content (if profane) or the original content (if not profane): |
| 80 | + |
| 81 | + ``` ruby |
| 82 | + Obscenity.sanitize("simple text") |
| 83 | + => "simple text" |
| 84 | + |
| 85 | + Obscenity.sanitize("text with shit") |
| 86 | + => "text with $@!#%" |
| 87 | + ``` |
| 88 | + |
| 89 | +`Obscenity.replacement(style).sanitize(text)` allows you to pass the replacement method to be used when sanitizing the given content. Available replacement values are `:default`, `:garbled`, `:stars`, `:vowels`, and a custom string. |
| 90 | + |
| 91 | + ``` ruby |
| 92 | + Obscenity.replacement(:default).sanitize("text with shit") |
| 93 | + => "text with $@!#%" |
| 94 | + |
| 95 | + Obscenity.replacement(:garbled).sanitize("text with shit") |
| 96 | + => "text with $@!#%" |
| 97 | + |
| 98 | + Obscenity.replacement(:stars).sanitize("text with shit") |
| 99 | + => "text with ****" |
| 100 | + |
| 101 | + Obscenity.replacement(:vowels).sanitize("text with shit") |
| 102 | + => "text with sh*t" |
| 103 | + |
| 104 | + Obscenity.replacement("[censored]").sanitize("text with shit") |
| 105 | + => "text with [censored]" |
| 106 | + ``` |
| 107 | + |
| 108 | +`Obscenity.offensive(text)` returns an array of profane words in the given content: |
| 109 | + |
| 110 | + ``` ruby |
| 111 | + Obscenity.offensive("simple text") |
| 112 | + => [] |
| 113 | + |
| 114 | + Obscenity.offensive("text with shit and another biatch") |
| 115 | + => ["shit", "biatch"] |
| 116 | + ``` |
| 117 | + |
| 118 | +### ActiveModel |
| 119 | + |
| 120 | +The ActiveModel component provides easy profanity validation for your models. |
| 121 | + |
| 122 | +First, you need to explicitly require the ActiveModel component: |
| 123 | + |
| 124 | + ``` ruby |
| 125 | + require 'obscenity/active_model' |
| 126 | + ``` |
| 127 | + |
| 128 | +Then you can use it in your models as such: |
| 129 | + |
| 130 | + ``` ruby |
| 131 | + # ActiveRecord example |
| 132 | + class Post < ActiveRecord::Base |
| 133 | + |
| 134 | + validates :title, obscenity: true |
| 135 | + validates :body, obscenity: { sanitize: true, replacement: "[censored]" } |
| 136 | + end |
| 137 | + |
| 138 | + # MongoMapper example |
| 139 | + class Book |
| 140 | + include MongoMapper::Document |
| 141 | + |
| 142 | + key :title, String |
| 143 | + key :body, String |
| 144 | + |
| 145 | + validates :title, obscenity: true |
| 146 | + validates :body, obscenity: { sanitize: true, replacement: :vowels } |
| 147 | + end |
| 148 | + |
| 149 | + # Mongoid example |
| 150 | + class Page |
| 151 | + include Mongoid::Document |
| 152 | + |
| 153 | + field :title, type: String |
| 154 | + field :body, type: String |
| 155 | + |
| 156 | + validates :title, obscenity: true |
| 157 | + validates :body, obscenity: { sanitize: true, replacement: :garbled } |
| 158 | + end |
| 159 | + ``` |
| 160 | + |
| 161 | +The following usage is available: |
| 162 | + |
| 163 | +`obscenity: true` : Does a profanity validation in the field using `.profane?` and returns `true/false`. If true, ActiveModel's Validation will return a default error message. |
| 164 | + |
| 165 | +`obscenity: { message: 'Custom message' }` : Does a profanity validation in the field using `.profane?` and returns `true/false`. If true, ActiveModel's Validation will return your custom error message. |
| 166 | + |
| 167 | +`obscenity: { sanitize: true }` : Silently sanitizes the field and replaces its content with the sanitized version. If the `:replacement` key is included, it will use that style when replacing the content. |
| 168 | + |
| 169 | +### Rack middleware |
| 170 | + |
| 171 | +You can use Obscenity as a Rack middleware to automatically reject requests that include profane parameter values or sanitize those values before they reach your Application. |
| 172 | + |
| 173 | +First you need to explicitly require the Rack middleware: |
| 174 | + |
| 175 | + ``` ruby |
| 176 | + require 'obscenity/rack' |
| 177 | + ``` |
| 178 | +And to use the middleware, the basic syntax is: |
| 179 | + |
| 180 | + ``` ruby |
| 181 | + use Rack::Obscenity, {} # options Hash |
| 182 | + ``` |
| 183 | + |
| 184 | +You need to use the options below inside the options Hash (above) |
| 185 | + |
| 186 | +#### Rejecting Requests: |
| 187 | + |
| 188 | +Any of the following options can be used to reject a request. |
| 189 | + |
| 190 | +`reject: true` : will reject a request if any parameter value contains profanity. |
| 191 | + |
| 192 | + ``` ruby |
| 193 | + use Rack::Obscenity, reject: true |
| 194 | + ``` |
| 195 | +`reject: { params: [] }` : will analyze the selected parameters and reject the request if their values contain profanity. |
| 196 | + |
| 197 | + ``` ruby |
| 198 | + use Rack::Obscenity, reject: { params: [:foo, :bar] } |
| 199 | + ``` |
| 200 | + |
| 201 | +`reject: { message: 'Custom message' }` : will reject a request and display the custom message if any parameter value contains profanity |
| 202 | + |
| 203 | + ``` ruby |
| 204 | + use Rack::Obscenity, reject: { message: "We don't allow profanity!" } |
| 205 | + ``` |
| 206 | + |
| 207 | +`reject: { path: 'path/to/file' }` : will reject a request and render the custom file if any parameter value contains profanity |
| 208 | + |
| 209 | + ``` ruby |
| 210 | + use Rack::Obscenity, reject: { path: 'public/no_profanity.html' } |
| 211 | + ``` |
| 212 | + |
| 213 | +More usage example: |
| 214 | + |
| 215 | + ``` ruby |
| 216 | + # Rejects the request for all params and renders a file |
| 217 | + use Rack::Obscenity, reject: { params: :all, path: 'public/no_profanity.html' } |
| 218 | + |
| 219 | + # Rejects the request based on the selected params and renders a file |
| 220 | + use Rack::Obscenity, reject: { params: [:foo, :bar], path: 'public/no_profanity.html' } |
| 221 | + |
| 222 | + # Rejects the request based on the selected params and displays a message |
| 223 | + use Rack::Obscenity, reject: { params: [:foo, :bar], message: "Profanity is not allowed!" } |
| 224 | + ``` |
| 225 | + |
| 226 | +#### Sanitizing Requests: |
| 227 | + |
| 228 | +Any of the following options can be used to sanitize a request. |
| 229 | + |
| 230 | +`sanitize: true` : will sanitize all parameter values if they contain profanity. |
| 231 | + |
| 232 | + ``` ruby |
| 233 | + use Rack::Obscenity, sanitize: true |
| 234 | + ``` |
| 235 | +`sanitize: { params: [] }` : will analyze the selected parameters and sanitize them if their values contain profanity. |
| 236 | + |
| 237 | + ``` ruby |
| 238 | + use Rack::Obscenity, sanitize: { params: [:foo, :bar] } |
| 239 | + ``` |
| 240 | + |
| 241 | +`sanitize: { replacement: (:default | :garbled | :stars | :vowels | 'custom') }` : will use this replacement method when sanitizing parameter values |
| 242 | + |
| 243 | + ``` ruby |
| 244 | + use Rack::Obscenity, sanitize: { replacement: :vowels } |
| 245 | + ``` |
| 246 | + |
| 247 | +More usage example: |
| 248 | + |
| 249 | + ``` ruby |
| 250 | + # Sanitizes all params and replaces their values using :stars |
| 251 | + use Rack::Obscenity, sanitize: { params: :all, replacement: :stars } |
| 252 | + |
| 253 | + # Sanitizes the given params and replaces their values using a custom word |
| 254 | + use Rack::Obscenity, sanitize: { params: [:foo, :bar], replacement: "[censored]" } |
| 255 | + |
| 256 | + # Sanitizes all params and replaces their values using :garbled |
| 257 | + use Rack::Obscenity, sanitize: { replacement: :garbled } |
| 258 | + ``` |
| 259 | + |
| 260 | +## Contributing to obscenity |
| 261 | + |
| 262 | +* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. |
| 263 | +* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. |
| 264 | +* Fork the project. |
| 265 | +* Start a feature/bugfix branch. |
| 266 | +* Commit and push until you are happy with your contribution. |
| 267 | +* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. |
| 268 | +* Please try not to mess with the Rakefile, version, or history. |
| 269 | + |
| 270 | +## Authors |
| 271 | + |
| 272 | +* Thiago Jackiw: [@tjackiw](http://twitter.com/tjackiw) |
| 273 | + |
| 274 | +## Copyright |
| 275 | + |
| 276 | +Copyright (c) 2012 Thiago Jackiw. See LICENSE.txt for further details. |
| 277 | + |
0 commit comments