validates_against_stopforumspam 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Richard Hirner <hirner@bitfire.at>
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.
data/README ADDED
@@ -0,0 +1,60 @@
1
+ ValidatesAgainstStopforumspam
2
+ =============================
3
+
4
+ Rails gem for ActiveRecord comment model validation against StopForumSpam.com.
5
+ No API key required. Respect their terms of use: http://www.stopforumspam.com/apis
6
+
7
+ Compatibility: Rails 3, tested with Ruby 1.8.7
8
+
9
+
10
+ Installation
11
+ ============
12
+
13
+ Specify the gem in your Gemfile:
14
+
15
+ gem "validates_against_stopforumspam"
16
+
17
+ or for the current edge version:
18
+
19
+ gem "validates_against_stopforumspam", :git => 'git://github.com/rfc2822/validates_against_stopforumspam'
20
+
21
+ and install it with bundler.
22
+
23
+
24
+ Example
25
+ =======
26
+
27
+ Assuming that you have a model +Comment+ with the following fields:
28
+
29
+ t.string :name, :null => false
30
+ t.string :email
31
+ t.string :ip_address
32
+ t.string :comment, :null => false
33
+
34
+ You can use validates_against_stopforumspam to validate new comments:
35
+
36
+ class Comment < ActiveRecord::Base
37
+ validates_against_stopforumspam :fields => { :username => name, :email => :email, :ip => :ip_address }
38
+ end
39
+
40
+ The +fields+ parameter is required. It must define at least one of these parameters:
41
+
42
+ * :username
43
+ * :email
44
+ * :ip
45
+
46
+ These parameters are used to query StopForumSpam. When the comment may be spam (because at
47
+ least one of the parameters appear on stopforumspam.com), an error is added to the model instance.
48
+
49
+
50
+ To do
51
+ =====
52
+
53
+ If you like and/or use this plug-in, please consider extending it. What is needed:
54
+
55
+ * i18n'ed error message
56
+
57
+ Please submit patches via Github.
58
+
59
+
60
+ Copyright (c) 2011 Richard Hirner <hirner@bitfire.at>, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the validates_against_stopforumspam plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the validates_against_stopforumspam plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'ValidatesAgainstStopforumspam'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
@@ -0,0 +1,33 @@
1
+ require "net/http"
2
+ require "uri"
3
+
4
+ module ValidatesAgainstStopForumSpam
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def validates_against_stopforumspam(options)
11
+ validate do |comment|
12
+ query_options = []
13
+ options[:fields].each do |type, name|
14
+ value = comment.read_attribute(name)
15
+ query_options.push "#{type}=#{URI.escape(value)}" unless value.nil?
16
+ end
17
+ return if query_options.empty?
18
+ url = "http://www.stopforumspam.com/api?" + query_options.join('&')
19
+ logger.info "Querying StopForumSpam.com: #{url}"
20
+ begin
21
+ sfs = Hash.from_xml(Net::HTTP.get(URI.parse(url)))
22
+ #logger.debug sfs.inspect
23
+ errors.add :base, "may be spam according to StopForumSpam.com" if [ sfs["response"]["appears"] ].flatten.include?("yes")
24
+ rescue
25
+ logger.warn "StopForumSpam validation not successful"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+
33
+ ActiveRecord::Base.send(:include, ValidatesAgainstStopForumSpam)
data/test/schema.rb ADDED
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :comments do |t|
3
+ t.string :name
4
+ t.string :email
5
+ t.string :ip
6
+ t.string :comment
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'logger'
3
+ require 'test/unit'
4
+ require 'active_record'
5
+ require 'active_support'
6
+
7
+ ENV['RAILS_ENV'] = 'test'
8
+
9
+ def load_schema
10
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
11
+ ActiveRecord::Base.establish_connection({
12
+ :adapter => 'sqlite3',
13
+ :database => ':memory:'
14
+ })
15
+ load(File.dirname(__FILE__) + "/schema.rb")
16
+ end
17
+
@@ -0,0 +1,47 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require File.dirname(__FILE__) + '/../rails/init'
3
+
4
+ class ValidatesAgainstStopForumSpamTest < ActiveSupport::TestCase
5
+ load_schema
6
+
7
+ class Comment < ActiveRecord::Base
8
+ validates_against_stopforumspam :fields => { :email => :email, :ip => :ip, :username => :name }
9
+ end
10
+
11
+ # test data: see http://www.stopforumspam.com/forum/viewtopic.php?pid=14680
12
+
13
+ test "empty comment" do
14
+ comment = Comment.new
15
+ assert comment.valid?
16
+ end
17
+
18
+ test "ham email" do
19
+ comment = Comment.create(:email => 'this-is-not-a@spammer-but-a-hammer.com')
20
+ assert comment.valid?
21
+ end
22
+
23
+ test "ham email and IP" do
24
+ comment = Comment.create(:email => 'this-is-not-a@spammer-but-a-hammer.com', :ip => '127.0.0.1')
25
+ assert comment.valid?
26
+ end
27
+
28
+ test "spam email" do
29
+ comment = Comment.create(:email => 'test@test.com')
30
+ assert comment.invalid?
31
+ end
32
+
33
+ test "spam email and ip" do
34
+ comment = Comment.create(:email => 'test@test.com', :ip => '1.2.3.4')
35
+ assert comment.invalid?
36
+ end
37
+
38
+ test "spam ip" do
39
+ comment = Comment.create(:ip => '1.2.3.4')
40
+ assert comment.invalid?
41
+ end
42
+
43
+ test "spam username" do
44
+ comment = Comment.create(:name => 'spammer')
45
+ assert comment.invalid?
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ require "rake"
2
+
3
+ PKG_FILES = FileList[
4
+ '[a-zA-Z]*',
5
+ 'generators/**/*',
6
+ 'lib/**/*',
7
+ 'rails/**/*',
8
+ 'tasks/**/*',
9
+ 'test/**/*'
10
+ ]
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = "validates_against_stopforumspam"
14
+ s.version = "0.0.1"
15
+ s.author = "Richard Hirner"
16
+ s.email = "hirner@bitfire.at"
17
+ s.homepage = "http://github.com/rfc2822/validates_against_stopforumspam"
18
+ s.platform = Gem::Platform::RUBY
19
+ s.summary = "ActiveRecord model validation against stopforumspam.com"
20
+ s.files = PKG_FILES.to_a
21
+ s.require_path = "lib"
22
+ s.has_rdoc = false
23
+ s.extra_rdoc_files = ["README"]
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_against_stopforumspam
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Richard Hirner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-02 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: hirner@bitfire.at
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README
30
+ files:
31
+ - validates_against_stopforumspam.gemspec
32
+ - MIT-LICENSE
33
+ - Rakefile
34
+ - README
35
+ - lib/validates_against_stopforumspam.rb
36
+ - test/validates_against_stopforumspam_test.rb
37
+ - test/schema.rb
38
+ - test/test_helper.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/rfc2822/validates_against_stopforumspam
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: ActiveRecord model validation against stopforumspam.com
73
+ test_files: []
74
+