merb_akismet 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 raspberry lemon
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,6 @@
1
+ merb_akismet
2
+ =================
3
+
4
+ Spam checking with Akismet (http://akismet.com/) for Merb.
5
+
6
+ Remember to run rake akismet:settings
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'lib/merb_akismet'
4
+
5
+ PLUGIN = "merb_akismet"
6
+ NAME = "merb_akismet"
7
+ AUTHOR = "raspberry lemon"
8
+ EMAIL = "lemon@raspberry-style.net"
9
+ HOMEPAGE = "http://merb-plugins.rubyforge.org/merb_akismet/"
10
+ SUMMARY = "Provides spam checking with Akismet for Merb apps"
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = NAME
14
+ s.version = MerbAkismet::VERSION
15
+ s.platform = Gem::Platform::RUBY
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
18
+ s.summary = SUMMARY
19
+ s.description = s.summary
20
+ s.author = AUTHOR
21
+ s.email = EMAIL
22
+ s.homepage = HOMEPAGE
23
+ s.requirements << 'merb'
24
+ s.require_path = 'lib'
25
+ s.autorequire = PLUGIN
26
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |pkg|
30
+ pkg.gem_spec = spec
31
+ end
32
+
33
+ task :install => [:package] do
34
+ sh %{sudo gem install pkg/#{NAME}-#{MerbAkismet::VERSION}}
35
+ end
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ TODO:
2
+ Simpler configuration?
@@ -0,0 +1,35 @@
1
+ require 'net/http'
2
+ require 'merb'
3
+
4
+ # make sure we're running inside Merb
5
+ if defined?(Merb::Plugins)
6
+ Merb::Plugins.config[:merb_akismet] = {
7
+ :application => 'merb_akismet',
8
+ :blog => 'http://your_site_here',
9
+ :charset => 'utf-8',
10
+ :key => 'your_akismet_key'
11
+ }
12
+
13
+ # :o lulz
14
+ file = File.join(MERB_ROOT, 'config', 'akismet.yml')
15
+ if File.exist?(file)
16
+ Merb::Plugins.config[:merb_akismet].merge!(YAML.load_file(file))
17
+ end
18
+
19
+ Merb::Plugins.add_rakefiles "merb_akismet/merbtasks"
20
+ end
21
+
22
+ module MerbAkismet
23
+ VERSION = '0.0.2'
24
+
25
+ def self.config
26
+ Merb::Plugins.config[:merb_akismet]
27
+ end
28
+ end
29
+
30
+ require File.join(File.dirname(__FILE__), 'merb_akismet', 'request')
31
+ require File.join(File.dirname(__FILE__), 'merb_akismet', 'mixin')
32
+
33
+ class Merb::AbstractController
34
+ include MerbAkismet::Mixin
35
+ end
@@ -0,0 +1,21 @@
1
+ namespace :akismet do
2
+ desc "Generate akismet.yml"
3
+ task :setup do
4
+ file = File.join(MERB_ROOT, 'config', 'akismet.yml')
5
+ if not File.exists?(file)
6
+ File.open(file, 'w') { |f| f << MerbAkismet.config.to_yaml }
7
+ puts "Please edit config/akismet.yml"
8
+ else
9
+ puts "Configuration file already exists"
10
+ end
11
+ end
12
+
13
+ desc "Verify akismet API key"
14
+ task :verify do
15
+ if MerbAkismet::Request.has_valid_key?
16
+ puts "Verified and A-OK!"
17
+ else
18
+ puts "Invalid Key. To get a key, register at http://www.wordpress.com/"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ module MerbAkismet
2
+ module Mixin
3
+ # Returns true if +data+ is spam.
4
+ # +data+ can be a hash with parameters described in MerbAkismet::Request, or
5
+ # an arbitrary object which responds to #content, #author, #email, #url and #type
6
+ def spam?(data)
7
+ akismet_request('comment-check', data)
8
+ end
9
+
10
+ # When Akismet makes a mistake, we tell them about it
11
+ def submit_spam(data)
12
+ akismet_request('submit-spam', data)
13
+ end
14
+
15
+ # Marks a false positive and reports to Akismet
16
+ def submit_ham(data)
17
+ akismet_request('submit-ham', data)
18
+ end
19
+
20
+ private
21
+ # Extracts information from data and sends an Akismet request
22
+ def akismet_request(function, data)
23
+ data = {
24
+ :comment_content => data.content,
25
+ :comment_type => data.type,
26
+ :comment_author => data.author,
27
+ :comment_author_email => data.email,
28
+ :comment_author_url => data.url
29
+ } if data.respond_to? :content
30
+ data[:user_ip] ||= request.remote_ip
31
+ data[:user_agent] ||= request.user_agent
32
+ data[:referrer] ||= request.referer
33
+ MerbAkismet::Request.new(function, data).response
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,57 @@
1
+ module MerbAkismet
2
+ class Request
3
+ HEADERS = {
4
+ 'User-Agent' => "Merb/#{Merb::VERSION} | MerbAkismet/#{MerbAkismet::VERSION}",
5
+ 'Content-Type' => "application/x-www-form-urlencoded; charset=#{MerbAkismet.config[:charset]}"
6
+ }
7
+
8
+ # True when Akismet determines that a comment is spam
9
+ attr_reader :response
10
+
11
+ # Makes an API call to Akismet
12
+ #
13
+ # +function+ should be one of 'comment-check', 'submit-spam'
14
+ # or 'submit-ham'
15
+ #
16
+ # +args+ should be a hash containing all the extra info Akismet wants.
17
+ #
18
+ # Required arguments
19
+ # blog: The front page or home URL of the instance making the request.
20
+ # If you've set this in the options, it can be left out
21
+ # user_ip: IP address of the comment submitter.
22
+ # user_agent: User agent information
23
+ #
24
+ # Optional (but recommended!) arguments
25
+ # referrer: HTTP_REFERER
26
+ # permalink: The permanent location of the entry the comment was submitted to.
27
+ # comment_type: May be blank, comment, trackback, pingback, or a made up value like "registration".
28
+ # comment_author: Submitted name with the comment
29
+ # comment_author_email: Submitted email address
30
+ # comment_author_url: Commenter URL
31
+ # comment_content: The content that was submitted.
32
+ # env: Other environment variables
33
+ def initialize(function, args)
34
+ args[:blog] ||= MerbAkismet.config[:blog]
35
+ req = Net::HTTP.new("#{key}.rest.akismet.com", 80)
36
+ res, data = req.post("/1.1/#{function}", args.to_params, HEADERS)
37
+ @response = (data != "false")
38
+ end
39
+
40
+ private
41
+ def key
42
+ MerbAkismet.config[:key]
43
+ end
44
+
45
+ class << self
46
+ def has_valid_key?
47
+ req = Net::HTTP.new('rest.akismet.com', 80)
48
+ qs = {
49
+ :key => MerbAkismet.config[:key],
50
+ :blog => MerbAkismet.config[:blog]
51
+ }.to_params
52
+ res, data = req.post('/1.1/verify-key', qs, HEADERS)
53
+ data == "valid"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,45 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe MerbAkismet do
4
+ it 'should use the Merb::Plugins.config hash for its configuration' do
5
+ MerbAkismet.config.should == Merb::Plugins.config[:merb_akismet]
6
+ end
7
+ end
8
+
9
+ describe MerbAkismet::Request do
10
+ before(:each) do
11
+ setup_config
12
+ @comment = {
13
+ :user_ip => '58.120.49.28',
14
+ :user_agent => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10',
15
+ :comment_author => 'Mr. Bell',
16
+ :comment_content => 'This is a really interesting place. I love your blaahg.',
17
+ :referrer => 'http://127.0.0.1/'
18
+ }
19
+ end
20
+
21
+ it 'should be able to verify keys' do
22
+ MerbAkismet::Request.should have_valid_key
23
+ end
24
+
25
+ it 'should only verify keys that are valid' do
26
+ MerbAkismet.config[:key] = 'invalid key'
27
+ MerbAkismet::Request.should_not have_valid_key
28
+ end
29
+
30
+ it 'should allow harmless comments' do
31
+ request = MerbAkismet::Request.new('comment-check', @comment)
32
+ request.response.should be_false
33
+ end
34
+
35
+ # A request with 'viagra-test-123' as the author is always considered spam
36
+ it 'should identify spam' do
37
+ @comment[:comment_author] = "viagra-test-123"
38
+ request = MerbAkismet::Request.new('comment-check', @comment)
39
+ request.response.should be_true
40
+ end
41
+ end
42
+
43
+ describe MerbAkismet::Mixin do
44
+ Merb::AbstractController.new.should respond_to(:spam?)
45
+ end
@@ -0,0 +1,3 @@
1
+ ---
2
+ :blog: http://test.blog.museum/
3
+ :key: Replace this with a valid key
@@ -0,0 +1,11 @@
1
+ $TESTING=true
2
+ $:.push File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'spec'
5
+ require 'merb'
6
+ require 'merb_akismet'
7
+
8
+ def setup_config
9
+ yaml = YAML.load_file(File.join(File.dirname(__FILE__), 'spec_config.yaml'))
10
+ MerbAkismet.config.merge!(yaml)
11
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: merb_akismet
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.2
7
+ date: 2007-10-27 00:00:00 +02:00
8
+ summary: Provides spam checking with Akismet for Merb apps
9
+ require_paths:
10
+ - lib
11
+ email: lemon@raspberry-style.net
12
+ homepage: http://merb-plugins.rubyforge.org/merb_akismet/
13
+ rubyforge_project:
14
+ description: Provides spam checking with Akismet for Merb apps
15
+ autorequire: merb_akismet
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - raspberry lemon
31
+ files:
32
+ - LICENSE
33
+ - README
34
+ - Rakefile
35
+ - TODO
36
+ - lib/merb_akismet
37
+ - lib/merb_akismet/merbtasks.rb
38
+ - lib/merb_akismet/mixin.rb
39
+ - lib/merb_akismet/request.rb
40
+ - lib/merb_akismet.rb
41
+ - specs/merb_akismet_spec.rb
42
+ - specs/spec_config.yaml
43
+ - specs/spec_helper.rb
44
+ test_files: []
45
+
46
+ rdoc_options: []
47
+
48
+ extra_rdoc_files:
49
+ - README
50
+ - LICENSE
51
+ - TODO
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ requirements:
57
+ - merb
58
+ dependencies: []
59
+