diff --git a/README.md b/README.md index c0ce04a..9f39b04 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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. diff --git a/lib/obscenity.rb b/lib/obscenity.rb index 071add5..a9ca07f 100644 --- a/lib/obscenity.rb +++ b/lib/obscenity.rb @@ -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 diff --git a/lib/obscenity/base.rb b/lib/obscenity/base.rb index 44063c7..a192c19 100644 --- a/lib/obscenity/base.rb +++ b/lib/obscenity/base.rb @@ -19,7 +19,7 @@ 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 @@ -27,7 +27,7 @@ def profane?(text) 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 @@ -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 diff --git a/lib/obscenity/config.rb b/lib/obscenity/config.rb index eb8a9fe..ee6bf2e 100644 --- a/lib/obscenity/config.rb +++ b/lib/obscenity/config.rb @@ -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? @@ -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 diff --git a/obscenity.gemspec b/obscenity.gemspec index d345cad..9d47e29 100644 --- a/obscenity.gemspec +++ b/obscenity.gemspec @@ -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"] diff --git a/test/test_base.rb b/test/test_base.rb index 37be492..a090cbb 100644 --- a/test/test_base.rb +++ b/test/test_base.rb @@ -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 @@ -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 diff --git a/test/test_config.rb b/test/test_config.rb index c97e0b8..33184b3 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -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 @@ -16,16 +17,19 @@ 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 @@ -33,15 +37,18 @@ class TestConfig < Test::Unit::TestCase 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