Permalink
Browse files

Added basic spam protection

  • Loading branch information...
1 parent 7a18a3e commit c970deff215eecc4acc5ebce8f9488fac72c63d6 @nakajima committed May 21, 2010
Showing with 35 additions and 0 deletions.
  1. +11 −0 app/models/bookmarklet.rb
  2. +13 −0 app/models/spam_checker.rb
  3. +11 −0 test/unit/spam_checker_test.rb
@@ -1,4 +1,15 @@
class Bookmarklet < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :code
+ validate :spam_check, :if => :spam?
+
+ def spam?
+ SpamChecker.new(code).spam?
+ end
+
+ private
+
+ def spam_check
+ errors.add(:spam, "detection failed.")
+ end
end
@@ -0,0 +1,13 @@
+class SpamChecker
+ def initialize(code)
+ @code = code
+ end
+
+ def spam?
+ !matches_keywords?
+ end
+
+ def matches_keywords?
+ @code =~ /function|window|document|document\.location|alert\(/
+ end
+end
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class SpamCheckerTest < ActiveSupport::TestCase
+ def test_not_spammy
+ assert ! SpamChecker.new("function() { }").spam?
+ end
+
+ def test_spammy_when_does_not_include_function
+ assert SpamChecker.new("something").spam?
+ end
+end

0 comments on commit c970def

Please sign in to comment.