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 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
@@ -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 => 'your_akismet_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
- 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'
16
+ module MerbAkismet
17
+ VERSION = '0.0.3'
24
18
 
25
- def self.config
26
- Merb::Plugins.config[:merb_akismet]
19
+ def self.config
20
+ Merb::Plugins.config[:merb_akismet]
21
+ end
27
22
  end
28
- end
23
+ require File.join(File.dirname(__FILE__), 'merb_akismet', 'request')
24
+ require File.join(File.dirname(__FILE__), 'merb_akismet', 'mixin')
29
25
 
30
- require File.join(File.dirname(__FILE__), 'merb_akismet', 'request')
31
- require File.join(File.dirname(__FILE__), 'merb_akismet', 'mixin')
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
- class Merb::AbstractController
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 "Verified and A-OK!"
16
+ puts "Akismet key verified and A-OK!"
17
17
  else
18
- puts "Invalid Key. To get a key, register at http://www.wordpress.com/"
18
+ puts "Invalid key. To get a key, register at http://www.wordpress.com/"
19
19
  end
20
20
  end
21
21
  end
@@ -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 to Akismet
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
- 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")
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
@@ -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 verify keys that are valid' do
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
@@ -1,3 +1,3 @@
1
1
  ---
2
2
  :blog: http://test.blog.museum/
3
- :key: Replace this with a valid key
3
+ :key: _____
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.2
7
- date: 2007-10-27 00:00:00 +02:00
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