mollom 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mollom.rb +28 -7
- data/test/mollom_test.rb +49 -11
- metadata +3 -3
data/lib/mollom.rb
CHANGED
@@ -7,6 +7,10 @@ require 'base64'
|
|
7
7
|
|
8
8
|
class Mollom
|
9
9
|
API_VERSION = '1.0'
|
10
|
+
STATIC_SERVER_LIST = [{:proto => 'http', :host => 'xmlrpc3.mollom.com'},
|
11
|
+
{:proto => 'http', :host => 'xmlrpc2.mollom.com'},
|
12
|
+
{:proto => 'http', :host => 'xmlrpc1.mollom.com'}].freeze
|
13
|
+
|
10
14
|
module Errors
|
11
15
|
Standard = 1000
|
12
16
|
Refresh = 1100
|
@@ -14,7 +18,6 @@ class Mollom
|
|
14
18
|
end
|
15
19
|
|
16
20
|
attr_accessor :private_key, :public_key
|
17
|
-
attr_writer :server_list
|
18
21
|
|
19
22
|
# Creates a new Mollom object. Takes +private_key+ and +public_key+ as keys.
|
20
23
|
#
|
@@ -123,20 +126,38 @@ class Mollom
|
|
123
126
|
# Takes an optional parameter +refresh+, which resets the cached value.
|
124
127
|
#
|
125
128
|
# mollom.server_list
|
126
|
-
# # => [{:proto=>"http", :
|
129
|
+
# # => [{:proto=>"http", :host=>"88.151.243.81"}, {:proto=>"http", :host=>"82.103.131.136"}]
|
127
130
|
def server_list refresh = false
|
128
|
-
@server_list
|
129
|
-
|
130
|
-
|
131
|
-
|
131
|
+
return @server_list if @server_list && refresh
|
132
|
+
STATIC_SERVER_LIST.each do |static_server|
|
133
|
+
@server_list = get_server_list_from(static_server)
|
134
|
+
return @server_list if @server_list
|
135
|
+
end
|
136
|
+
# Should have returned a server_list here..
|
137
|
+
raise(Error.new("Can't get mollom server-list"))
|
138
|
+
end
|
139
|
+
|
140
|
+
def server_list=(list)
|
141
|
+
# Check if we get an actual serverlist-array
|
142
|
+
if list.is_a?(Array) && list.all? {|hash| hash.has_key?(:host) && hash.has_key?(:proto) }
|
143
|
+
@server_list = list
|
132
144
|
end
|
133
145
|
end
|
134
146
|
|
135
147
|
private
|
148
|
+
def get_server_list_from(server)
|
149
|
+
XMLRPC::Client.new(server[:host], "/#{API_VERSION}").call('mollom.getServerList', authentication_hash).collect do |server|
|
150
|
+
proto, ip = server.split('://')
|
151
|
+
{:proto => proto, :host => ip}
|
152
|
+
end
|
153
|
+
rescue
|
154
|
+
nil
|
155
|
+
end
|
156
|
+
|
136
157
|
def send_command(command, data = {})
|
137
158
|
server_list.each do |server|
|
138
159
|
begin
|
139
|
-
return XMLRPC::Client.new(server[:
|
160
|
+
return XMLRPC::Client.new(server[:host], "/#{API_VERSION}").call(command, data.merge(authentication_hash))
|
140
161
|
# TODO: Rescue more stuff (Connection Timeout and such)
|
141
162
|
rescue XMLRPC::FaultException => error
|
142
163
|
case error.faultCode
|
data/test/mollom_test.rb
CHANGED
@@ -34,25 +34,63 @@ class TestMollom < Test::Unit::TestCase
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def test_server_list
|
37
|
+
Mollom.any_instance.expects(:get_server_list_from).with(:host => 'xmlrpc3.mollom.com', :proto => 'http').returns([{:host => '172.16.0.1', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'https'}])
|
38
|
+
assert_equal [{:host => '172.16.0.1', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'https'}], @mollom.server_list
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_server_list_with_first_server_bad
|
42
|
+
Mollom.any_instance.expects(:get_server_list_from).with(:host => 'xmlrpc3.mollom.com', :proto => 'http').returns(nil)
|
43
|
+
Mollom.any_instance.expects(:get_server_list_from).with(:host => 'xmlrpc2.mollom.com', :proto => 'http').returns([{:host => '172.16.0.1', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'https'}])
|
44
|
+
assert_equal [{:host => '172.16.0.1', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'https'}], @mollom.server_list
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_server_list_with_all_servers_bad
|
48
|
+
Mollom.any_instance.expects(:get_server_list_from).with(:host => 'xmlrpc3.mollom.com', :proto => 'http').returns(nil)
|
49
|
+
Mollom.any_instance.expects(:get_server_list_from).with(:host => 'xmlrpc2.mollom.com', :proto => 'http').returns(nil)
|
50
|
+
Mollom.any_instance.expects(:get_server_list_from).with(:host => 'xmlrpc1.mollom.com', :proto => 'http').returns(nil)
|
51
|
+
assert_raise(Mollom::Error) { @mollom.server_list }
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_get_server_list_from_with_ok_return
|
37
55
|
xml_rpc = mock
|
38
|
-
xml_rpc.expects(:call).with('mollom.getServerList', is_a(Hash)).returns(['http://172.16.0.1', 'http://172.16.0.2', 'https://172.16.0.2'])
|
39
|
-
XMLRPC::Client.
|
40
|
-
|
41
|
-
assert_equal [{:ip => '172.16.0.1', :proto => 'http'}, {:ip => '172.16.0.2', :proto => 'http'}, {:ip => '172.16.0.2', :proto => 'https'}], @mollom.server_list
|
56
|
+
xml_rpc.expects(:call).times(1).with('mollom.getServerList', is_a(Hash)).returns(['http://172.16.0.1', 'http://172.16.0.2', 'https://172.16.0.2'])
|
57
|
+
XMLRPC::Client.stubs(:new).with('xmlrpc.mollom.com', '/1.0').returns(xml_rpc)
|
58
|
+
assert_equal([{:host => '172.16.0.1', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'https'}], @mollom.send(:get_server_list_from, {:host => 'xmlrpc.mollom.com', :proto => 'http'}))
|
42
59
|
end
|
43
60
|
|
44
|
-
def
|
61
|
+
def test_get_server_list_from_with_raising_return
|
45
62
|
xml_rpc = mock
|
46
|
-
xml_rpc.expects(:call).times(
|
63
|
+
xml_rpc.expects(:call).times(1).with('mollom.getServerList', is_a(Hash)).raises(XMLRPC::FaultException.new(1000, "Broken mollom"))
|
47
64
|
XMLRPC::Client.stubs(:new).with('xmlrpc.mollom.com', '/1.0').returns(xml_rpc)
|
65
|
+
assert_equal(nil, @mollom.send(:get_server_list_from, {:host => 'xmlrpc.mollom.com', :proto => 'http'}))
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_server_list_force_reload
|
69
|
+
Mollom.any_instance.expects(:get_server_list_from).times(2).with(:host => 'xmlrpc3.mollom.com', :proto => 'http').returns([{:host => '172.16.0.1', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'https'}])
|
48
70
|
|
49
71
|
@mollom.server_list
|
50
72
|
@mollom.server_list
|
51
73
|
@mollom.server_list(true)
|
52
74
|
end
|
53
75
|
|
76
|
+
def test_server_list_setter_with_good_list
|
77
|
+
@mollom.server_list = [{:host => '172.16.0.1', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'http'}]
|
78
|
+
assert_equal [{:host => '172.16.0.1', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'http'}], @mollom.instance_variable_get('@server_list')
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_send_command_with_old_server_list
|
82
|
+
@mollom.server_list = [{:ip => '172.16.0.1', :proto => 'http'}, {:ip => '172.16.0.2', :proto => 'http'}]
|
83
|
+
assert_equal nil, @mollom.instance_variable_get('@server_list')
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_send_command_with_bad_server_list
|
87
|
+
@mollom.server_list = "404 Not Found Bad User Input"
|
88
|
+
assert_equal nil, @mollom.instance_variable_get('@server_list')
|
89
|
+
end
|
90
|
+
|
91
|
+
|
54
92
|
def test_send_command_with_good_server
|
55
|
-
Mollom.any_instance.expects(:server_list).returns([{:
|
93
|
+
Mollom.any_instance.expects(:server_list).returns([{:host => '172.16.0.1', :proto => 'http'}])
|
56
94
|
xml_rpc = mock
|
57
95
|
xml_rpc.expects(:call).with('mollom.testMessage', has_entry(:options => 'foo'))
|
58
96
|
XMLRPC::Client.expects(:new).with('172.16.0.1', '/1.0').returns(xml_rpc)
|
@@ -62,7 +100,7 @@ class TestMollom < Test::Unit::TestCase
|
|
62
100
|
|
63
101
|
|
64
102
|
def test_send_command_with_bad_server
|
65
|
-
Mollom.any_instance.expects(:server_list).returns([{:
|
103
|
+
Mollom.any_instance.expects(:server_list).returns([{:host => '172.16.0.1', :proto => 'http'}, {:host => '172.16.0.2', :proto => 'http'}])
|
66
104
|
xml_rpc = mock
|
67
105
|
xml_rpc.expects(:call).with('mollom.testMessage', has_entry(:options => 'foo')).raises(XMLRPC::FaultException.new(1200, "Redirect"))
|
68
106
|
xml_rpc2 = mock
|
@@ -74,7 +112,7 @@ class TestMollom < Test::Unit::TestCase
|
|
74
112
|
end
|
75
113
|
|
76
114
|
def test_send_command_with_reload_exception
|
77
|
-
Mollom.any_instance.stubs(:server_list).returns([{:
|
115
|
+
Mollom.any_instance.stubs(:server_list).returns([{:host => '172.16.0.1', :proto => 'http'}], [{:host => '172.16.0.2', :proto => 'http'}])
|
78
116
|
xml_rpc = mock
|
79
117
|
xml_rpc.expects(:call).with('mollom.testMessage', has_entry(:options => 'foo')).raises(XMLRPC::FaultException.new(1100, "Refresh"))
|
80
118
|
xml_rpc2 = mock
|
@@ -86,7 +124,7 @@ class TestMollom < Test::Unit::TestCase
|
|
86
124
|
end
|
87
125
|
|
88
126
|
def test_send_command_with_bad_command
|
89
|
-
Mollom.any_instance.expects(:server_list).returns([{:
|
127
|
+
Mollom.any_instance.expects(:server_list).returns([{:host => '172.16.0.1', :proto => 'http'}])
|
90
128
|
xml_rpc = mock
|
91
129
|
xml_rpc.expects(:call).with('mollom.testMessage', has_entry(:options => 'foo')).raises(XMLRPC::FaultException.new(1000, "Fault String"))
|
92
130
|
XMLRPC::Client.expects(:new).with('172.16.0.1', '/1.0').returns(xml_rpc)
|
@@ -95,7 +133,7 @@ class TestMollom < Test::Unit::TestCase
|
|
95
133
|
end
|
96
134
|
|
97
135
|
def test_send_command_with_bad_server_and_no_more_available
|
98
|
-
Mollom.any_instance.expects(:server_list).returns([{:
|
136
|
+
Mollom.any_instance.expects(:server_list).returns([{:host => '172.16.0.1', :proto => 'http'}])
|
99
137
|
xml_rpc = mock
|
100
138
|
xml_rpc.expects(:call).with('mollom.testMessage', has_entry(:options => 'foo')).raises(XMLRPC::FaultException.new(1200, "Redirect"))
|
101
139
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mollom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan De Poorter
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-09-25 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -46,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
46
|
requirements: []
|
47
47
|
|
48
48
|
rubyforge_project: mollom
|
49
|
-
rubygems_version: 1.
|
49
|
+
rubygems_version: 1.2.0
|
50
50
|
signing_key:
|
51
51
|
specification_version: 2
|
52
52
|
summary: Ruby class for easy interfacing with the mollom.com open API for spam detection and content quality assesment.
|