Ruby library for the Akismet anti-spam service.

Usage

First you need an Akismet (or Typepad Antispam) API key. Then you need to setup a few configuration variables:

Akismet.key  = '123456789'
Akismet.blog = 'http://example.com'

To use Typepad Antispam, just specify the host:

Akismet.host = 'api.antispam.typepad.com'

Then you need to call any of the methods with a few attributes, and possibly an ActionDispatch::Request object.

Documentation

Check rubydoc.info:

rubydoc.info/github/ysbaddaden/ruby-akismet/master/frames

Or generate your own:

~/src/ruby-akismet

Integrate with Ruby on Rails

ruby-akismet integrates nicely with Ruby on Rails, but isn't tied to it except for the ActionDispatch::Request object, which isn't required. It should be easily integratable with your favorite framework like Sinatra and Merb.

Rails 3

Add this gem to your Gemfile:

gem 'ruby-akismet', :require => 'akismet'

Rails 2

First install the gem:

gem install ruby-akismet

Then add it to your app:

config.gem 'ruby-akismet', :lib => 'akismet'

Configuration

Create an initializer file like config/initializers/akismet.rb with your configuration:

Akismet.key    = '123456789'
Akismet.blog   = 'http://example.com'
Akismet.logger = Rails.logger

Usage

ruby-akismet is meant to be used on the controller side and not on the model side, because the Akismet API requires some data that's only available from the HTTP request –like the user and proxy IP, referer, etc.

Here is a Rails 3 example:

class CommentsController < ApplicationController
  before_filter :set_post

  respond_to :html, :xml

  def create
    @comment = @post.comments.new(params[:comment])
    @comment.spam = Akismet.spam?(akismet_attributes, request)
    @comment.save
    respond_with(@comment, :location => @post)
  end

  def spam
    @comment = Comment.find(params[:id])
    @comment.update_attribute(:spam, false)
    Akismet.submit_spam(akismet_attributes)
    respond_with(@comment, :location => @post)
  end

  def ham
    @comment = Comment.find(params[:id])
    @comment.update_attribute(:spam, false)
    Akismet.submit_ham(akismet_attributes)
    respond_with(@comment, :location => @post)
  end

  private
    def akismet_attributes
      {
        :comment_author       => @comment.author,
        :comment_author_url   => @comment.author_url,
        :comment_author_email => @comment.author_email,
        :comment_content      => @comment.body,
        :permalink            => post_url(@post)
      }
    end

    def set_post
      @post = Post.find(params[:post_id])
    end
end

Author

  • Julien Portalier <ysbaddaden@gmail.com>

ruby-akismet is a complete rewrite of Akismetor by Ryan Bates and Levy Carneiro Jr. that you can find at github.com/levycarneiro/akismetor