ruby-akismet 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +74 -0
  2. data/lib/akismet.rb +197 -0
  3. data/test/akismet_test.rb +59 -0
  4. metadata +68 -0
data/README.rdoc ADDED
@@ -0,0 +1,74 @@
1
+ Akismet compatible library for checking spams. Works with Typepad Antispam
2
+ by default, but should be useable with any Akismet compatible server.
3
+
4
+ = Usage
5
+
6
+ First you need an Akismet (or Typepad Antispam) API key. Then you need
7
+ to setup a few configuration variable.
8
+
9
+ Akismet.key = '123456789'
10
+ Akismet.blog = 'http://example.com'
11
+
12
+ To use Typepad Antispam, just set the host:
13
+
14
+ Akismet.host = 'api.antispam.typepad.com'
15
+
16
+ Then you need to call any of the methods with a few attributes,
17
+ and possibly an ActionDispatch::Request object.
18
+
19
+ = Integrate with Ruby on Rails (3.0+)
20
+
21
+ Add this gem to your Gemfile:
22
+
23
+ gem 'ruby-akismet', :require => 'akismet',
24
+ :git => 'git://github.com/ysbaddaden/ruby-akismet.git'
25
+
26
+ Create an initializer file like <code>config/initializers/akismet.rb</code>
27
+ with your configuration:
28
+
29
+ Akismet.key = '123456789'
30
+ Akismet.blog = 'http://example.com'
31
+
32
+ Then in your controller call the appropriate methods:
33
+
34
+ class CommentsController < ApplicationController
35
+ before_filter :set_post
36
+ respond_to :html, :xml
37
+
38
+ def create
39
+ @comment = @post.comments.new(params[:comment])
40
+ @comment.spam = Akismet.spam?(akismet_attributes, request)
41
+ @comment.save
42
+ respond_with(@comment, :location => @post)
43
+ end
44
+
45
+ def spam
46
+ @comment = Comment.find(params[:id])
47
+ @comment.update_attribute(:spam, false)
48
+ Akismet.submit_spam(akismet_attributes)
49
+ respond_with(@comment, :location => @post)
50
+ end
51
+
52
+ def ham
53
+ @comment = Comment.find(params[:id])
54
+ @comment.update_attribute(:spam, false)
55
+ Akismet.submit_ham(akismet_attributes)
56
+ respond_with(@comment, :location => @post)
57
+ end
58
+
59
+ private
60
+ def akismet_attributes
61
+ {
62
+ :comment_author => @comment.author,
63
+ :comment_author_url => @comment.author_url,
64
+ :comment_author_email => @comment.author_email,
65
+ :comment_content => @comment.body,
66
+ :permalink => post_url(@post)
67
+ }
68
+ end
69
+
70
+ def set_post
71
+ @post = Post.find(params[:post_id])
72
+ end
73
+ end
74
+
data/lib/akismet.rb ADDED
@@ -0,0 +1,197 @@
1
+ require 'net/http'
2
+
3
+ # Akismet compatible library for checking spams. Works with Typepad Antispam
4
+ # by default, but should be useable with any Akismet compatible server.
5
+ #
6
+ # = Usage
7
+ #
8
+ # First you need an Akismet (or Typepad Antispam) API key. Then you need
9
+ # to setup a few configuration variable.
10
+ #
11
+ # Akismet.key = '123456789'
12
+ # Akismet.blog = 'http://example.com'
13
+ #
14
+ # To use Typepad Antispam, just set the host:
15
+ #
16
+ # Akismet.host = 'api.antispam.typepad.com'
17
+ #
18
+ # Then you need to call any of the methods with a few attributes,
19
+ # and possibly an ActionDispatch::Request object.
20
+ #
21
+ # = Integrate with Ruby on Rails (3.0+)
22
+ #
23
+ # Add this gem to your Gemfile:
24
+ #
25
+ # gem 'ruby-akismet', :require => 'akismet',
26
+ # :git => 'git://github.com/ysbaddaden/ruby-akismet.git'
27
+ #
28
+ # Create an initializer file like <code>config/initializers/akismet.rb</code>
29
+ # with your configuration:
30
+ #
31
+ # Akismet.key = '123456789'
32
+ # Akismet.blog = 'http://example.com'
33
+ #
34
+ # Then in your controller call the appropriate methods:
35
+ #
36
+ # class CommentsController < ApplicationController
37
+ # before_filter :set_post
38
+ # respond_to :html, :xml
39
+ #
40
+ # def create
41
+ # @comment = @post.comments.new(params[:comment])
42
+ # @comment.spam = Akismet.spam?(akismet_attributes, request)
43
+ # @comment.save
44
+ # respond_with(@comment, :location => @post)
45
+ # end
46
+ #
47
+ # def spam
48
+ # @comment = Comment.find(params[:id])
49
+ # @comment.update_attribute(:spam, false)
50
+ # Akismet.submit_spam(akismet_attributes)
51
+ # respond_with(@comment, :location => @post)
52
+ # end
53
+ #
54
+ # def ham
55
+ # @comment = Comment.find(params[:id])
56
+ # @comment.update_attribute(:spam, false)
57
+ # Akismet.submit_ham(akismet_attributes)
58
+ # respond_with(@comment, :location => @post)
59
+ # end
60
+ #
61
+ # private
62
+ # def akismet_attributes
63
+ # {
64
+ # :comment_author => @comment.author,
65
+ # :comment_author_url => @comment.author_url,
66
+ # :comment_author_email => @comment.author_email,
67
+ # :comment_content => @comment.body,
68
+ # :permalink => post_url(@post)
69
+ # }
70
+ # end
71
+ #
72
+ # def set_post
73
+ # @post = Post.find(params[:post_id])
74
+ # end
75
+ # end
76
+ #
77
+ class Akismet
78
+ VERSION = '0.9'.freeze
79
+ API_VERSION = '1.1'.freeze
80
+
81
+ @@host = 'rest.akismet.com'
82
+ @@key = nil
83
+ @@blog = nil
84
+
85
+ class << self
86
+ def host=(host)
87
+ @@host = host
88
+ end
89
+
90
+ def key=(key)
91
+ @@key = key
92
+ end
93
+
94
+ def blog=(blog)
95
+ @@blog = blog
96
+ end
97
+
98
+ def valid_key?(key)
99
+ call('verify-key', :key => key) == "valid"
100
+ end
101
+
102
+ # Checks wether a comment is spam or not.
103
+ #
104
+ # Required attributes:
105
+ #
106
+ # - <code>:permalink</code>
107
+ # - <code>:comment_author</code>
108
+ # - <code>:comment_author_url</code>
109
+ # - <code>:comment_author_email</code>
110
+ # - <code>:comment_content</code>
111
+ #
112
+ # Those are also required, but will be extracted from the request object:
113
+ #
114
+ # - <code>:user_ip</code>
115
+ # - <code>:user_agent</code>
116
+ # - <code>:referer</code>
117
+ # - plus any more relevant HTTP header.
118
+ #
119
+ def spam?(attributes, request = nil)
120
+ call('comment-check', attributes, request) == "true"
121
+ end
122
+
123
+ # Returns true unless comment is spam. Accepts the same arguments than spam.
124
+ def ham?(attributes, request = nil)
125
+ call('comment-check', attributes, request) == "false"
126
+ end
127
+
128
+ # Submits a spam comment to Akismet that hadn't been recognized
129
+ # as spam. Takes the same attributes than spam.
130
+ def submit_spam(attributes)
131
+ call('submit-spam', attributes)
132
+ end
133
+
134
+ # Submits a false-positive comment as non-spam to Akismet.
135
+ # Takes the same attributes than spam.
136
+ def submit_ham(attributes)
137
+ call('submit-ham', attributes)
138
+ end
139
+
140
+ private
141
+ def call(command, attributes, request = nil)
142
+ new(command, attributes, request).call
143
+ end
144
+ end
145
+
146
+ def initialize(command, attributes, request = nil) # :nodoc:
147
+ @command = command
148
+ @attributes = attributes
149
+ @request = request
150
+ end
151
+
152
+ def call # :nodoc:
153
+ http = Net::HTTP.new(http_host, 80)
154
+ http.post(http_path, post_attributes, http_headers).body
155
+ end
156
+
157
+ private
158
+ def attributes
159
+ @attributes[:blog] ||= @@blog
160
+
161
+ unless @command == 'verify-key'
162
+ @attributes[:comment_type] ||= 'comment'
163
+
164
+ unless @request.nil?
165
+ @attributes[:user_ip] ||= @request.remote_ip
166
+ @attributes[:user_agent] ||= @request.headers["User-Agent"]
167
+ @attributes[:referrer] ||= @request.headers["Http-Referer"]
168
+ end
169
+ end
170
+
171
+ @attributes
172
+ end
173
+
174
+ def post_attributes
175
+ post = attributes.map { |k,v| "#{k}=#{v}" }.join("&")
176
+ URI.escape(post)
177
+ end
178
+
179
+ def http_headers
180
+ {
181
+ "User-Agent" => "RubyAkismet/#{VERSION} | Akismet/#{API_VERSION}",
182
+ "Content-Type" => "application/x-www-form-urlencoded"
183
+ }
184
+ end
185
+
186
+ def http_host
187
+ unless @command == 'verify-key'
188
+ "#{@@key}.#{@@host}"
189
+ else
190
+ "#{@@host}"
191
+ end
192
+ end
193
+
194
+ def http_path
195
+ "/#{API_VERSION}/#{@command}"
196
+ end
197
+ end
@@ -0,0 +1,59 @@
1
+ require 'test/unit'
2
+ require File.expand_path("../../lib/akismet.rb", __FILE__)
3
+
4
+ class AkismetTest < Test::Unit::TestCase
5
+ def setup
6
+ Akismet.host = 'api.antispam.typepad.com'
7
+ Akismet.key = '123456789'
8
+ Akismet.blog = 'http://www.example.com/'
9
+ end
10
+
11
+ def valid_attributes
12
+ {
13
+ :user_ip => '127.0.0.1',
14
+ :user_agent => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3',
15
+ :referrer => 'http://www.example.com/posts',
16
+ :permalink => 'http://www.example.com/posts/1',
17
+ :comment_author => 'Julien Portalier',
18
+ :comment_author_url => 'http://ysbaddaden.wordpress.com/',
19
+ :comment_author_email => 'julien@example.com',
20
+ :comment_content => 'this is a normal comment',
21
+ }
22
+ end
23
+
24
+ def invalid_attributes
25
+ invalid = valid_attributes.dup
26
+ invalid[:comment_author] = 'viagra-test-123'
27
+ invalid
28
+ end
29
+
30
+ def test_valid_key
31
+ assert Akismet.valid_key?('123456789')
32
+ end
33
+
34
+ # def test_invalid_key
35
+ # assert !Akismet.valid_key?('abc123')
36
+ # end
37
+
38
+ def test_spam
39
+ assert Akismet.spam?(invalid_attributes)
40
+ end
41
+
42
+ def test_not_spam
43
+ assert !Akismet.spam?(valid_attributes)
44
+ end
45
+
46
+ def test_ham
47
+ assert Akismet.ham?(valid_attributes)
48
+ end
49
+
50
+ def test_not_ham
51
+ assert !Akismet.ham?(invalid_attributes)
52
+ end
53
+
54
+ def test_submit_spam
55
+ end
56
+
57
+ def test_submit_ham
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-akismet
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 9
9
+ version: "0.9"
10
+ platform: ruby
11
+ authors:
12
+ - Julien Portalier
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-22 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Akismet is basically a big machine that sucks up all the data it possibly can, looks for patterns, and learns from its mistakes. Thus far it has been highly effective at stopping spam and adapting to new techniques and attempts to evade it, and time will tell how it stands up.
22
+ email: ysbaddaden@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.rdoc
29
+ files:
30
+ - README.rdoc
31
+ - lib/akismet.rb
32
+ - test/akismet_test.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/ysbaddaden/ruby-akismet
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.7
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Ruby library for the Akismet service.
67
+ test_files:
68
+ - test/akismet_test.rb