simple_blacklist 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +5 -11
- data/bin/simple_blacklist +4 -39
- data/lib/simple_blacklist.rb +43 -16
- data/lib/simple_blacklist/setup.rb +59 -0
- data/lib/simple_blacklist/version.rb +1 -1
- data/simple_blacklist.gemspec +4 -3
- data/spec/simple_blacklist_spec.rb +32 -2
- metadata +12 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbdbf52e043433dc0d2ca2fadb094a43167cc9f6
|
4
|
+
data.tar.gz: 7ef374ce7af7a1056d4cbb6c5565092c14b67bb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f21ebbf752aa71d750668dff2e4575e49243106a1ff901e6645b2141eb8576ec3c9adfa7f444758c421b3dec22a96ab49085eb790b3f481c8b5392ba783a96a
|
7
|
+
data.tar.gz: 73dced19be6e6791e53388469271703279acedb09b95c5526b9a05275fd053e7559d3934859d1a2db14bb3f5e5a64d5cd4abfe3971962e67937e98099a1caff6
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
# SimpleBlacklist
|
2
|
-
___
|
3
2
|
|
4
3
|
This project is designed to make easy the task of sanitizing string that contains words not allowed.
|
5
4
|
|
6
5
|
## Installation
|
7
|
-
___
|
8
6
|
|
9
7
|
Add this line to your application's Gemfile:
|
10
8
|
|
@@ -21,17 +19,15 @@ Or install it yourself as:
|
|
21
19
|
$ gem install simple_blacklist
|
22
20
|
|
23
21
|
## Setup (Rails)
|
24
|
-
___
|
25
22
|
|
26
23
|
$ simple_blacklist --configure
|
27
24
|
|
28
25
|
Will be created:
|
29
26
|
|
30
|
-
*
|
31
|
-
*
|
27
|
+
* **config/initializers/simple_blacklist.rb** (file where are defined some parameters to the gem)
|
28
|
+
* **config/blacklist.yml** (list where you must add words that you want deny)
|
32
29
|
|
33
30
|
## Usage
|
34
|
-
___
|
35
31
|
|
36
32
|
In your code, do:
|
37
33
|
|
@@ -39,19 +35,18 @@ In your code, do:
|
|
39
35
|
offensive_text = "Kiss my ass"
|
40
36
|
SimpleBlacklist.sanitize(offensive_text)
|
41
37
|
|
42
|
-
# "Kiss my
|
38
|
+
# "Kiss my ****"
|
43
39
|
```
|
44
40
|
|
45
|
-
If configured
|
41
|
+
If configured **letters_variations** is also possible to sanitize texts written with special characters
|
46
42
|
```ruby
|
47
43
|
offensive_text = "Kiss my @$$"
|
48
44
|
SimpleBlacklist.sanitize(offensive_text)
|
49
45
|
|
50
|
-
# "Kiss my
|
46
|
+
# "Kiss my ****"
|
51
47
|
```
|
52
48
|
|
53
49
|
## Development
|
54
|
-
___
|
55
50
|
|
56
51
|
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.
|
57
52
|
|
@@ -63,7 +58,6 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
63
58
|
|
64
59
|
|
65
60
|
## License
|
66
|
-
___
|
67
61
|
|
68
62
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
69
63
|
|
data/bin/simple_blacklist
CHANGED
@@ -1,50 +1,15 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require '
|
3
|
-
require 'simple_blacklist/config'
|
2
|
+
require 'simple_blacklist/setup'
|
4
3
|
|
5
|
-
|
6
|
-
config_dir = File.join(base, BLACKLIST_CONFIG['config_path'])
|
7
|
-
initializers_dir = File.join(config_dir, BLACKLIST_CONFIG['initializers_path'])
|
8
|
-
FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir)
|
9
|
-
FileUtils.mkdir_p(initializers_dir) unless File.directory?(initializers_dir)
|
10
|
-
initializer_file = File.join(initializers_dir, 'simple_blacklist.rb')
|
11
|
-
blacklist_file = File.join(config_dir, 'blacklist.yml')
|
4
|
+
setup = SimpleBlacklist::Setup.new
|
12
5
|
|
13
6
|
if ARGV.include? "--configure"
|
14
|
-
|
15
|
-
# Define simple_blacklist file
|
16
|
-
SimpleBlacklist.set_blacklist_file(File.join(BLACKLIST_BASE, "config", "blacklist.yml"))
|
17
|
-
|
18
|
-
# Defines masks that will replace the words contained in the simple_blacklist
|
19
|
-
SimpleBlacklist.set_mask_to_denied_words("xxxxx")
|
20
|
-
|
21
|
-
# list of letters and characters that can replace the letters
|
22
|
-
SimpleBlacklist.set_letters_variations({
|
23
|
-
'a' => ['@', '4', 'ª'],
|
24
|
-
'e' => ['3', '&'],
|
25
|
-
'i' => ['|', '1'],
|
26
|
-
'o' => ['0', 'º'],
|
27
|
-
's' => ['$', '5'],
|
28
|
-
't' => ['7'],
|
29
|
-
'g' => ['6'],
|
30
|
-
'b' => ['6', '8']
|
31
|
-
})
|
32
|
-
FILE
|
33
|
-
|
34
|
-
blacklist_file_content = <<-FILE
|
35
|
-
ass
|
36
|
-
fuck
|
37
|
-
bitch
|
38
|
-
FILE
|
39
|
-
|
40
|
-
File.open(initializer_file, 'wb') {|f| f.write(initializer_file_content) } unless File.exist?(initializer_file)
|
41
|
-
File.open(blacklist_file, 'wb') {|f| f.write(blacklist_file_content) } unless File.exist?(blacklist_file)
|
42
|
-
|
7
|
+
setup.configure
|
43
8
|
puts " ༼ つ ◕_◕ ༽つ OK... Gem has been set!!! "
|
44
9
|
exit
|
45
10
|
end
|
46
11
|
|
47
|
-
unless File.exist?(initializer_file)
|
12
|
+
unless File.exist?(setup.initializer_file)
|
48
13
|
puts " ༼ つ ◕_◕ ༽つ OOOPS... Gem has not yet been set. To set run 'simple_blacklist --configure' "
|
49
14
|
exit
|
50
15
|
end
|
data/lib/simple_blacklist.rb
CHANGED
@@ -1,28 +1,42 @@
|
|
1
|
-
require "simple_blacklist/
|
1
|
+
require "simple_blacklist/setup"
|
2
2
|
require "simple_blacklist/version"
|
3
3
|
|
4
4
|
module SimpleBlacklist
|
5
|
-
@@
|
5
|
+
@@blacklist_file ||= File.join(BLACKLIST_BASE, BLACKLIST_CONFIG['config_path'], 'blacklist.yml')
|
6
6
|
@@letters_variations ||= {}
|
7
|
+
@@mask_denied_words ||= "****"
|
7
8
|
@@blacklist ||= []
|
8
|
-
@@mask ||= "*****"
|
9
9
|
|
10
10
|
module_function
|
11
|
-
def
|
12
|
-
|
11
|
+
def blacklist_file=(file)
|
12
|
+
raise "Given blacklist_file is not a File" unless File.exist? file
|
13
|
+
@@blacklist_file = file
|
13
14
|
end
|
14
15
|
|
15
|
-
def
|
16
|
-
@@
|
16
|
+
def blacklist_file
|
17
|
+
@@blacklist_file
|
17
18
|
end
|
18
19
|
|
19
|
-
def
|
20
|
-
raise "Given
|
20
|
+
def letters_variations=(list)
|
21
|
+
raise "Given letters_variations is not a Hash" unless list.instance_of? Hash
|
21
22
|
@@letters_variations = list
|
22
23
|
end
|
23
24
|
|
25
|
+
def letters_variations
|
26
|
+
@@letters_variations
|
27
|
+
end
|
28
|
+
|
29
|
+
def mask_denied_words=(mask)
|
30
|
+
raise "Given mask_denied_words is not a String" unless mask.instance_of? String
|
31
|
+
@@mask_denied_words = mask
|
32
|
+
end
|
33
|
+
|
34
|
+
def mask_denied_words
|
35
|
+
@@mask_denied_words
|
36
|
+
end
|
37
|
+
|
24
38
|
def reload_blacklist
|
25
|
-
f = File.open(@@
|
39
|
+
f = File.open(@@blacklist_file, 'rb')
|
26
40
|
content = f.read
|
27
41
|
@@blacklist = content.split(/\W+/)
|
28
42
|
@@blacklist.map!(&:downcase)
|
@@ -30,22 +44,35 @@ module SimpleBlacklist
|
|
30
44
|
|
31
45
|
def sanitize(text)
|
32
46
|
reload_blacklist
|
33
|
-
words = text.split(/[
|
47
|
+
words = text.split(/([[:space:]]|[.,;:]|[[:blank:]])/)
|
34
48
|
words.each do |word|
|
35
|
-
|
36
|
-
|
49
|
+
word_replaced = replace_letters(word)
|
50
|
+
if @@blacklist.include? word_replaced.downcase
|
51
|
+
if word_replaced.downcase == word.downcase
|
52
|
+
text.gsub!(/(\b([#{word.split('').join("][")}])\b)/i, @@mask_denied_words)
|
53
|
+
else
|
54
|
+
if text=~/(\B([#{word.split('').join("][")}])\B)/i
|
55
|
+
text.gsub!(/(\B([#{word.split('').join("][")}])\B)/i, @@mask_denied_words)
|
56
|
+
else
|
57
|
+
text.gsub!(/([#{word.split('').join("][")}])/i, @@mask_denied_words)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
37
61
|
end
|
38
62
|
text
|
39
63
|
end
|
40
64
|
|
41
65
|
def replace_letters(word)
|
42
|
-
|
66
|
+
word_replaced = word.clone
|
43
67
|
chars = word.split('')
|
44
68
|
chars.each do |char|
|
45
69
|
@@letters_variations.each do |(key,list_chars)|
|
46
|
-
|
70
|
+
if list_chars.include? char.downcase
|
71
|
+
word_replaced.gsub!(/[#{char}]/, "#{key}")
|
72
|
+
end
|
47
73
|
end
|
48
74
|
end
|
49
|
-
|
75
|
+
word_replaced
|
50
76
|
end
|
77
|
+
|
51
78
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'simple_blacklist/config'
|
3
|
+
|
4
|
+
module SimpleBlacklist
|
5
|
+
class Setup
|
6
|
+
|
7
|
+
attr_accessor :base, :config_dir, :initializers_dir, :initializer_file, :blacklist_file
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@base = BLACKLIST_BASE
|
11
|
+
@config_dir = File.join(base, BLACKLIST_CONFIG['config_path'])
|
12
|
+
@initializers_dir = File.join(@config_dir, BLACKLIST_CONFIG['initializers_path'])
|
13
|
+
FileUtils.mkdir_p(@config_dir) unless File.directory?(@config_dir)
|
14
|
+
FileUtils.mkdir_p(@initializers_dir) unless File.directory?(@initializers_dir)
|
15
|
+
@initializer_file = File.join(initializers_dir, 'simple_blacklist.rb')
|
16
|
+
@blacklist_file = File.join(config_dir, 'blacklist.yml')
|
17
|
+
end
|
18
|
+
|
19
|
+
def configure
|
20
|
+
create_initializer
|
21
|
+
create_blacklist
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_initializer
|
25
|
+
initializer_file_content = <<-FILE
|
26
|
+
# Define simple_blacklist file
|
27
|
+
SimpleBlacklist.blacklist_file = File.join(BLACKLIST_BASE, "config", "blacklist.yml")
|
28
|
+
|
29
|
+
# Defines masks that will replace the words contained in the simple_blacklist
|
30
|
+
SimpleBlacklist.mask_denied_words = "****"
|
31
|
+
|
32
|
+
# list of letters and characters that can replace the letters
|
33
|
+
SimpleBlacklist.letters_variations = {
|
34
|
+
'a' => ['@', '4'],
|
35
|
+
'e' => ['3'],
|
36
|
+
'i' => ['1', '|', '!'],
|
37
|
+
'o' => ['0'],
|
38
|
+
's' => ['$', '5'],
|
39
|
+
't' => ['7', '+'],
|
40
|
+
'g' => ['6'],
|
41
|
+
'b' => ['8']
|
42
|
+
}
|
43
|
+
FILE
|
44
|
+
|
45
|
+
File.open(@initializer_file, 'wb') {|f| f.write(initializer_file_content) } unless File.exist?(@initializer_file)
|
46
|
+
end
|
47
|
+
|
48
|
+
def create_blacklist
|
49
|
+
blacklist_file_content = <<-FILE
|
50
|
+
ass
|
51
|
+
fuck
|
52
|
+
bitch
|
53
|
+
FILE
|
54
|
+
|
55
|
+
File.open(@blacklist_file, 'wb') {|f| f.write(blacklist_file_content) } unless File.exist?(@blacklist_file)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
data/simple_blacklist.gemspec
CHANGED
@@ -16,12 +16,13 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.test_files = `git ls-files -- {test,spec}/*`.split("\n")
|
17
17
|
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
18
|
spec.require_paths = ["lib"]
|
19
|
-
|
19
|
+
spec.required_ruby_version = '>= 2.0.0'
|
20
20
|
|
21
21
|
spec.summary = %q{Simple Blacklist to sanitize strings}
|
22
22
|
spec.description = %q{This project is designed to make easy the task of sanitizing string that contains words not allowed. }
|
23
23
|
|
24
|
-
spec.add_dependency "bundler"
|
25
|
-
|
24
|
+
spec.add_dependency "bundler"
|
25
|
+
|
26
|
+
spec.add_development_dependency "rake"
|
26
27
|
spec.add_development_dependency "rspec"
|
27
28
|
end
|
@@ -1,11 +1,41 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe SimpleBlacklist do
|
4
|
+
before(:all) do
|
5
|
+
@setup = SimpleBlacklist::Setup.new
|
6
|
+
@setup.configure
|
7
|
+
require @setup.initializer_file
|
8
|
+
end
|
9
|
+
|
10
|
+
after(:all) do
|
11
|
+
FileUtils.rm_rf @setup.config_dir
|
12
|
+
end
|
13
|
+
|
4
14
|
it 'has a version number' do
|
5
15
|
expect(SimpleBlacklist::VERSION).not_to be nil
|
6
16
|
end
|
7
17
|
|
8
|
-
|
9
|
-
|
18
|
+
describe "#sanitize" do
|
19
|
+
|
20
|
+
subject(:text_simple) { 'kiss my ass' }
|
21
|
+
it 'a word contained in the blacklist is replaced' do
|
22
|
+
expect(SimpleBlacklist.sanitize(text_simple)).to eq('kiss my ****')
|
23
|
+
end
|
24
|
+
|
25
|
+
subject(:text_special_char) { 'kiss my @$$' }
|
26
|
+
it 'a word written with special characters is replaced' do
|
27
|
+
expect(SimpleBlacklist.sanitize(text_special_char)).to eq('kiss my ****')
|
28
|
+
end
|
29
|
+
|
30
|
+
subject(:text_with_word_like) { 'kiss my ass assassin' }
|
31
|
+
it 'a similar word (not contained in the blacklist) can not be replaced' do
|
32
|
+
expect(SimpleBlacklist.sanitize(text_with_word_like)).to eq('kiss my **** assassin')
|
33
|
+
end
|
34
|
+
|
35
|
+
subject(:text_special_char_with_word_like) { 'kiss my @$$ @$$assin' }
|
36
|
+
it 'a similar word with special characters (not contained in the blacklist) can not be replaced' do
|
37
|
+
expect(SimpleBlacklist.sanitize(text_special_char_with_word_like)).to eq('kiss my **** @$$assin')
|
38
|
+
end
|
39
|
+
|
10
40
|
end
|
11
41
|
end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_blacklist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Silva
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- bin/simple_blacklist
|
77
77
|
- lib/simple_blacklist.rb
|
78
78
|
- lib/simple_blacklist/config.rb
|
79
|
+
- lib/simple_blacklist/setup.rb
|
79
80
|
- lib/simple_blacklist/version.rb
|
80
81
|
- simple_blacklist.gemspec
|
81
82
|
- spec/simple_blacklist_spec.rb
|
@@ -92,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
93
|
requirements:
|
93
94
|
- - ">="
|
94
95
|
- !ruby/object:Gem::Version
|
95
|
-
version:
|
96
|
+
version: 2.0.0
|
96
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
98
|
requirements:
|
98
99
|
- - ">="
|