swearjar 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -18,3 +18,11 @@ gem install swearjar
18
18
 
19
19
  Swearjar.default.censor('jim henson has a massive hard on he is gonna use to fuck everybody')
20
20
  << 'jim henson has a massive **** ** he is gonna use to **** everybody'
21
+
22
+ To load from a custom yaml file, you can do the following
23
+
24
+ sj = Swearjar.new
25
+ sj.load_file('my_yaml.yml')
26
+
27
+ The YAML file can have two sections, `simple` and `regex`. For an example, see `lib/config/en.yml`.
28
+
data/lib/swearjar.rb CHANGED
@@ -13,23 +13,26 @@ class Swearjar
13
13
 
14
14
  attr_reader :tester, :hash
15
15
 
16
- def initialize(file)
17
- data = YAML.load_file(file)
18
-
16
+ def initialize(file = nil)
19
17
  @tester = FuzzyHash.new
20
18
  @hash = {}
19
+ load_file(file) if file
20
+ end
21
+
22
+ def load_file(file)
23
+ data = YAML.load_file(file)
21
24
 
22
25
  data['regex'].each do |pattern, type|
23
26
  @tester[Regexp.new(pattern)] = type
24
- end
27
+ end if data['regex']
25
28
 
26
29
  data['simple'].each do |test, type|
27
30
  @hash[test] = type
28
- end
29
-
31
+ end if data['simple']
30
32
  end
31
33
 
32
34
  def scan(string, &block)
35
+ string = string.to_s
33
36
  string.scan(/\b[a-zA-Z-]+\b/) do |word|
34
37
  block.call(word, hash[word.downcase] || hash[word.downcase.gsub(/e?s$/,'')] )
35
38
  end
@@ -39,18 +42,20 @@ class Swearjar
39
42
  end
40
43
 
41
44
  def profane?(string)
45
+ string = string.to_s
42
46
  scan(string) {|word, test| return true if !test.nil?}
43
47
  return false
44
48
  end
45
49
 
46
50
  def scorecard(string)
51
+ string = string.to_s
47
52
  scorecard = {}
48
53
  scan(string) {|word, test| test.each { |type| scorecard.key?(type) ? scorecard[type] += 1 : scorecard[type] = 1} if test}
49
54
  scorecard
50
55
  end
51
56
 
52
57
  def censor(string)
53
- censored_string = string.dup
58
+ censored_string = string.to_s.dup
54
59
  scan(string) {|word, test| censored_string.gsub!(word, block_given? ? yield(word) : word.gsub(/\S/, '*')) if test}
55
60
  censored_string
56
61
  end
@@ -1,3 +1,3 @@
1
1
  class Swearjar
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -0,0 +1,2 @@
1
+ simple:
2
+ "python": ["sexual"]
@@ -35,4 +35,14 @@ describe Swearjar do
35
35
  Swearjar.default.censor('jim henson has a massive hard on he is gonna use to fuck everybody').should == 'jim henson has a massive **** ** he is gonna use to **** everybody'
36
36
  end
37
37
 
38
+ it "should not do much when given a non-string" do
39
+ Swearjar.default.profane?(nil).should be_false
40
+ end
41
+
42
+ it "should allow you to load a new yaml file" do
43
+ sj = Swearjar.new
44
+ sj.load_file(File.expand_path('../data/swear.yml', __FILE__))
45
+ sj.censor("Python is the best language!").should == "****** is the best language!"
46
+ end
47
+
38
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swearjar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-16 00:00:00.000000000Z
12
+ date: 2011-12-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fuzzyhash
16
- requirement: &70345550448020 !ruby/object:Gem::Requirement
16
+ requirement: &70298428416720 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.0.11
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70345550448020
24
+ version_requirements: *70298428416720
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &70345550447440 !ruby/object:Gem::Requirement
27
+ requirement: &70298428457400 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70345550447440
35
+ version_requirements: *70298428457400
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70345550446860 !ruby/object:Gem::Requirement
38
+ requirement: &70298428484540 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.8.7
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70345550446860
46
+ version_requirements: *70298428484540
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70345550446340 !ruby/object:Gem::Requirement
49
+ requirement: &70298429515220 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 1.3.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70345550446340
57
+ version_requirements: *70298429515220
58
58
  description: Put another nickel in the swearjar. Simple profanity detection with content
59
59
  analysis.
60
60
  email: joshbuddy@gmail.com
@@ -71,6 +71,7 @@ files:
71
71
  - lib/swearjar.rb
72
72
  - lib/swearjar/tester.rb
73
73
  - lib/swearjar/version.rb
74
+ - spec/data/swear.yml
74
75
  - spec/spec.opts
75
76
  - spec/spec_helper.rb
76
77
  - spec/swearjar_spec.rb
@@ -90,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
91
  version: '0'
91
92
  segments:
92
93
  - 0
93
- hash: 2946955865055443431
94
+ hash: -2399529509977720211
94
95
  required_rubygems_version: !ruby/object:Gem::Requirement
95
96
  none: false
96
97
  requirements:
@@ -105,6 +106,7 @@ specification_version: 3
105
106
  summary: Put another nickel in the swearjar. Simple profanity detection with content
106
107
  analysis
107
108
  test_files:
109
+ - spec/data/swear.yml
108
110
  - spec/spec.opts
109
111
  - spec/spec_helper.rb
110
112
  - spec/swearjar_spec.rb