forked from karmi/marley
/
akismetor.rb
51 lines (40 loc) · 1.38 KB
/
akismetor.rb
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
require 'net/http'
# http://railscasts.com/episodes/65-stopping-spam-with-akismet
# http://github.com/hornbeck/blerb-core/tree/master/lib/akismetor.rb
class Akismetor
attr_accessor :attributes
# Does a comment-check on Akismet with the submitted hash.
# Returns true or false depending on response.
def self.spam?(attributes)
self.new(attributes).execute('comment-check') == "true"
end
# Does a submit-spam on Akismet with the submitted hash.
# Use this when Akismet incorrectly approves a spam comment.
def self.submit_spam(attributes)
self.new(attributes).execute('submit-spam')
end
# Does a submit-ham on Akismet with the submitted hash.
# Use this for a false positive, when Akismet incorrectly rejects a normal comment
def self.submit_ham(attributes)
self.new(attributes).execute('submit-ham')
end
def initialize(attributes)
@attributes = attributes
end
def execute(command)
http = Net::HTTP.new("#{attributes[:key]}.rest.akismet.com", 80)
response, content = http.post("/1.1/#{command}", attributes_for_post, http_headers)
content
end
private
def http_headers
{
'User-Agent' => 'Akismetor Ruby Library/1.0',
'Content-Type' => 'application/x-www-form-urlencoded'
}
end
def attributes_for_post
result = attributes.map { |k, v| "#{k}=#{v}" }.join('&')
URI.escape(result)
end
end