| 1 | # tp-antispam.rb $Revision: 1 $ |
|---|
| 2 | # Copyright (C) 2008 Michitaka Ohno <elpeo@mars.dti.ne.jp> |
|---|
| 3 | # You can redistribute it and/or modify it under GPL2. |
|---|
| 4 | |
|---|
| 5 | require 'cgi' |
|---|
| 6 | require 'net/http' |
|---|
| 7 | |
|---|
| 8 | TYPEPADANTISPAM_VERSION = '1.0' |
|---|
| 9 | TYPEPADANTISPAM_API_VERSION = '1.1' |
|---|
| 10 | TYPEPADANTISPAM_SIG = %w|blog user_ip user_agent article_date permalink comment_type comment_author comment_author_email comment_author_url comment_content| |
|---|
| 11 | |
|---|
| 12 | class TypePadAntiSpam < Struct.new( *TYPEPADANTISPAM_SIG.map{|i| i.to_sym} ) |
|---|
| 13 | def initialize( key, charset = 'UTF-8' ) |
|---|
| 14 | @key = key |
|---|
| 15 | @charset = charset |
|---|
| 16 | self.blog = 'http://blog.example.com/' # ブログのURL |
|---|
| 17 | self.permalink = 'http://blog.example.com/entry-1.html' # エントリのURL |
|---|
| 18 | self.article_date = '2008-05-31 01:24:00' # エントリの日付 |
|---|
| 19 | self.user_ip = '192.168.0.1' # コメント投稿者のIPアドレス |
|---|
| 20 | self.comment_type = 'comment' # commentまたはtrackback |
|---|
| 21 | self.comment_author = 'foo' # コメント投稿者の名前 |
|---|
| 22 | self.comment_author_email = 'foo@bar.com' # コメント投稿者のメールアドレス |
|---|
| 23 | self.comment_author_url = 'http://blog.bar.com/' # コメント投稿者のURL |
|---|
| 24 | self.comment_content = 'Hello!' # コメントの内容 |
|---|
| 25 | end |
|---|
| 26 | |
|---|
| 27 | def check |
|---|
| 28 | header = { |
|---|
| 29 | 'User-Agent' => "TypePad AntiSpam Ruby Library/#{TYPEPADANTISPAM_VERSION}", |
|---|
| 30 | 'Content-Type' => "application/x-www-form-urlencoded; charset=#{@charset}" |
|---|
| 31 | } |
|---|
| 32 | data = [] |
|---|
| 33 | each_pair do |member, value| |
|---|
| 34 | data << "#{member}=#{CGI::escape( value )}" if value |
|---|
| 35 | end |
|---|
| 36 | r = nil |
|---|
| 37 | Net::HTTP.version_1_2 |
|---|
| 38 | Net::HTTP.start( "#{@key}.api.antispam.typepad.com" ) do |http| |
|---|
| 39 | response, = http.post( "/#{TYPEPADANTISPAM_API_VERSION}/comment-check", data*'&', header ) |
|---|
| 40 | case response.body |
|---|
| 41 | when /false/i |
|---|
| 42 | r = true |
|---|
| 43 | when /true/i |
|---|
| 44 | r = false |
|---|
| 45 | end |
|---|
| 46 | end |
|---|
| 47 | r |
|---|
| 48 | end |
|---|
| 49 | end |
|---|