merb_akismet 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +6 -0
- data/lib/merb_akismet.rb +16 -20
- data/lib/merb_akismet/merbtasks.rb +2 -2
- data/lib/merb_akismet/request.rb +12 -4
- data/specs/merb_akismet_spec.rb +15 -1
- data/specs/spec_config.yaml +1 -1
- metadata +2 -2
data/Rakefile
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake/gempackagetask'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
require 'merb'
|
3
5
|
require 'lib/merb_akismet'
|
4
6
|
|
5
7
|
PLUGIN = "merb_akismet"
|
@@ -26,6 +28,10 @@ spec = Gem::Specification.new do |s|
|
|
26
28
|
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
|
27
29
|
end
|
28
30
|
|
31
|
+
Spec::Rake::SpecTask.new do |t|
|
32
|
+
t.spec_files = FileList['specs/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
29
35
|
Rake::GemPackageTask.new(spec) do |pkg|
|
30
36
|
pkg.gem_spec = spec
|
31
37
|
end
|
data/lib/merb_akismet.rb
CHANGED
@@ -1,35 +1,31 @@
|
|
1
1
|
require 'net/http'
|
2
|
-
require 'merb'
|
3
2
|
|
4
|
-
# make sure we're running inside Merb
|
5
3
|
if defined?(Merb::Plugins)
|
6
4
|
Merb::Plugins.config[:merb_akismet] = {
|
7
5
|
:application => 'merb_akismet',
|
8
6
|
:blog => 'http://your_site_here',
|
9
7
|
:charset => 'utf-8',
|
10
|
-
:key => '
|
8
|
+
:key => '',
|
9
|
+
:timeout => 20,
|
10
|
+
|
11
|
+
# By default, everything is considered spam when unable to access Akismet
|
12
|
+
:fallback_answer => true
|
11
13
|
}
|
12
14
|
|
13
15
|
# :o lulz
|
14
|
-
|
15
|
-
|
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'
|
16
|
+
module MerbAkismet
|
17
|
+
VERSION = '0.0.3'
|
24
18
|
|
25
|
-
|
26
|
-
|
19
|
+
def self.config
|
20
|
+
Merb::Plugins.config[:merb_akismet]
|
21
|
+
end
|
27
22
|
end
|
28
|
-
|
23
|
+
require File.join(File.dirname(__FILE__), 'merb_akismet', 'request')
|
24
|
+
require File.join(File.dirname(__FILE__), 'merb_akismet', 'mixin')
|
29
25
|
|
30
|
-
|
31
|
-
|
26
|
+
config = File.join(MERB_ROOT, 'config', 'akismet.yml')
|
27
|
+
Merb::Plugins.config[:merb_akismet].merge!(YAML.load_file(config)) if File.exist?(config)
|
32
28
|
|
33
|
-
|
34
|
-
include MerbAkismet::Mixin
|
29
|
+
Merb::Plugins.add_rakefiles "merb_akismet/merbtasks"
|
30
|
+
class Merb::AbstractController; include MerbAkismet::Mixin end
|
35
31
|
end
|
@@ -13,9 +13,9 @@ namespace :akismet do
|
|
13
13
|
desc "Verify akismet API key"
|
14
14
|
task :verify do
|
15
15
|
if MerbAkismet::Request.has_valid_key?
|
16
|
-
puts "
|
16
|
+
puts "Akismet key verified and A-OK!"
|
17
17
|
else
|
18
|
-
puts "Invalid
|
18
|
+
puts "Invalid key. To get a key, register at http://www.wordpress.com/"
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/merb_akismet/request.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
module MerbAkismet
|
2
|
+
class KeyNotProvided < StandardError; end
|
3
|
+
|
2
4
|
class Request
|
3
5
|
HEADERS = {
|
4
6
|
'User-Agent' => "Merb/#{Merb::VERSION} | MerbAkismet/#{MerbAkismet::VERSION}",
|
@@ -8,7 +10,7 @@ module MerbAkismet
|
|
8
10
|
# True when Akismet determines that a comment is spam
|
9
11
|
attr_reader :response
|
10
12
|
|
11
|
-
# Makes an API call
|
13
|
+
# Makes an Akismet API call
|
12
14
|
#
|
13
15
|
# +function+ should be one of 'comment-check', 'submit-spam'
|
14
16
|
# or 'submit-ham'
|
@@ -32,9 +34,15 @@ module MerbAkismet
|
|
32
34
|
# env: Other environment variables
|
33
35
|
def initialize(function, args)
|
34
36
|
args[:blog] ||= MerbAkismet.config[:blog]
|
35
|
-
|
36
|
-
|
37
|
-
|
37
|
+
begin
|
38
|
+
raise KeyNotProvided if key.blank?
|
39
|
+
req = Net::HTTP.new("#{key}.rest.akismet.com", 80)
|
40
|
+
req.read_timeout = MerbAkismet.config[:timeout]
|
41
|
+
res, data = req.post("/1.1/#{function}", args.to_params, HEADERS)
|
42
|
+
@response = (data != "false")
|
43
|
+
rescue TimeoutError, KeyNotProvided
|
44
|
+
@response = MerbAkismet.config[:fallback_answer]
|
45
|
+
end
|
38
46
|
end
|
39
47
|
|
40
48
|
private
|
data/specs/merb_akismet_spec.rb
CHANGED
@@ -22,7 +22,7 @@ describe MerbAkismet::Request do
|
|
22
22
|
MerbAkismet::Request.should have_valid_key
|
23
23
|
end
|
24
24
|
|
25
|
-
it 'should only
|
25
|
+
it 'should only allow valid keys' do
|
26
26
|
MerbAkismet.config[:key] = 'invalid key'
|
27
27
|
MerbAkismet::Request.should_not have_valid_key
|
28
28
|
end
|
@@ -38,6 +38,20 @@ describe MerbAkismet::Request do
|
|
38
38
|
request = MerbAkismet::Request.new('comment-check', @comment)
|
39
39
|
request.response.should be_true
|
40
40
|
end
|
41
|
+
|
42
|
+
it 'should handle timeouts gracefully' do
|
43
|
+
MerbAkismet.config[:timeout] = 0.001 # Should be enough ;)
|
44
|
+
MerbAkismet.config[:fallback_answer] = "Timed out"
|
45
|
+
request = MerbAkismet::Request.new('comment-check', @comment)
|
46
|
+
request.response.should == "Timed out"
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should drop to the fallback answer if no api key is provided' do
|
50
|
+
MerbAkismet.config[:fallback_answer] = "Oh my"
|
51
|
+
MerbAkismet.config[:key] = ''
|
52
|
+
request = MerbAkismet::Request.new('comment-check', @comment)
|
53
|
+
request.response.should == "Oh my"
|
54
|
+
end
|
41
55
|
end
|
42
56
|
|
43
57
|
describe MerbAkismet::Mixin do
|
data/specs/spec_config.yaml
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: merb_akismet
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.0.3
|
7
|
+
date: 2007-11-14 00:00:00 +01:00
|
8
8
|
summary: Provides spam checking with Akismet for Merb apps
|
9
9
|
require_paths:
|
10
10
|
- lib
|