blacklist_validator 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in blacklist_validator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Axel Vergult
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,41 @@
1
+ Installation
2
+ ------------
3
+
4
+ Add this line to your Gemfile:
5
+
6
+ gem 'blacklist_validator'
7
+
8
+ Then run:
9
+
10
+ bundle install
11
+
12
+ Usage
13
+ -----
14
+
15
+ Add the following to one of your models:
16
+
17
+ validates :name, blacklist: true
18
+
19
+ You can also modify the default message ("is blacklisted") if validation fails:
20
+
21
+ validates :name, blacklist: { message: "is not part of the whitelist" }
22
+
23
+ Blacklist file
24
+ --------------
25
+
26
+ You can create a blacklist.yml file in the config directory of your Rails application if you need to overload the one used by this gem.
27
+
28
+ I18n
29
+ ----
30
+
31
+ If you use I18n, the default key to translate is :blacklisted. So if you add to your User model:
32
+
33
+ validates :name, blacklist: true
34
+
35
+ You can translate (or overload) the default message via for e.g. using activerecord (in english): "en.activerecord.errors.models.user.attributes.name.blacklisted"
36
+
37
+ Tests
38
+ -----
39
+
40
+ cd test
41
+ ruby blacklist_validator_test.rb
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "blacklist_validator"
6
+ s.version = "0.0.1"
7
+ s.authors = ["Axel Vergult"]
8
+ s.email = ["axel.vergult@gmail.com"]
9
+ s.homepage = "https://github.com/episko/blacklist_validator"
10
+ s.summary = %q{Blacklist validator for Rails 3}
11
+ s.description = %q{Blacklist validator for Rails 3}
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+ s.add_dependency("activemodel", ">= 0")
18
+ end
@@ -0,0 +1,264 @@
1
+ ---
2
+ - 4r5e
3
+ - 5h1t
4
+ - 5hit
5
+ - a55
6
+ - anal
7
+ - anus
8
+ - ar5e
9
+ - arrse
10
+ - arse
11
+ - ass
12
+ - ass-fucker
13
+ - asshole
14
+ - asswhole
15
+ - a_s_s
16
+ - b!tch
17
+ - b00bs
18
+ - b17ch
19
+ - b1tch
20
+ - ballbag
21
+ - balls
22
+ - ballsack
23
+ - bastard
24
+ - beastial
25
+ - beastiality
26
+ - bellend
27
+ - bestial
28
+ - bestiality
29
+ - bi+ch
30
+ - biatch
31
+ - bitch
32
+ - bloody
33
+ - blow job
34
+ - blowjob
35
+ - boiolas
36
+ - bollock
37
+ - bollok
38
+ - boner
39
+ - boob
40
+ - booobs
41
+ - boooobs
42
+ - booooobs
43
+ - booooooobs
44
+ - breasts
45
+ - buceta
46
+ - bugger
47
+ - bum
48
+ - bunny fucker
49
+ - butt
50
+ - c0ck
51
+ - carpet muncher
52
+ - cawk
53
+ - chink
54
+ - cipa
55
+ - cl1t
56
+ - clit
57
+ - clitoris
58
+ - clits
59
+ - cnut
60
+ - cock
61
+ - cock-sucker
62
+ - cok
63
+ - coon
64
+ - cox
65
+ - crap
66
+ - cum
67
+ - cunilingus
68
+ - cunillingus
69
+ - cunnilingus
70
+ - cunt
71
+ - cyalis
72
+ - cyberfuc
73
+ - cyberfuck
74
+ - d1ck
75
+ - damn
76
+ - dick
77
+ - dildo
78
+ - dink
79
+ - dirsa
80
+ - dlck
81
+ - dog-fucker
82
+ - doggin
83
+ - donkeyribber
84
+ - doosh
85
+ - duche
86
+ - dyke
87
+ - ejaculate
88
+ - ejaculating
89
+ - ejaculation
90
+ - ejakulate
91
+ - f u c k
92
+ - f u c k e r
93
+ - f4nny
94
+ - fag
95
+ - fanny
96
+ - fanyy
97
+ - fatass
98
+ - fcuk
99
+ - feck
100
+ - felching
101
+ - fellate
102
+ - fellatio
103
+ - fingerfuck
104
+ - fistfuck
105
+ - flange
106
+ - fook
107
+ - fuck
108
+ - fudge packer
109
+ - fudgepacker
110
+ - fuk
111
+ - fux
112
+ - fux0r
113
+ - f_u_c_k
114
+ - gangbang
115
+ - gaylord
116
+ - gaysex
117
+ - goatse
118
+ - God
119
+ - god-dam
120
+ - god-damned
121
+ - goddamn
122
+ - goddamned
123
+ - hardcoresex
124
+ - hell
125
+ - heshe
126
+ - hoar
127
+ - hoare
128
+ - hoer
129
+ - homo
130
+ - hore
131
+ - horniest
132
+ - horny
133
+ - hotsex
134
+ - jack-off
135
+ - jackoff
136
+ - jap
137
+ - jerk-off
138
+ - jism
139
+ - jiz
140
+ - kawk
141
+ - knob
142
+ - kock
143
+ - kondum
144
+ - kum
145
+ - kummer
146
+ - kunilingus
147
+ - l3i+ch
148
+ - l3itch
149
+ - labia
150
+ - lmfao
151
+ - lust
152
+ - m0f0
153
+ - m0fo
154
+ - m45terbate
155
+ - ma5terb8
156
+ - ma5terbate
157
+ - masochist
158
+ - master-bate
159
+ - masterb8
160
+ - masterbat
161
+ - mo-fo
162
+ - mof0
163
+ - mofo
164
+ - mothafuck
165
+ - motherfuck
166
+ - motherfuckka
167
+ - muff
168
+ - mutha
169
+ - muthafecker
170
+ - muthafuckker
171
+ - muther
172
+ - mutherfucker
173
+ - n1gga
174
+ - n1gger
175
+ - nazi
176
+ - nigg3r
177
+ - nigg4h
178
+ - nigga
179
+ - nigger
180
+ - nob
181
+ - numbnuts
182
+ - nutsack
183
+ - orgasim
184
+ - orgasm
185
+ - p0rn
186
+ - pawn
187
+ - pecker
188
+ - penis
189
+ - phonesex
190
+ - phuck
191
+ - phuk
192
+ - phuq
193
+ - pigfucker
194
+ - pimpis
195
+ - piss
196
+ - poop
197
+ - porn
198
+ - prick
199
+ - pron
200
+ - pube
201
+ - pusse
202
+ - pussi
203
+ - pussy
204
+ - rectum
205
+ - retard
206
+ - rimjaw
207
+ - rimming
208
+ - s hit
209
+ - s.o.b.
210
+ - sadist
211
+ - schlong
212
+ - screwing
213
+ - scroat
214
+ - scrote
215
+ - scrotum
216
+ - semen
217
+ - sex
218
+ - sh!+
219
+ - sh!t
220
+ - sh1t
221
+ - shag
222
+ - shagger
223
+ - shaggin
224
+ - shagging
225
+ - shemale
226
+ - shi+
227
+ - shit
228
+ - skank
229
+ - slut
230
+ - smegma
231
+ - smut
232
+ - snatch
233
+ - son-of-a-bitch
234
+ - spac
235
+ - spunk
236
+ - s_h_i_t
237
+ - t1tt1e5
238
+ - t1tties
239
+ - teets
240
+ - teez
241
+ - testical
242
+ - testicle
243
+ - tit
244
+ - tosser
245
+ - turd
246
+ - tw4t
247
+ - twat
248
+ - twunt
249
+ - v14gra
250
+ - v1gra
251
+ - vagina
252
+ - viagra
253
+ - vulva
254
+ - w00se
255
+ - wang
256
+ - wank
257
+ - wanker
258
+ - wanky
259
+ - whoar
260
+ - whore
261
+ - willies
262
+ - willy
263
+ - xrated
264
+ - xxx
@@ -0,0 +1,44 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ class BlacklistValidator < ActiveModel::EachValidator
4
+ attr_accessor :blacklist
5
+
6
+ def initialize(options)
7
+ load_blacklist!
8
+ super(options)
9
+ end
10
+
11
+ def validate_each(record, attribute, value)
12
+ record.errors.add(attribute, options[:message] || invalid_message(record, attribute)) if blacklisted?(value)
13
+ end
14
+
15
+ #######################
16
+ ### Private methods ###
17
+ #######################
18
+
19
+ private
20
+
21
+ def invalid_message(record, attribute)
22
+ I18n.t :blacklisted,
23
+ scope: "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
24
+ default: "is blacklisted"
25
+ end
26
+
27
+ def blacklisted?(str)
28
+ @blacklist.each { |word| return true if greedy_match?(str, word) }
29
+ false
30
+ end
31
+
32
+ def greedy_match?(str, word)
33
+ str =~ /(#{word})+/i
34
+ end
35
+
36
+ def load_blacklist!
37
+ if defined?(Rails.root) && (blacklist_file_path = Rails.root.join("config", "blacklist.yml")).exist?
38
+ blacklist_path = blacklist_file_path
39
+ end
40
+ blacklist_path ||= File.read(File.join(File.dirname(__FILE__), "../config/blacklist.yml"))
41
+ @blacklist = YAML::load(blacklist_path)
42
+ end
43
+
44
+ end
@@ -0,0 +1,53 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require_relative 'test_helper'
4
+
5
+ class TestUser < TestModel
6
+ validates :name, blacklist: true
7
+ end
8
+
9
+ class TestUserWithMessage < TestModel
10
+ validates :name, blacklist: { message: 'is not part of the whitelist' }
11
+ end
12
+
13
+ class TestBlacklistValidator < MiniTest::Unit::TestCase
14
+
15
+ def test_some_blacklisted_words
16
+ names_that_should_be_blacklisted.each do |name|
17
+ test_user = TestUser.new(name: name)
18
+ refute test_user.valid?
19
+ assert test_user.errors[:name]
20
+ end
21
+ end
22
+
23
+ def test_some_whitelisted_words
24
+ names_that_should_be_whitelisted.each { |name| assert TestUser.new(name: name).valid? }
25
+ end
26
+
27
+ def test_default_message_on_error
28
+ test_user = TestUser.new(name: "Get up, fuck up")
29
+ refute test_user.valid?
30
+ assert test_user.errors[:name].include?("is blacklisted")
31
+ end
32
+
33
+ def test_custom_message_on_error
34
+ test_user = TestUserWithMessage.new(name: "Fuck it up")
35
+ refute test_user.valid?
36
+ assert test_user.errors[:name].include?("is not part of the whitelist")
37
+ end
38
+
39
+ #######################
40
+ ### Private methods ###
41
+ #######################
42
+
43
+ private
44
+
45
+ def names_that_should_be_blacklisted
46
+ ["Fucking gay", "I have a boner", "Show me your ass"]
47
+ end
48
+
49
+ def names_that_should_be_whitelisted
50
+ ["Alice in wonderland", "The Doors", "This is a simple name"]
51
+ end
52
+
53
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'rubygems'
4
+ require 'active_model'
5
+ require 'minitest/autorun'
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+
10
+ require 'blacklist_validator'
11
+
12
+ class TestModel
13
+ include ActiveModel::Validations
14
+
15
+ def initialize(attributes = {})
16
+ @attributes = attributes
17
+ end
18
+
19
+ def read_attribute_for_validation(key)
20
+ @attributes[key]
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blacklist_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Axel Vergult
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-15 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &70198876131880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70198876131880
25
+ description: Blacklist validator for Rails 3
26
+ email:
27
+ - axel.vergult@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - blacklist_validator.gemspec
38
+ - config/blacklist.yml
39
+ - lib/blacklist_validator.rb
40
+ - test/blacklist_validator_test.rb
41
+ - test/test_helper.rb
42
+ homepage: https://github.com/episko/blacklist_validator
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.10
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Blacklist validator for Rails 3
66
+ test_files:
67
+ - test/blacklist_validator_test.rb
68
+ - test/test_helper.rb