1. Skip to navigation
  2. Skip to content

The ELC Community Blog

A knowledge exchange on Ruby on Rails and Agile Development


Defensio Lite

by Ryan Garver on May 14, 2008

This is a quick post, but I wanted to point out that our new commenting system is now using Defensio spam filtering! This is good because after a day of watching the commenting statistics its pretty clear that we would have been consumed by a porn site or something by the end of the week. The code that we used for this is super simple (possibly too simple) and duplicates some work already done by the talented Marc-André. Oh well. Sometimes you just need to do it yourself. Below is my Defensio API.

   1  class Defensio
   2    cattr_accessor :format
   3    self.format = :xml
   4    
   5    cattr_accessor :service_type
   6    self.service_type = :app # Can be :blog
   7    
   8    cattr_accessor :api_version
   9    self.api_version = '1.2'
  10    
  11    cattr_accessor :api_key
  12    cattr_accessor :owner_url
  13    
  14    def self.configure(confhash)
  15      if confhash['test']
  16        @mock = true
  17        self.owner_url = 'http://www.example.com'
  18        return
  19      else
  20        confhash.each do |prop, val|
  21          self.send("#{prop}=", val)
  22        end
  23      end
  24    end
  25    
  26    def self.method_missing(name, *args)
  27      self.post(name.to_s.dasherize, *args)
  28    end
  29    
  30    private
  31      def self.connection
  32        uri = URI.parse('http://api.defensio.com/')
  33        Net::HTTP.start(uri.host, uri.port)
  34      end
  35    
  36      def self.post(action, params = {})
  37        resp = connection.post(real_path(action), params_from_hash(params))
  38        raise "Problem with request: #{action}" unless resp.code == '200'
  39        parse_response(resp.body)
  40      end
  41    
  42      def self.real_path(action)
  43        "/#{service_type}/#{api_version}/#{action}/#{api_key}.#{format}"
  44      end
  45    
  46      def self.params_from_hash(params = {})
  47        # Thanks Net::HTTPHeader
  48        params.stringify_keys.merge('owner-url' => owner_url).map {|k,v| "#{CGI.escape(k.dasherize.to_s)}=#{CGI.escape(v.to_s)}" }.join('&') 
  49      end
  50    
  51      def self.parse_response(body)
  52        case format
  53        when :yaml
  54          YAML.load(body)
  55        when :xml
  56          Hash.from_xml(body)
  57        end
  58      end
  59  end

I clearly didn't spend much time polishing this, but the usage is a pretty straight forward mapping from the API docs. So to announce an article I call:

   1  Defensio.announce_article(:article_author => 'Ryan Garver', :article_author_email => 'rgarver@domain.com', :article_title => 'Defensio Lite', ... )

There are also some site wide values that are set in a yml file. I'll close with an example.

   1  development:
   2    api_key: a09f87a09f87a098f7a098f7a098f7a0
   3    owner_url: http://www.example.com
   4  
   5  staging:
   6    api_key: 12f3412f341f234f123f4123f412f34f
   7    owner_url: http://mystaging_blog.com
   8  
   9  production:
  10    api_key: 123f412f412f412f3412f3412f3412f3
  11    owner_url: http://elctech.com
  12  
  13  test:
  14    test: true

Comments

Add a comment


home | services | Ruby on Rails Development | code | blog | company