stop_forum_spam 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/stop_forum_spam.rb +2 -1
- data/lib/stop_forum_spam/client.rb +25 -0
- data/lib/stop_forum_spam/spammer.rb +21 -24
- data/spec/spammer_spec.rb +104 -0
- data/spec/spec_helper.rb +22 -1
- data/spec/stop_forum_spam_spec.rb +28 -77
- metadata +5 -3
data/lib/stop_forum_spam.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__)) unless
|
2
2
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
3
|
|
4
|
+
require 'rubygems'
|
4
5
|
require 'httparty'
|
5
6
|
require 'stop_forum_spam/spammer'
|
6
|
-
|
7
|
+
require 'stop_forum_spam/client'
|
7
8
|
|
8
9
|
module StopForumSpam
|
9
10
|
VERSION = '0.0.1'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module StopForumSpam
|
2
|
+
class Client
|
3
|
+
include HTTParty
|
4
|
+
format :xml
|
5
|
+
base_uri("http://stopforumspam.com")
|
6
|
+
|
7
|
+
attr_accessor :api_key
|
8
|
+
|
9
|
+
def initialize(api_key=nil)
|
10
|
+
@api_key = api_key
|
11
|
+
end
|
12
|
+
|
13
|
+
def post(options={})
|
14
|
+
self.class.post('/post.php',
|
15
|
+
:body => {:ip_addr => options[:ip_address],
|
16
|
+
:email=> options[:email],
|
17
|
+
:username => options[:username],
|
18
|
+
:api_key => api_key})
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(options={})
|
22
|
+
self.class.get('/api', options)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,38 +1,35 @@
|
|
1
1
|
module StopForumSpam
|
2
2
|
class Spammer
|
3
|
-
|
4
|
-
format :xml
|
5
|
-
base_uri("http://stopforumspam.com:80")
|
3
|
+
attr_reader :id, :type, :frequency, :last_seen, :appears
|
6
4
|
|
7
|
-
|
5
|
+
alias_method :count, :frequency
|
6
|
+
alias_method :appears?, :appears
|
8
7
|
|
9
8
|
def initialize(id)
|
10
|
-
@id
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@type = 'email'
|
16
|
-
else
|
17
|
-
@type = 'username'
|
18
|
-
end
|
19
|
-
|
20
|
-
@frequency = response['response']["frequency"]
|
21
|
-
@last_seen = response['response']["lastseen"]
|
22
|
-
@appears = response['response']['appears'] == 'yes' ? true : false
|
9
|
+
@id = id
|
10
|
+
@type = self.class.guess_type(id)
|
11
|
+
@frequency = response["frequency"]
|
12
|
+
@last_seen = response["lastseen"]
|
13
|
+
@appears = response['appears'] == 'yes' ? true : false
|
23
14
|
end
|
24
15
|
|
25
|
-
def appears?
|
26
|
-
@appears
|
27
|
-
end
|
28
|
-
|
29
16
|
def self.is_spammer?(id)
|
30
|
-
|
17
|
+
new(id).appears?
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.guess_type(id)
|
21
|
+
return 'ip' if id.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/)
|
22
|
+
return 'email' if id.match(/.*@.*/)
|
23
|
+
return 'username'
|
31
24
|
end
|
32
25
|
|
33
|
-
private
|
26
|
+
private
|
34
27
|
def response
|
35
|
-
@response ||=
|
28
|
+
@response ||= make_request['response']
|
29
|
+
end
|
30
|
+
def make_request
|
31
|
+
client = StopForumSpam::Client.new
|
32
|
+
client.get(:query => {@type.to_sym => @id})
|
36
33
|
end
|
37
34
|
end
|
38
35
|
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StopForumSpam::Spammer do
|
4
|
+
FakeWeb.allow_net_connect = false
|
5
|
+
|
6
|
+
context 'initializing a spammer by ip, email, and username' do
|
7
|
+
{ :ip => '127.0.0.1',
|
8
|
+
:email => 'test@tester.com',
|
9
|
+
:username => 'testuser'
|
10
|
+
}.each do |id_type, v|
|
11
|
+
|
12
|
+
it "initializes with an #{id_type}" do
|
13
|
+
fake_get_response(id_type => v)
|
14
|
+
StopForumSpam::Spammer.new(v).class.should == StopForumSpam::Spammer
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should initialize a spammer by ip" do
|
20
|
+
fake_get_response(:ip => '127.0.0.1')
|
21
|
+
spammer = StopForumSpam::Spammer.new('127.0.0.1')
|
22
|
+
spammer.type.should == 'ip'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should initialize a spammer by email' do
|
26
|
+
fake_get_response(:email => 'test%40tester.com')
|
27
|
+
spammer = StopForumSpam::Spammer.new('test@tester.com')
|
28
|
+
spammer.type.should == 'email'
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should initialize a spammer by username' do
|
32
|
+
fake_get_response(:username => 'testuser')
|
33
|
+
spammer = StopForumSpam::Spammer.new('testuser')
|
34
|
+
spammer.type.should == 'username'
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should have attributes from the response' do
|
38
|
+
last_seen = "2009-04-16 23:11:19"
|
39
|
+
fake_get_response(:ip => '127.0.0.1', :last_seen => last_seen, :appears => true)
|
40
|
+
spammer = StopForumSpam::Spammer.new('127.0.0.1')
|
41
|
+
spammer.type.should == "ip"
|
42
|
+
spammer.appears?.should be_true
|
43
|
+
spammer.last_seen.should == last_seen
|
44
|
+
end
|
45
|
+
|
46
|
+
context '#count' do
|
47
|
+
it 'should count the amount of times the spammer has been added' do
|
48
|
+
fake_get_response(:ip => '127.0.0.1', :frequency => 41)
|
49
|
+
spammer = StopForumSpam::Spammer.new('127.0.0.1')
|
50
|
+
spammer.count.should == '41'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should have an id' do
|
55
|
+
fake_get_response(:ip => '127.0.0.1')
|
56
|
+
spammer = StopForumSpam::Spammer.new('127.0.0.1')
|
57
|
+
spammer.id.should == '127.0.0.1'
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should find a spammer by ip" do
|
61
|
+
fake_get_response(:ip => '127.0.0.1', :appears => true)
|
62
|
+
StopForumSpam::Spammer.is_spammer?("127.0.0.1").should be_true
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should return false when the spammer is not found by ip" do
|
66
|
+
fake_get_response(:ip => '127.0.0.1', :appears => false)
|
67
|
+
StopForumSpam::Spammer.is_spammer?('127.0.0.1').should be_false
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should find a spammer by email" do
|
71
|
+
fake_get_response(:email => 'test%40tester.com', :appears => true)
|
72
|
+
StopForumSpam::Spammer.is_spammer?("test@tester.com").should be_true
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return false when a spammer is not found by email" do
|
76
|
+
fake_get_response(:email => 'test%40tester.com', :appears => false)
|
77
|
+
StopForumSpam::Spammer.is_spammer?('test@tester.com').should be_false
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should find a spammer by username" do
|
81
|
+
fake_get_response(:username => 'testuser', :appears => true)
|
82
|
+
StopForumSpam::Spammer.is_spammer?('testuser').should be_true
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should return false when a spammer is not found by username" do
|
86
|
+
fake_get_response(:username => 'testuser', :appears => false)
|
87
|
+
StopForumSpam::Spammer.is_spammer?('testuser').should be_false
|
88
|
+
end
|
89
|
+
|
90
|
+
context '#guess_type' do
|
91
|
+
it 'should return the ip type' do
|
92
|
+
StopForumSpam::Spammer.guess_type('127.0.0.1').should.eql? "ip"
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should return the email type ' do
|
96
|
+
StopForumSpam::Spammer.guess_type('test@testuser.com').should.eql? "email"
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should return the username type' do
|
100
|
+
StopForumSpam::Spammer.guess_type("testuser").should.eql? 'username'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
1
3
|
require 'spec'
|
2
4
|
require 'fakeweb'
|
3
5
|
|
4
|
-
require File.join(File.dirname(__FILE__), '..', 'lib', 'stop_forum_spam')
|
6
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'stop_forum_spam')
|
7
|
+
|
8
|
+
def fake_get_response(options={})
|
9
|
+
id_type = [:ip, :email, :username].select { |k| options.has_key?(k) }.first.to_s
|
10
|
+
url = "http://stopforumspam.com/api?#{id_type}=#{options[id_type.to_sym]}"
|
11
|
+
options[:type] = id_type
|
12
|
+
options[:string] = format_options(options)
|
13
|
+
FakeWeb.register_uri(:get, url, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def fake_post_response(api_key, ip_addr, email, username, options={})
|
17
|
+
FakeWeb.register_uri(:post, "http://stopforumspam.com:80/post.php", options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def format_options(options)
|
21
|
+
appears = options[:appears]
|
22
|
+
frequency = options[:frequency]
|
23
|
+
last_seen = options[:last_seen]
|
24
|
+
"<response success='true'><type>#{options[:type]}</type><appears>#{appears ? 'yes' : 'no'}</appears><frequency>#{appears ? '0' : frequency}</frequency><lastseen>#{last_seen}</lastseen></response>"
|
25
|
+
end
|
@@ -1,78 +1,29 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe StopForumSpam::
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
spammer.type.should == "ip"
|
30
|
-
spammer.appears?.should be_true
|
31
|
-
spammer.last_seen.should == "2009-04-16 23:11:19"
|
32
|
-
spammer.frequency == '41'
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'should have an id' do
|
36
|
-
FakeWeb.register_uri(:get, "http://stopforumspam.com/api?ip=127.0.0.1",
|
37
|
-
:string => "<response success='true'><type>ip</type><appears>yes</appears><lastseen>2009-04-16 23:11:19</lastseen><frequency>41</frequency></response>")
|
38
|
-
spammer = StopForumSpam::Spammer.new('127.0.0.1')
|
39
|
-
spammer.id.should == '127.0.0.1'
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should find a spammer by ip" do
|
43
|
-
FakeWeb.allow_net_connect = false
|
44
|
-
FakeWeb.register_uri(:get, "http://stopforumspam.com/api?ip=127.0.0.1",
|
45
|
-
:string => "<response success='true'><type>ip</type><appears>yes</appears><lastseen>2009-04-16 23:11:19</lastseen><frequency>41</frequency></response>")
|
46
|
-
StopForumSpam::Spammer.is_spammer?("127.0.0.1").should be_true
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should return false when the spammer is not found by ip" do
|
50
|
-
FakeWeb.register_uri(:get, "http://stopforumspam.com/api?ip=127.0.0.1",
|
51
|
-
:string => "<response success='true'><type>ip</type><appears>no</appears><frequency>0</frequency></response>")
|
52
|
-
StopForumSpam::Spammer.is_spammer?('127.0.0.1').should be_false
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should find a spammer by email" do
|
56
|
-
FakeWeb.register_uri(:get, "http://stopforumspam.com/api?email=test%40tester.com",
|
57
|
-
:string => "<response success='true'><type>email</type><appears>yes</appears><lastseen>2009-06-25 00:24:29</lastseen><frequency>2</frequency></response>")
|
58
|
-
StopForumSpam::Spammer.is_spammer?("test@tester.com").should be_true
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should return false when a spammer is not found by email" do
|
62
|
-
FakeWeb.register_uri(:get, "http://stopforumspam.com/api?email=test%40tester.com",
|
63
|
-
:string => "<response success='true'><type>email</type><appears>no</appears><frequency>0</frequency></response>")
|
64
|
-
StopForumSpam::Spammer.is_spammer?('test@tester.com').should be_false
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should find a spammer by username" do
|
68
|
-
FakeWeb.register_uri(:get, "http://stopforumspam.com/api?username=testuser",
|
69
|
-
:string => "<response success='true'><type>username</type><appears>yes</appears><frequency>10</frequency></response>")
|
70
|
-
StopForumSpam::Spammer.is_spammer?('testuser')
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should return false when a spammer is not found by username" do
|
74
|
-
FakeWeb.register_uri(:get, "http://stopforumspam.com/api?username=testuser",
|
75
|
-
:string => "<response success='true'><type>username</type><appears>no</appears><frequency>0</frequency></response>")
|
76
|
-
StopForumSpam::Spammer.is_spammer?('testuser')
|
77
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StopForumSpam::Client do
|
4
|
+
|
5
|
+
context "a new client" do
|
6
|
+
it "should instantiate" do
|
7
|
+
StopForumSpam::Client.new.class.should == StopForumSpam::Client
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should initialize with an optional api key" do
|
11
|
+
client = StopForumSpam::Client.new('123456789')
|
12
|
+
client.api_key.should == '123456789'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'posts a spammer' do
|
17
|
+
context 'with a valid ip address, email address, and username' do
|
18
|
+
before do
|
19
|
+
fake_post_response('123456789', '127.0.0.1', 'spammer%40ru.com', 'spammer500', :string => "valid post")
|
20
|
+
client = StopForumSpam::Client.new('123456789')
|
21
|
+
@response = client.post(:id_address => '127.0.0.1', :email => 'spammer@ru.com', :username => 'spammer500')
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return true " do
|
25
|
+
@response.should be_true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
78
29
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stop_forum_spam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lake Denman
|
@@ -9,11 +9,11 @@ autorequire: httparty
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-09 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: Small api wrapper for
|
16
|
+
description: Small api wrapper for getting and posting data to http://stopforumspam.com
|
17
17
|
email: lake@lakedenman.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -22,8 +22,10 @@ extensions: []
|
|
22
22
|
extra_rdoc_files: []
|
23
23
|
|
24
24
|
files:
|
25
|
+
- lib/stop_forum_spam/client.rb
|
25
26
|
- lib/stop_forum_spam/spammer.rb
|
26
27
|
- lib/stop_forum_spam.rb
|
28
|
+
- spec/spammer_spec.rb
|
27
29
|
- spec/spec_helper.rb
|
28
30
|
- spec/stop_forum_spam_spec.rb
|
29
31
|
has_rdoc: true
|