Created
March 15, 2010 11:42
-
-
Save ukstudio/332767 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WordFilter | |
| attr_reader :words | |
| attr_accessor :censor_message | |
| def initialize(*words) | |
| @words = words | |
| @censor_message = "censored" | |
| end | |
| def detect(message) | |
| message =~ create_regexpression | |
| end | |
| def censor(message) | |
| user_part, message = split_from_message(message) | |
| user_part + message.gsub(create_regexpression, "<#{censor_message}>") | |
| end | |
| def add_words(*words) | |
| @words += words | |
| @words.uniq! | |
| end | |
| def delete_words(*words) | |
| words.each do |w| | |
| @words.delete(w) | |
| end | |
| end | |
| private | |
| def create_regexpression | |
| /#{@words.sort.reverse.map{|w| Regexp.quote(w)}.join('|')}/ | |
| end | |
| def split_from_message(message) | |
| if message =~ /(\S+:\s)(.+)/ | |
| [$1,$2] | |
| else | |
| ["", message] | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'spec' | |
| require 'word_filter' | |
| describe WordFilter do | |
| it "NGWordをひとつ指定できること" do | |
| lambda do | |
| filter = WordFilter.new("NGWord") | |
| end.should_not raise_error | |
| end | |
| it "NGWordを指定しなくてもいいこと" do | |
| pending | |
| end | |
| it "NGWordを複数指定できること" do | |
| lambda do | |
| filter = WordFilter.new("NGWord1", "NGWord2") | |
| end.should_not raise_error | |
| end | |
| it "NGWordを追加できること" do | |
| filter = WordFilter.new("NGWord1") | |
| lambda do | |
| filter.add_words("NGWord2", "NGWord3") | |
| end.should_not raise_error | |
| end | |
| context "NGWordを追加したとき" do | |
| before do | |
| @filter = WordFilter.new("NGWord1") | |
| @filter.add_words("NGWord2", "NGWord3") | |
| end | |
| it "既存のNGWordを取得できること" do | |
| @filter.words.should be_include("NGWord1") | |
| end | |
| it "追加したNGWordを取得できること" do | |
| @filter.words.should be_include("NGWord2") | |
| @filter.words.should be_include("NGWord3") | |
| end | |
| it "追加したNGWordでdetectすること" do | |
| @filter.detect("NGWord2").should be_true | |
| @filter.detect("NGWord3").should be_true | |
| end | |
| it "追加したNGWordでcensorできること" do | |
| @filter.censor("NGWord2とNGWord3").should == "<censored>と<censored>" | |
| end | |
| end | |
| context "NGWordが文字列以外だったら" do | |
| it "文字列として扱う?" do | |
| pending | |
| end | |
| end | |
| context "特殊文字をNGWord指定したら" do | |
| it "\をdetectできること" do | |
| filter = WordFilter.new("\\") | |
| filter.detect("\\").should be_true | |
| end | |
| it "\をcensorできること" do | |
| filter = WordFilter.new("\\") | |
| filter.censor("\\").should == "<censored>" | |
| end | |
| end | |
| context "NGWordが別のNGWordを内包しているとき" do | |
| it "AAAAとAAAだったらAAAAでcensorする"do | |
| filter = WordFilter.new("AAA", "AAAA") | |
| filter.censor("AAAA").should == "<censored>" | |
| end | |
| it "1111と111だったら1111でcensorする"do | |
| filter = WordFilter.new("111", "1111") | |
| filter.censor("1111").should == "<censored>" | |
| end | |
| end | |
| context "NGWordが指定されている場合" do | |
| before do | |
| @filter = WordFilter.new("NGWord") | |
| end | |
| it "NGWordをdetectできること" do | |
| @filter.detect("NGWord").should be_true | |
| end | |
| it "NGWordじゃない場合detectしない" do | |
| @filter.detect("hoge").should be_false | |
| end | |
| it "NGWordをcensorできること" do | |
| @filter.censor("t_wada: あいうNGWord").should == "t_wada: あいう<censored>" | |
| end | |
| it "NGWordを含まなければそのまま返すこと" do | |
| @filter.censor("t_wada: あいうえお").should == "t_wada: あいうえお" | |
| end | |
| end | |
| context "NGWordが複数指定している場合" do | |
| before do | |
| @filter = WordFilter.new("NGWord1", "NGWord2") | |
| end | |
| it "NGWordをdetectできること" do | |
| @filter.detect("NGWord1").should be_true | |
| @filter.detect("NGWord2").should be_true | |
| end | |
| it "NGWordをcensorできること" do | |
| @filter.censor("t_wada: あいうNGWord1").should == "t_wada: あいう<censored>" | |
| @filter.censor("t_wada: あいうNGWord2").should == "t_wada: あいう<censored>" | |
| @filter.censor("t_wada: あいうえお").should == "t_wada: あいうえお" | |
| end | |
| it "NGWordを削除できること" do | |
| @filter.delete_words("NGWord1") | |
| @filter.words.should_not be_include("NGWord1") | |
| end | |
| it "NGWordを複数削除できること" do | |
| @filter.delete_words("NGWord1", "NGWord2") | |
| @filter.words.should_not be_include("NGWord1") | |
| @filter.words.should_not be_include("NGWord2") | |
| end | |
| end | |
| context "IDがNGWordだったら" do | |
| before do | |
| @filter = WordFilter.new('t_wada') | |
| end | |
| it "IDはcensorしない" do | |
| @filter.censor("t_wada: t_wada").should == "t_wada: <censored>" | |
| end | |
| end | |
| it "<censored>の文字列を変更できること" do | |
| filter = WordFilter.new("NGWord") | |
| filter.censor_message = "xxx" | |
| filter.censor("NGWord").should == "<xxx>" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment