mollom 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ docs
2
+ test_mollom.rb
3
+ pkg
4
+ .DS_Store
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Jan De Poorter - Openminds BVBA
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'rake/rdoctask'
3
+ require 'rake/testtask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gemspec|
8
+ gemspec.name = "mollom"
9
+ gemspec.summary = "Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment."
10
+ gemspec.description = "Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment."
11
+ gemspec.email = "mollom@openminds.be"
12
+ gemspec.homepage = "http://mollom.rubyforge.com"
13
+ gemspec.authors = ["Jan De Poorter"]
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ Rake::TestTask.new do |t|
21
+ t.libs << "test"
22
+ t.test_files = FileList['test/*_test.rb']
23
+ t.verbose = true
24
+ end
25
+
26
+ Rake::RDocTask.new { |rdoc|
27
+ rdoc.rdoc_dir = 'docs'
28
+ rdoc.title = "Mollom -- Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment."
29
+ rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
30
+ rdoc.options << '--charset' << 'utf-8'
31
+ rdoc.rdoc_files.include('README.rdoc')
32
+ rdoc.rdoc_files.include('lib/**/*.rb')
33
+ }
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -1,6 +1,8 @@
1
1
  require 'xmlrpc/client'
2
2
  require 'openssl'
3
3
  require 'base64'
4
+ require 'mollom/content_response'
5
+ require 'mollom/api_compatibility'
4
6
 
5
7
  # Mollom API requires this to change, but this gives a warning!
6
8
  # XMLRPC::Client::USER_AGENT = "Ruby Mollom/0.1"
@@ -51,7 +53,7 @@ class Mollom
51
53
  # response.spam? # => false
52
54
  # response.ham? # => true
53
55
  def check_content content = {}
54
- return ContentResponse.new(send_command('mollom.checkContent', content))
56
+ ContentResponse.new(send_command('mollom.checkContent', content))
55
57
  end
56
58
 
57
59
  # Requests an Image captcha from Mollom. It takes the optional <tt>session_id</tt> and <tt>author_ip</tt> keys, if you allready have a session.
@@ -61,7 +63,7 @@ class Mollom
61
63
  # captcha['url'] # => http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.png
62
64
  # captcha['session_id'] # => a9616e6b4cd6a81ecdd509fa624d895d
63
65
  def image_captcha info = {}
64
- return send_command('mollom.getImageCaptcha', info)
66
+ send_command('mollom.getImageCaptcha', info)
65
67
  end
66
68
 
67
69
  # Requests an Audio captcha from Mollom. It takes the optional +session_id+ and +author_ip+ keys, if you allready have a session.
@@ -71,7 +73,7 @@ class Mollom
71
73
  # captcha['url'] # => http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.mp3
72
74
  # captcha['session_id'] # => a9616e6b4cd6a81ecdd509fa624d895d
73
75
  def audio_captcha info = {}
74
- return send_command('mollom.getAudioCaptcha', info)
76
+ send_command('mollom.getAudioCaptcha', info)
75
77
  end
76
78
 
77
79
  # Checks with mollom if the given captcha (by the user) is correct. Takes +session_id+ and +solution+ keys. Both keys are required.
@@ -82,14 +84,14 @@ class Mollom
82
84
  # return = mollom.valid_captcha? :session_id => captcha['session_id'], :solution => 'abcDe9'
83
85
  # return # => true
84
86
  def valid_captcha? info = {}
85
- return send_command('mollom.checkCaptcha', info)
87
+ send_command('mollom.checkCaptcha', info)
86
88
  end
87
89
 
88
90
  # Standard check to see if your public/private keypair are recognized. Takes no options
89
91
  def key_ok?
90
- return send_command('mollom.verifyKey')
92
+ send_command('mollom.verifyKey')
91
93
  rescue XMLRPC::FaultException
92
- return false
94
+ false
93
95
  end
94
96
 
95
97
  # Gets some statistics from Mollom about your site.
@@ -105,7 +107,7 @@ class Mollom
105
107
  #
106
108
  # mollom.statistics :type => 'total_accepted' # => 123
107
109
  def statistics options = {}
108
- return send_command('mollom.getStatistics', options)
110
+ send_command('mollom.getStatistics', options)
109
111
  end
110
112
 
111
113
  # Send feedback to Mollom about a certain content. Required keys are +session_id+ and +feedback+.
@@ -118,7 +120,14 @@ class Mollom
118
120
  #
119
121
  # mollom.send_feedback :session_id => 'a9616e6b4cd6a81ecdd509fa624d895d', :feedback => 'unwanted'
120
122
  def send_feedback feedback = {}
121
- return send_command('mollom.sendFeedback', feedback)
123
+ send_command('mollom.sendFeedback', feedback)
124
+ end
125
+
126
+ # Gets language of input text.
127
+ #
128
+ # mollom.language_for 'This is my text'
129
+ def language_for text
130
+ send_command('mollom.detectLanguage', :text => text)
122
131
  end
123
132
 
124
133
  # Gets a list of servers from Mollom. You should cache this information in your application in a temporary file or in a database. You can set this with Mollom#server_list=
@@ -160,7 +169,8 @@ class Mollom
160
169
  def send_command(command, data = {})
161
170
  server_list.each do |server|
162
171
  begin
163
- return XMLRPC::Client.new(server[:host], "/#{API_VERSION}").call(command, data.merge(authentication_hash))
172
+ client = XMLRPC::Client.new(server[:host], "/#{API_VERSION}")
173
+ return client.call(command, data.merge(authentication_hash))
164
174
  # TODO: Rescue more stuff (Connection Timeout and such)
165
175
  rescue XMLRPC::FaultException => error
166
176
  case error.faultCode
@@ -195,52 +205,8 @@ class Mollom
195
205
  return :public_key=> @public_key, :time => now, :hash => hash, :nonce => nonce
196
206
  end
197
207
 
198
- class ContentResponse
199
- attr_reader :session_id, :quality
200
-
201
- Unknown = 0
202
- Ham = 1
203
- Unsure = 2
204
- Spam = 3
205
-
206
- # This class should only be initialized from within the +check_content+ command.
207
- def initialize(hash)
208
- @spam_response = hash["spam"]
209
- @session_id = hash["session_id"]
210
- @quality = hash["quality"]
211
- end
212
-
213
- # Is the content Spam?
214
- def spam?
215
- @spam_response == Spam
216
- end
217
-
218
- # Is the content Ham?
219
- def ham?
220
- @spam_response == Ham
221
- end
222
-
223
- # is Mollom unsure about the content?
224
- def unsure?
225
- @spam_response == Unsure
226
- end
227
-
228
- # is the content unknown?
229
- def unknown?
230
- @spam_response == Unknown
231
- end
232
-
233
- # Returns 'unknown', 'ham', 'unsure' or 'spam', depending on what the content is.
234
- def to_s
235
- case @spam_response
236
- when Unknown then 'unknown'
237
- when Ham then 'ham'
238
- when Unsure then 'unsure'
239
- when Spam then 'spam'
240
- end
241
- end
242
- end
243
-
244
208
  class Error < StandardError; end
245
209
  class NoAvailableServers < Error; end
210
+
211
+ include ApiCompatibility
246
212
  end
@@ -0,0 +1,16 @@
1
+ class Mollom
2
+ module ApiCompatibility
3
+ def self.included base
4
+ base.class_eval do
5
+ alias checkContent check_content
6
+ alias getImageCaptcha image_captcha
7
+ alias getAudioCaptcha audio_captcha
8
+ alias checkCaptcha valid_captcha?
9
+ alias verifyKey key_ok?
10
+ alias getStatistics statistics
11
+ alias sendFeedback send_feedback
12
+ alias detectLanguage language_for
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,47 @@
1
+ class Mollom
2
+ class ContentResponse
3
+ attr_reader :session_id, :quality
4
+
5
+ Unknown = 0
6
+ Ham = 1
7
+ Unsure = 2
8
+ Spam = 3
9
+
10
+ # This class should only be initialized from within the +check_content+ command.
11
+ def initialize(hash)
12
+ @spam_response = hash["spam"]
13
+ @session_id = hash["session_id"]
14
+ @quality = hash["quality"]
15
+ end
16
+
17
+ # Is the content Spam?
18
+ def spam?
19
+ @spam_response == Spam
20
+ end
21
+
22
+ # Is the content Ham?
23
+ def ham?
24
+ @spam_response == Ham
25
+ end
26
+
27
+ # is Mollom unsure about the content?
28
+ def unsure?
29
+ @spam_response == Unsure
30
+ end
31
+
32
+ # is the content unknown?
33
+ def unknown?
34
+ @spam_response == Unknown
35
+ end
36
+
37
+ # Returns 'unknown', 'ham', 'unsure' or 'spam', depending on what the content is.
38
+ def to_s
39
+ case @spam_response
40
+ when Unknown then 'unknown'
41
+ when Ham then 'ham'
42
+ when Unsure then 'unsure'
43
+ when Spam then 'spam'
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mollom}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jan De Poorter"]
12
+ s.date = %q{2010-03-10}
13
+ s.description = %q{Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment.}
14
+ s.email = %q{mollom@openminds.be}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/mollom.rb",
25
+ "lib/mollom/api_compatibility.rb",
26
+ "lib/mollom/content_response.rb",
27
+ "mollom.gemspec",
28
+ "pkg/mollom-0.1.1.gem",
29
+ "test/content_response_test.rb",
30
+ "test/mollom_test.rb"
31
+ ]
32
+ s.homepage = %q{http://mollom.rubyforge.com}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.6}
36
+ s.summary = %q{Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment.}
37
+ s.test_files = [
38
+ "test/content_response_test.rb",
39
+ "test/mollom_test.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
52
+
@@ -150,46 +150,95 @@ class TestMollom < Test::Unit::TestCase
150
150
  end
151
151
 
152
152
  def test_check_content
153
- Mollom.any_instance.expects(:send_command).with('mollom.checkContent', :author_ip => '172.16.0.1', :post_body => 'Lorem Ipsum').returns("spam" => 1, "quality" => 0.40, "session_id" => 1)
154
- cr = @mollom.check_content(:author_ip => '172.16.0.1', :post_body => 'Lorem Ipsum')
155
- assert cr.ham?
156
- assert_equal 1, cr.session_id
157
- assert_equal 0.40, cr.quality
153
+ options = {:author_ip => '172.16.0.1', :post_body => 'Lorem Ipsum'}
154
+
155
+ assert_command 'mollom.checkContent', :with => options, :returns => {"spam" => 1, "quality" => 0.40, "session_id" => 1} do
156
+ cr = @mollom.check_content(options)
157
+ assert cr.ham?
158
+ assert_equal 1, cr.session_id
159
+ assert_equal 0.40, cr.quality
160
+ end
158
161
  end
159
162
 
160
163
  def test_image_captcha
161
- Mollom.any_instance.expects(:send_command).with('mollom.getImageCaptcha', :author_ip => '172.16.0.1').returns('url' => 'http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.png', 'session_id' => 'a9616e6b4cd6a81ecdd509fa624d895d')
162
- @mollom.image_captcha(:author_ip => '172.16.0.1')
164
+ options = {:author_ip => '172.16.0.1'}
165
+
166
+ assert_command 'mollom.getImageCaptcha', :with => options, :returns => {'url' => 'http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.png', 'session_id' => 'a9616e6b4cd6a81ecdd509fa624d895d'} do
167
+ @mollom.image_captcha(:author_ip => '172.16.0.1')
168
+ end
163
169
  end
164
170
 
165
171
  def test_audio_captcha
166
- Mollom.any_instance.expects(:send_command).with('mollom.getAudioCaptcha', :author_ip => '172.16.0.1').returns('url' => 'http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.mp3', 'session_id' => 'a9616e6b4cd6a81ecdd509fa624d895d')
167
- @mollom.audio_captcha(:author_ip => '172.16.0.1')
172
+ options = {:author_ip => '172.16.0.1'}
173
+
174
+ assert_command 'mollom.getAudioCaptcha', :with => options, :returns => {'url' => 'http://xmlrpc1.mollom.com:80/a9616e6b4cd6a81ecdd509fa624d895d.mp3', 'session_id' => 'a9616e6b4cd6a81ecdd509fa624d895d'} do
175
+ @mollom.audio_captcha(options)
176
+ end
168
177
  end
169
178
 
170
179
  def test_valid_captcha
171
- Mollom.any_instance.expects(:send_command).with('mollom.checkCaptcha', :session_id => 'a9616e6b4cd6a81ecdd509fa624d895d', :solution => 'foo').returns(true)
172
- @mollom.valid_captcha?(:session_id => 'a9616e6b4cd6a81ecdd509fa624d895d', :solution => 'foo')
180
+ options = {:session_id => 'a9616e6b4cd6a81ecdd509fa624d895d', :solution => 'foo'}
181
+
182
+ assert_command 'mollom.checkCaptcha', :with => options, :returns => true do
183
+ assert @mollom.valid_captcha?(options)
184
+ end
173
185
  end
174
186
 
175
187
  def test_key_ok
176
- Mollom.any_instance.expects(:send_command).with('mollom.verifyKey').returns(true)
177
- assert @mollom.key_ok?
188
+ assert_command 'mollom.verifyKey', :returns => true do
189
+ assert @mollom.key_ok?
190
+ end
178
191
  end
179
192
 
180
193
  def test_invalid_key
181
- Mollom.any_instance.expects(:send_command).with('mollom.verifyKey').raises(XMLRPC::FaultException.new(1000, "Internal server error due to malformed request, or the hamster powering the server died..."))
182
- assert !@mollom.key_ok?
194
+ assert_command 'mollom.verifyKey', :raises => internal_server_error do
195
+ assert !@mollom.key_ok?
196
+ end
183
197
  end
184
198
 
185
199
  def test_statistics
186
- Mollom.any_instance.expects(:send_command).with('mollom.getStatistics', :type => 'total_accepted').returns(123)
187
- @mollom.statistics(:type => 'total_accepted')
200
+ assert_command 'mollom.getStatistics', :with => {:type => 'total_accepted'}, :returns => 12 do
201
+ @mollom.statistics(:type => 'total_accepted')
202
+ end
188
203
  end
189
204
 
190
205
  def test_send_feedback
191
- Mollom.any_instance.expects(:send_command).with('mollom.sendFeedback', :session_id => 1, :feedback => 'profanity')
192
- @mollom.send_feedback :session_id => 1, :feedback => 'profanity'
193
- end
206
+ assert_command 'mollom.sendFeedback', :with => {:session_id => 1, :feedback => 'profanity'} do
207
+ @mollom.send_feedback :session_id => 1, :feedback => 'profanity'
208
+ end
209
+ end
210
+
211
+ def test_detect_language
212
+ assert_command 'mollom.detectLanguage', :with => {:text => 'Dit is nederlands'}, :returns => [{"confidence"=>0.332, "language"=>"nl"}] do
213
+ assert_equal [{"confidence"=>0.332, "language"=>"nl"}], @mollom.language_for('Dit is nederlands')
214
+ end
215
+ end
216
+
217
+ def test_api_compatability
218
+ assert @mollom.respond_to? :checkContent
219
+ assert @mollom.respond_to? :getImageCaptcha
220
+ assert @mollom.respond_to? :getAudioCaptcha
221
+ assert @mollom.respond_to? :checkCaptcha
222
+ assert @mollom.respond_to? :verifyKey
223
+ assert @mollom.respond_to? :getStatistics
224
+ assert @mollom.respond_to? :sendFeedback
225
+ assert @mollom.respond_to? :detectLanguage
226
+ end
227
+
228
+ private
229
+ def assert_command command, options = {}
230
+ expectation = Mollom.any_instance.expects(:send_command)
231
+ expectation.with do |*arguments|
232
+ arguments.first == command &&
233
+ (!options[:with] || options[:with] == arguments.last)
234
+ end
235
+ expectation.returns(options[:returns]) if options[:returns]
236
+ expectation.raises(options[:raises]) if options[:raises]
194
237
 
238
+ yield
239
+ end
240
+
241
+ def internal_server_error
242
+ XMLRPC::FaultException.new(1000, "Internal server error due to malformed request, or the hamster powering the server died...")
243
+ end
195
244
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mollom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Jan De Poorter
@@ -9,11 +14,11 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-09-28 00:00:00 +02:00
17
+ date: 2010-03-10 00:00:00 +01:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
16
- description:
21
+ description: Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment.
17
22
  email: mollom@openminds.be
18
23
  executables: []
19
24
 
@@ -22,33 +27,45 @@ extensions: []
22
27
  extra_rdoc_files:
23
28
  - README.rdoc
24
29
  files:
25
- - lib/mollom.rb
30
+ - .gitignore
31
+ - MIT-LICENSE
26
32
  - README.rdoc
33
+ - Rakefile
34
+ - VERSION
35
+ - lib/mollom.rb
36
+ - lib/mollom/api_compatibility.rb
37
+ - lib/mollom/content_response.rb
38
+ - mollom.gemspec
39
+ - pkg/mollom-0.1.1.gem
40
+ - test/content_response_test.rb
41
+ - test/mollom_test.rb
27
42
  has_rdoc: true
28
43
  homepage: http://mollom.rubyforge.com
29
44
  licenses: []
30
45
 
31
46
  post_install_message:
32
- rdoc_options: []
33
-
47
+ rdoc_options:
48
+ - --charset=UTF-8
34
49
  require_paths:
35
50
  - lib
36
51
  required_ruby_version: !ruby/object:Gem::Requirement
37
52
  requirements:
38
53
  - - ">="
39
54
  - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
40
57
  version: "0"
41
- version:
42
58
  required_rubygems_version: !ruby/object:Gem::Requirement
43
59
  requirements:
44
60
  - - ">="
45
61
  - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
46
64
  version: "0"
47
- version:
48
65
  requirements: []
49
66
 
50
- rubyforge_project: mollom
51
- rubygems_version: 1.3.5
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.6
52
69
  signing_key:
53
70
  specification_version: 3
54
71
  summary: Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment.