Defensio on Rails

I know what you’re thinking: what kind of lazy ass are your Marc ? Did you get a life, lost your computer, your girlfriend went into labour ? More then a month since I released a project! Wow, I must have been pretty busy…

While talking with Carl at the last Montreal on Rails meeting, it occurred to me that it would be cool to integrate spam filtering with Defensio in my next project (more details on this eventually).

Defensio has received good and bad reviews, but from what I understand, it really gets better with time as opposite to Akismet who plateau pretty soon. I think one of the reason why Defensio might be better is that it uses much more data to classify your comments. Not just the content and user IP but the author email, the date of the article and the color of the sox the author’s sister’s father in law wore last weekend. That might be hard and tortuous to supply, especially when he’s on vacation. So that’s why I though I could make a Rails plugin that make it easy to supply all this info without hunting into your sister’s father in law wardrobe.

Get to the point already

1) The first thing your need to do to use the defensio Rails plugin is to install it:

script/plugin install

2) Then you might want to create a config file in /yourapp/config/defensio.yml

  api_key: 1234abc

  api_key: 1234abc

  api_key: 6789xyz

You’ll need to get an API key at this point. Ask the guys at Defensio, or spam Carl until he gives you one.

3) Next step is to add the columns used to store the comment spaminess level

script/generate defensio_migration comment

(Replace comment with the name of your model that you want to be marked as spam/ham)

Run it like there’s no tomorrow !

rake db:migrate

4) In your article class (the one that holds the comments), add:


By default the following fields will be looked up for values: author, author_email, title, content, permalink.
You can change those fields with the :fields options, like this:

acts_as_defensio_article :fields => { :content => :body }

The content value will be fetched from the body method.

5) In your comment class (the one that can be spam or ham), add:


Same things apply with the :fields thinggy. Looking in the following fields by default: author, content, title, author_email, author_url, article.permalink, trusted_user and logged_in.

6) Add some magic in your controller:

class CommentsController < ApplicationController
  def index
    @ham  = @article.comments.find_all_by_spam(false)
    @spam = @article.comments.find_all_by_spam(true, :order => 'spaminess desc')

  def create
    @comment =[:comment])
    @comment.env = request.env

      if @comment.spam
        flash[:notice] = 'Your comment has been marked for review'
        flash[:notice] = 'Comment created'
      redirect_to article_url(@article)
      render :action => 'new'

  def report_as_spam
    @comment = @article.comments.find(params[:id])

  def report_as_ham
    @comment = @article.comments.find(params[:id])

  def stats
    @stats = Comment.defensio_stats

You’re done! Now go find some spammers !

Beta aka buggy

I’ve started to use this plugin myself in one of my personal project but not for very long. It might contain some bugz. Although I doubt it… I never write bugs. If you encounter any unwanted features (aka bug) please let me know in the comments here.

Thanks to the guys at Karabunga for making Defensio, using the API was a real pleasure!

Update: There are more detailed code sample in the plugin svn:

Update: Documentation is online.



Filed under rails, ruby

12 responses to “Defensio on Rails

  1. Pingback: Defensio, the blog » Blog Archive » Defensio on Rails!

  2. Pingback: Defensio launched « Marc-André Cournoyer’s blog

  3. Pingback: web1979 » Blog Archive » Thin: A New (made-in-montreal) Webserver

  4. Hi – great plugin – comprehensive functionality although the documentation and example could be more verbose.

    Question: is there a simple switch to bypass Defensio when in dev mode? I’m just concerned that I will get marked as a spammer by Defensio from all my dev posts, or from testing my “mark as spam” links etc! Or will Defensio see my IP as and ignore them effectively?

  5. Hi Adam,

    You can specify a different key in the defensio.yml file or you can simply make the act_as conditionnal:

    acts_as_defension_comment … if RAILS_ENV == ‘production’

    their’s some examples here:

    hope this helps!

  6. Marc, thanks for the plugin! Any chance we’ll see this bad boy on github any time soon?

  7. Hi,

    This is an interesting plugin and alternative to Askimet. Thanks for creating and publishing. Its very cool.

    I got one serious issue with this plugin as it is. If defensio service is not available, this plugin will raise an Defensio::Error exception that prevents my models that use defensio from loading. I just noticed this tonight.

    Here is the response from defensio.

    Invalid response from Defensio:

    The page is temporarily unavailable

    body { font-family: Tahoma, Verdana, Arial, sans-serif; }

    The page you are looking for is temporarily unavailable.
    Please try again later.

    and the stack trace….

    vendor/plugins/defensio/lib/defensio/client.rb:42:in `parse_response’
    vendor/plugins/defensio/lib/defensio/client.rb:13:in `initialize’
    vendor/plugins/defensio/lib/defensio/client.rb:226:in `new’
    vendor/plugins/defensio/lib/defensio/client.rb:226:in `call’
    vendor/plugins/defensio/lib/defensio/client.rb:102:in `validate_key’
    vendor/plugins/defensio/lib/defensio/acts_as.rb:79:in `acts_as_defensio’
    vendor/plugins/defensio/lib/defensio/acts_as.rb:17:in `acts_as_defensio_comment’


  8. jB

    Awesome. Any chance to get a git(hub) mirror up so we can use it as submodule?

    Cheers and thanks,

  9. Th

    Thanks , I don’t understand a lot. I just a beginner developer for Rails.

  10. Get reliable bug tested casino script and software for your Online Casino at Find best deals and lowest prices for online gambling scripts and software here!!

  11. You actually make it appear so easy along with your presentation but I find this topic to be really one thing that I think I would by no means understand. It seems too complicated and very vast for me. I’m having a look ahead on your subsequent put up, I will attempt to get the hold of it!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s