public
Fork of halorgium/mephisto
Description: A refactored Mephisto that has multiple spam detection engines.
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/francois/mephisto.git
francois (author)
Mon Mar 03 04:46:21 -0800 2008
commit  09ecfabb45832dad1d237bfc1384a5dcdd379031
tree    293788e9264066b1bd00eddd7847c365d34f21f5
parent  4cadd52ca6ef9453a65399f844102b58ed07f06f
mephisto / lib / mephisto / spam_detection_engines / akismet_engine.rb
100644 59 lines (49 sloc) 1.783 kb
1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module Mephisto
  module SpamDetectionEngines
    class AkismetEngine < Mephisto::SpamDetectionEngine::Base
      Site.register_spam_detection_engine "Akismet", self
 
      def ham?(permalink_url, comment)
        check_valid!
        !akismet.comment_check(comment_spam_options(permalink_url, comment))
      end
 
      def mark_as_ham(permalink_url, comment)
        check_valid!
        akismet.submit_ham(comment_spam_options(permalink_url, comment))
      end
 
      def mark_as_spam(permalink_url, comment)
        check_valid!
        akismet.submit_spam(comment_spam_options(permalink_url, comment))
      end
 
      def valid?
        [:akismet_key, :akismet_url].all? { |attr| !options[attr].blank? }
      end
 
      def valid_key?
        return false unless self.valid?
        false
      end
 
      def errors
        returning([]) do |es|
          es << "The Akismet key is missing" if options[:akismet_key].blank?
          es << "The Akismet url is missing" if options[:akismet_url].blank?
        end
      end
 
      protected
      def akismet
        @akismet ||= ::Akismet.new(options[:akismet_key], options[:akismet_url])
      end
 
      def comment_spam_options(permalink_url, comment)
        { :user_ip => comment.author_ip,
          :user_agent => comment.user_agent,
          :referrer => comment.referrer,
          :permalink => permalink_url,
          :comment_author => comment.author,
          :comment_author_email => comment.author_email,
          :comment_author_url => comment.author_url,
          :comment_content => comment.body}
      end
 
      def check_valid!
        raise Mephisto::SpamDetectionEngine::NotConfigured unless self.valid?
      end
    end
  end
end