obscenity2 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gitignore +26 -0
- data/.travis.yml +6 -0
- data/Gemfile +17 -0
- data/LICENSE.txt +20 -0
- data/README.md +295 -0
- data/Rakefile +52 -0
- data/config/blacklist.yml +566 -0
- data/config/international.yml +820 -0
- data/lib/obscenity/active_model.rb +18 -0
- data/lib/obscenity/base.rb +71 -0
- data/lib/obscenity/config.rb +52 -0
- data/lib/obscenity/error.rb +7 -0
- data/lib/obscenity/rack.rb +91 -0
- data/lib/obscenity/rspec_matcher.rb +9 -0
- data/lib/obscenity/version.rb +5 -0
- data/lib/obscenity.rb +38 -0
- data/obscenity.gemspec +20 -0
- data/test/helper.rb +26 -0
- data/test/static/422.html +1 -0
- data/test/test_active_model.rb +69 -0
- data/test/test_base.rb +258 -0
- data/test/test_config.rb +66 -0
- data/test/test_obscenity.rb +69 -0
- data/test/test_rack.rb +207 -0
- data/test/test_version.rb +9 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dd43f7bf4b821f134b90ea3b4c41672695b4396d
|
4
|
+
data.tar.gz: 7429ed434dd2ddb4eed940f95ffd116294cfc6c2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7934faf72083fd329ec38980106270a2070b9c8c73da6f5e7ff10fe06b3ec398c94f3a3a27f35301b873b1cbfd52cb2f9f1055895c2539a3f1d1ec6556d51663
|
7
|
+
data.tar.gz: 4c7a8f8d574e837aa426861dde233ebc79ca2a55739c40dabda856a64a36c623e4b8c90754ef46cd41ee4cb73a03d97c1e3119055852f36401307a52d8966d65
|
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
coverage.data
|
4
|
+
|
5
|
+
# rdoc generated
|
6
|
+
rdoc
|
7
|
+
|
8
|
+
# yard generated
|
9
|
+
doc
|
10
|
+
.yardoc
|
11
|
+
|
12
|
+
# bundler
|
13
|
+
.bundle
|
14
|
+
|
15
|
+
# jeweler generated
|
16
|
+
pkg
|
17
|
+
|
18
|
+
# For MacOS:
|
19
|
+
.DS_Store
|
20
|
+
|
21
|
+
# For TextMate
|
22
|
+
*.tmproj
|
23
|
+
tmtags
|
24
|
+
|
25
|
+
*.swp
|
26
|
+
Gemfile.lock
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :development, :test do
|
6
|
+
gem 'test-unit'
|
7
|
+
gem 'rack'
|
8
|
+
gem 'rake'
|
9
|
+
gem 'jeweler'
|
10
|
+
gem 'shoulda'
|
11
|
+
gem 'activemodel'
|
12
|
+
gem 'coveralls', :require => false
|
13
|
+
gem 'rack-test'
|
14
|
+
gem 'rspec'
|
15
|
+
gem 'rspec-rails'
|
16
|
+
gem 'simplecov', :require => false
|
17
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Thiago Jackiw
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,295 @@
|
|
1
|
+
# Obscenity [![Build Status](https://secure.travis-ci.org/tjackiw/obscenity.png)](http://travis-ci.org/tjackiw/obscenity)
|
2
|
+
|
3
|
+
Obscenity is a profanity filter gem for Ruby/Rubinius, 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, Ruby 2.0.X, Rubinius 1.9, 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
|
+
- :nonconsonants : Replaces non consonants with '*'
|
56
|
+
- "custom string" : Replaces the profane word with the custom string
|
57
|
+
|
58
|
+
Example:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Obscenity.configure do |config|
|
62
|
+
config.blacklist = "path/to/blacklist/file.yml"
|
63
|
+
config.whitelist = ["safe", "word"]
|
64
|
+
config.replacement = :stars
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
### Basic Usage
|
69
|
+
|
70
|
+
`Obscenity.profane?(text)` analyses the content and returns `true` or `false` based on its profanity:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Obscenity.profane?("simple text")
|
74
|
+
=> false
|
75
|
+
|
76
|
+
Obscenity.profane?("text with shit")
|
77
|
+
=> true
|
78
|
+
```
|
79
|
+
|
80
|
+
`Obscenity.sanitize(text)` sanities the content and returns a new sanitized content (if profane) or the original content (if not profane):
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
Obscenity.sanitize("simple text")
|
84
|
+
=> "simple text"
|
85
|
+
|
86
|
+
Obscenity.sanitize("text with shit")
|
87
|
+
=> "text with $@!#%"
|
88
|
+
```
|
89
|
+
|
90
|
+
`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.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
Obscenity.replacement(:default).sanitize("text with shit")
|
94
|
+
=> "text with $@!#%"
|
95
|
+
|
96
|
+
Obscenity.replacement(:garbled).sanitize("text with shit")
|
97
|
+
=> "text with $@!#%"
|
98
|
+
|
99
|
+
Obscenity.replacement(:stars).sanitize("text with shit")
|
100
|
+
=> "text with ****"
|
101
|
+
|
102
|
+
Obscenity.replacement(:vowels).sanitize("text with shit")
|
103
|
+
=> "text with sh*t"
|
104
|
+
|
105
|
+
Obscenity.replacement(:nonconsonants).sanitize('Oh 5hit')
|
106
|
+
=> "Oh *h*t"
|
107
|
+
|
108
|
+
Obscenity.replacement("[censored]").sanitize("text with shit")
|
109
|
+
=> "text with [censored]"
|
110
|
+
```
|
111
|
+
|
112
|
+
`Obscenity.offensive(text)` returns an array of profane words in the given content:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
Obscenity.offensive("simple text")
|
116
|
+
=> []
|
117
|
+
|
118
|
+
Obscenity.offensive("text with shit and another biatch")
|
119
|
+
=> ["shit", "biatch"]
|
120
|
+
```
|
121
|
+
|
122
|
+
### ActiveModel
|
123
|
+
|
124
|
+
The ActiveModel component provides easy profanity validation for your models.
|
125
|
+
|
126
|
+
First, you need to explicitly require the ActiveModel component:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
require 'obscenity/active_model'
|
130
|
+
```
|
131
|
+
|
132
|
+
Then you can use it in your models as such:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
# ActiveRecord example
|
136
|
+
class Post < ActiveRecord::Base
|
137
|
+
|
138
|
+
validates :title, obscenity: true
|
139
|
+
validates :body, obscenity: { sanitize: true, replacement: "[censored]" }
|
140
|
+
end
|
141
|
+
|
142
|
+
# MongoMapper example
|
143
|
+
class Book
|
144
|
+
include MongoMapper::Document
|
145
|
+
|
146
|
+
key :title, String
|
147
|
+
key :body, String
|
148
|
+
|
149
|
+
validates :title, obscenity: true
|
150
|
+
validates :body, obscenity: { sanitize: true, replacement: :vowels }
|
151
|
+
end
|
152
|
+
|
153
|
+
# Mongoid example
|
154
|
+
class Page
|
155
|
+
include Mongoid::Document
|
156
|
+
|
157
|
+
field :title, type: String
|
158
|
+
field :body, type: String
|
159
|
+
|
160
|
+
validates :title, obscenity: true
|
161
|
+
validates :body, obscenity: { sanitize: true, replacement: :garbled }
|
162
|
+
end
|
163
|
+
```
|
164
|
+
|
165
|
+
The following usage is available:
|
166
|
+
|
167
|
+
`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.
|
168
|
+
|
169
|
+
`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.
|
170
|
+
|
171
|
+
`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.
|
172
|
+
|
173
|
+
### Rack middleware
|
174
|
+
|
175
|
+
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.
|
176
|
+
|
177
|
+
First you need to explicitly require the Rack middleware:
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
require 'obscenity/rack'
|
181
|
+
```
|
182
|
+
|
183
|
+
And to use the middleware, the basic syntax is:
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
use Rack::Obscenity, {} # options Hash
|
187
|
+
```
|
188
|
+
|
189
|
+
You need to use the options below inside the options Hash (above)
|
190
|
+
|
191
|
+
#### Rejecting Requests:
|
192
|
+
|
193
|
+
Any of the following options can be used to reject a request.
|
194
|
+
|
195
|
+
`reject: true` : will reject a request if any parameter value contains profanity.
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
use Rack::Obscenity, reject: true
|
199
|
+
```
|
200
|
+
|
201
|
+
`reject: { params: [] }` : will analyze the selected parameters and reject the request if their values contain profanity.
|
202
|
+
|
203
|
+
```ruby
|
204
|
+
use Rack::Obscenity, reject: { params: [:foo, :bar] }
|
205
|
+
```
|
206
|
+
|
207
|
+
`reject: { message: 'Custom message' }` : will reject a request and display the custom message if any parameter value contains profanity
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
use Rack::Obscenity, reject: { message: "We don't allow profanity!" }
|
211
|
+
```
|
212
|
+
|
213
|
+
`reject: { path: 'path/to/file' }` : will reject a request and render the custom file if any parameter value contains profanity
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
use Rack::Obscenity, reject: { path: 'public/no_profanity.html' }
|
217
|
+
```
|
218
|
+
|
219
|
+
More usage example:
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
# Rejects the request for all params and renders a file
|
223
|
+
use Rack::Obscenity, reject: { params: :all, path: 'public/no_profanity.html' }
|
224
|
+
|
225
|
+
# Rejects the request based on the selected params and renders a file
|
226
|
+
use Rack::Obscenity, reject: { params: [:foo, :bar], path: 'public/no_profanity.html' }
|
227
|
+
|
228
|
+
# Rejects the request based on the selected params and displays a message
|
229
|
+
use Rack::Obscenity, reject: { params: [:foo, :bar], message: "Profanity is not allowed!" }
|
230
|
+
```
|
231
|
+
|
232
|
+
#### Sanitizing Requests:
|
233
|
+
|
234
|
+
Any of the following options can be used to sanitize a request.
|
235
|
+
|
236
|
+
`sanitize: true` : will sanitize all parameter values if they contain profanity.
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
use Rack::Obscenity, sanitize: true
|
240
|
+
```
|
241
|
+
|
242
|
+
`sanitize: { params: [] }` : will analyze the selected parameters and sanitize them if their values contain profanity.
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
use Rack::Obscenity, sanitize: { params: [:foo, :bar] }
|
246
|
+
```
|
247
|
+
|
248
|
+
`sanitize: { replacement: (:default | :garbled | :stars | :vowels | 'custom') }` : will use this replacement method when sanitizing parameter values
|
249
|
+
|
250
|
+
```ruby
|
251
|
+
use Rack::Obscenity, sanitize: { replacement: :vowels }
|
252
|
+
```
|
253
|
+
|
254
|
+
More usage example:
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
# Sanitizes all params and replaces their values using :stars
|
258
|
+
use Rack::Obscenity, sanitize: { params: :all, replacement: :stars }
|
259
|
+
|
260
|
+
# Sanitizes the given params and replaces their values using a custom word
|
261
|
+
use Rack::Obscenity, sanitize: { params: [:foo, :bar], replacement: "[censored]" }
|
262
|
+
|
263
|
+
# Sanitizes all params and replaces their values using :garbled
|
264
|
+
use Rack::Obscenity, sanitize: { replacement: :garbled }
|
265
|
+
```
|
266
|
+
### Test Helpers
|
267
|
+
|
268
|
+
Obscenity currently provides test helpers for RSpec only, but we have plans to add helpers to Shoulda as well.
|
269
|
+
|
270
|
+
#### RSpec Matcher
|
271
|
+
|
272
|
+
A `be_profane` matcher is available for RSpec. Its usage is very simple:
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
user.username.should_not be_profane
|
276
|
+
```
|
277
|
+
|
278
|
+
## Contributing to obscenity
|
279
|
+
|
280
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
281
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
282
|
+
* Fork the project.
|
283
|
+
* Start a feature/bugfix branch.
|
284
|
+
* Commit and push until you are happy with your contribution.
|
285
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
286
|
+
* Please try not to mess with the Rakefile, version, or history.
|
287
|
+
|
288
|
+
## Authors
|
289
|
+
|
290
|
+
* Thiago Jackiw: [@tjackiw](http://twitter.com/tjackiw)
|
291
|
+
|
292
|
+
## Copyright
|
293
|
+
|
294
|
+
Copyright (c) 2012 Thiago Jackiw. See LICENSE.txt for further details.
|
295
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'rake'
|
14
|
+
require 'jeweler'
|
15
|
+
|
16
|
+
$:.push File.expand_path("../lib", __FILE__)
|
17
|
+
require "obscenity/version"
|
18
|
+
|
19
|
+
Jeweler::Tasks.new do |gem|
|
20
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
21
|
+
gem.name = "obscenity"
|
22
|
+
gem.version = Obscenity::VERSION
|
23
|
+
gem.authors = ["Thiago Jackiw"]
|
24
|
+
gem.email = "tjackiw@gmail.com"
|
25
|
+
gem.homepage = "http://github.com/tjackiw/obscenity"
|
26
|
+
gem.license = "MIT"
|
27
|
+
gem.summary = %Q{ Obscenity is a profanity filter gem for Ruby/Rubinius, Rails (through ActiveModel), and Rack middleware }
|
28
|
+
gem.description = %Q{ Obscenity is a profanity filter gem for Ruby/Rubinius, Rails (through ActiveModel), and Rack middleware }
|
29
|
+
gem.files = `git ls-files`.split("\n")
|
30
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
31
|
+
gem.require_paths = ["lib"]
|
32
|
+
end
|
33
|
+
Jeweler::RubygemsDotOrgTasks.new
|
34
|
+
|
35
|
+
require 'rake/testtask'
|
36
|
+
Rake::TestTask.new(:test) do |test|
|
37
|
+
test.libs << 'lib' << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rdoc/task'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "obscenity #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|