Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

52 lines (40 sloc) 1.409 kb
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
Jump to Line
Something went wrong with that request. Please try again.