Skip to content
This repository has been archived by the owner on Dec 19, 2020. It is now read-only.

Allow the word size to be configured and minor usability updates #15

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ The following methods are available to use with Obscenity:
- :nonconsonants : Replaces non consonants with '*'
- "custom string" : Replaces the profane word with the custom string

`config.word_size` accepts a numeric value indicating the min size of a word to check in the blacklist or whitelist
- default if not set in config is 3

Example:

```ruby
Obscenity.configure do |config|
config.blacklist = "path/to/blacklist/file.yml"
config.whitelist = ["safe", "word"]
config.replacement = :stars
config.replacement = :stars,
config.word_size = 2
end
```

Expand Down Expand Up @@ -275,6 +279,13 @@ A `be_profane` matcher is available for RSpec. Its usage is very simple:
user.username.should_not be_profane
```

#### RubyMine Testing Note
Require the full path of the helper file if you wish to run in RubyMine. For example:

```ruby
require File.dirname(__FILE__) + '/helper'
```

## Contributing to obscenity

* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
Expand Down
4 changes: 4 additions & 0 deletions lib/obscenity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def replacement(chars)
def offensive(text)
Obscenity::Base.offensive(text)
end

def word_size(value)
Obscenity::Base.word_size(value)
end


end
Expand Down
15 changes: 12 additions & 3 deletions lib/obscenity/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ def whitelist=(value)
end

def profane?(text)
return(false) unless text.to_s.size >= 3
return(false) unless text.to_s.size >= word_size
blacklist.each do |foul|
return(true) if text =~ /\b#{foul}\b/i && !whitelist.include?(foul)
end
false
end

def sanitize(text)
return(text) unless text.to_s.size >= 3
return(text) unless text.to_s.size >= word_size
blacklist.each do |foul|
text.gsub!(/\b#{foul}\b/i, replace(foul)) unless whitelist.include?(foul)
end
Expand All @@ -40,9 +40,18 @@ def replacement(chars)
self
end

def word_size
@word_size = Obscenity.config.word_size
end

def word_size=(value)
Obscenity.config.word_size = value
word_size
end

def offensive(text)
words = []
return(words) unless text.to_s.size >= 3
return(words) unless text.to_s.size >= word_size
blacklist.each do |foul|
words << foul if text =~ /\b#{foul}\b/i && !whitelist.include?(foul)
end
Expand Down
7 changes: 6 additions & 1 deletion lib/obscenity/config.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Obscenity
class Config

attr_accessor :replacement
attr_accessor :replacement, :word_size

DEFAULT_WHITELIST = []
DEFAULT_BLACKLIST = File.dirname(__FILE__) + "/../../config/blacklist.yml"
DEFAULT_WORD_SIZE = 3

def initialize
yield(self) if block_given?
Expand All @@ -30,6 +31,10 @@ def whitelist
def whitelist=(value)
@whitelist = value == :default ? DEFAULT_WHITELIST : value
end

def word_size
@word_size ||= DEFAULT_WORD_SIZE
end

private
def validate_config_options
Expand Down
3 changes: 2 additions & 1 deletion obscenity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
require_relative 'lib/obscenity/version'

Gem::Specification.new do |s|
s.name = "obscenity"
s.version = "1.0.2"
s.version = Obscenity::VERSION

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Thiago Jackiw"]
Expand Down
21 changes: 19 additions & 2 deletions test/test_base.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require 'helper'
#require with file.direname allows test to run in RubyMine as well as commandline
require File.dirname(__FILE__) + '/helper'

class TestBase < Test::Unit::TestCase

context "#respond_to?" do
should "respond to methods and attributes" do
[:blacklist, :whitelist, :profane?, :sanitize, :replacement, :offensive, :replace].each do |field|
[:blacklist, :whitelist, :profane?, :sanitize, :replacement, :offensive, :replace, :word_size].each do |field|
assert Obscenity::Base.respond_to?(field)
end
end
Expand Down Expand Up @@ -63,6 +64,22 @@ class TestBase < Test::Unit::TestCase
assert !Obscenity::Base.profane?('biatch')
end
end
context "with 2 letter word" do
setup {
Obscenity::Base.blacklist = ['ho']
Obscenity::Base.whitelist = :default
Obscenity::Base.word_size = 2
}
should "validate the profanity of a 2 letter word based on the custom list" do
assert Obscenity::Base.profane?('ho')
assert !Obscenity::Base.profane?('yo')
assert !Obscenity::Base.profane?('go')
end
should "2 letter word should not be profane if default word size used" do
Obscenity::Base.word_size = Obscenity::Config::DEFAULT_WORD_SIZE
assert !Obscenity::Base.profane?('ho')
end
end
end
context "with whitelist" do
context "without custom blacklist config" do
Expand Down
11 changes: 9 additions & 2 deletions test/test_config.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require 'helper'
#require with file.direname allows test to run in RubyMine as well as commandline
require File.dirname(__FILE__) + '/helper'

class TestConfig < Test::Unit::TestCase

context "#respond_to?" do
should "respond to methods and attributes" do
Obscenity::Config.new do |config|
[:blacklist, :whitelist, :replacement].each do |field|
[:blacklist, :whitelist, :replacement, :word_size].each do |field|
assert config.respond_to?(field)
end
end
Expand All @@ -16,32 +17,38 @@ class TestConfig < Test::Unit::TestCase
blacklist = ['ass', 'shit', 'penis']
whitelist = ['penis']
replacement = :stars
word_size = 3

config = Obscenity::Config.new do |config|
config.blacklist = blacklist
config.whitelist = whitelist
config.replacement = replacement
config.word_size = word_size
end

assert_equal blacklist, config.blacklist
assert_equal whitelist, config.whitelist
assert_equal replacement, config.replacement
assert_equal word_size, config.word_size
end

should "return default values if none is set" do
config = Obscenity::Config.new
assert_equal [], config.whitelist
assert_equal :garbled, config.replacement
assert_match /config\/blacklist.yml/, config.blacklist
assert_equal 3, config.word_size
end

should "return default values when default values are set" do
config = Obscenity::Config.new do |config|
config.blacklist = :default
config.replacement = :default
config.word_size = :default
end
assert_equal [], config.whitelist
assert_equal :default, config.replacement
assert_equal :default, config.word_size
assert_match /config\/blacklist.yml/, config.blacklist
end

Expand Down