class Akismet

Constants

HOST
NORMAL_RESPONSES
PORT
STANDARD_HEADERS
TIMEOUT_THRESHOLD
VALID_RESPONSES

Attributes

host[RW]
normal_responses[RW]
options[RW]
port[RW]
standard_headers[RW]
valid_responses[RW]
verified_key[RW]

Public Class Methods

new(options) click to toggle source

Create a new instance of the Akismet class

Arguments

Arguments are provided in the form of a Hash with the following keys (as Symbols) available:

api_key

your Akismet API key

blog

the blog associated with your api key

The following keys are available and are entirely optional. They are available incase communication with Akismet's servers requires a proxy port and/or host:

  • proxy_port

  • proxy_host

# File lib/akismet.rb, line 34
def initialize(options)
        @options = options
        self.verified_key = false
end
url(action) click to toggle source

Returns the URL for an Akismet request

Arguments

action <~to_s>

a valid Akismet function name

Returns

String

# File lib/akismet.rb, line 84
def self.url(action)
        "/1.1/#{action}"
end

Public Instance Methods

check_comment(options={}) click to toggle source
# File lib/akismet.rb, line 48
def check_comment(options={})
        return false if invalid_options?
        message = call_akismet('comment-check', options)
        {:spam => !VALID_RESPONSES.include?(message), :message => message}
end
invalid_options?() click to toggle source
# File lib/akismet.rb, line 44
def invalid_options?
        false
end
mark_as_ham(options={}) click to toggle source

This call is intended for the marking of false positives, things that were incorrectly marked as spam. It takes identical arguments as check_comment and mark_as_spam.

# File lib/akismet.rb, line 72
def mark_as_ham(options={})
        return false if invalid_options?
        {:message => call_akismet('submit-ham', options)}
end
mark_as_spam(options={}) click to toggle source

This call is for submitting comments that weren't marked as spam but should have been (i.e. false negatives). It takes identical arguments as check_comment.

# File lib/akismet.rb, line 64
def mark_as_spam(options={})
        return false if invalid_options?
        {:message => call_akismet('submit-spam', options)}
end
spam?(options = {}) click to toggle source
# File lib/akismet.rb, line 54
def spam?(options = {})
        if resp = check_comment(options)
                return resp[:spam]
        end
        false
end
verified?() click to toggle source

Returns true if the API key has been verified, false otherwise

# File lib/akismet.rb, line 40
def verified?
        (@verified_key ||= verify_api_key) != :false
end

Protected Instance Methods

call_akismet(akismet_function, options={}) click to toggle source

Internal call to Akismet. Prepares the data for posting to the Akismet service.

Arguments

akismet_function <String>

the Akismet function that should be called

The following keys are available to configure a given call to Akismet:

user_ip (required)

IP address of the comment submitter.

user_agent (required)

user agent information.

referrer (note spelling)

the content of the HTTP_REFERER header should be sent here.

permalink

the permanent location of the entry the comment was submitted to.

comment_type

may be blank, comment, trackback, pingback, or a made up value like “registration”.

comment_author

submitted name with the comment

comment_author_email

submitted email address

comment_author_url

commenter URL

comment_content

the content that was submitted

# File lib/akismet.rb, line 117
def call_akismet(akismet_function, options={})
        http_post http_instance, akismet_function, options.update(:blog => options[:blog])
end
http_post(http, action, options = {}) click to toggle source
# File lib/akismet.rb, line 129
def http_post(http, action, options = {})
        params = options.map{ |key, val| "#{key}=#{URI.encode(val || '')}"}.join('&')
        resp = http.post(self.url(action), params, STANDARD_HEADERS)
        log_request(self.url(action), params, resp)
        resp.body
end
url(action) click to toggle source
# File lib/akismet.rb, line 136
def url(action)
        "/1.1/#{action}"
end
verify_api_key() click to toggle source

Call to check and verify your API key. You may then call the verified? method to see if your key has been validated

# File lib/akismet.rb, line 123
def verify_api_key
        return :false if invalid_options?
        value = http_post http_instance, 'verify-key', :key  => options[:api_key], :blog => options[:blog]
        self.verified_key = (value == "valid") ? true : :false
end

Private Instance Methods

http_instance() click to toggle source
# File lib/akismet.rb, line 148
def http_instance
        http = Net::HTTP.new([options[:api_key], HOST].join("."), options[:proxy_host], options[:proxy_port])
        http.read_timeout = http.open_timeout = TIMEOUT_THRESHOLD
        http
end
log_request(url, data, resp) click to toggle source
# File lib/akismet.rb, line 142
def log_request(url, data, resp)
        #
end