Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

easy configuration for Akismet and TypePad

  • Loading branch information...
commit 73c1933f2ce54f4c55871abf16714ca8d2f3aa11 1 parent ae6a276
@balexand authored
Showing with 41 additions and 11 deletions.
  1. +15 −4 README.md
  2. +18 −5 lib/spamster.rb
  3. +8 −2 spec/spamster_spec.rb
View
19 README.md
@@ -18,14 +18,25 @@ And then execute:
### Configuration
-Start by signing up for an API key [here](https://akismet.com/signup/). Then configure Spamster like:
+First you'll need to sign up for an API key from [Akismet](https://akismet.com/signup/) or [TypePad AntiSpam](http://antispam.typepad.com/info/get-api-key.html). Then configure Spamster like:
```ruby
-Spamster.blog = "http://yoursite.com/"
-Spamster.key = "your-api-key"
+Spamster.use_akismet("your-api-key", "http://yoursite.com/")
```
-FIXME middleware
+or...
+
+```ruby
+Spamster.use_typepad("your-api-key", "http://yoursite.com/")
+```
+
+If you're building a Rack app (all Rails 3+ apps are Rack apps), then you'll probably want to use the optional Rack middleware so Spamster can automatically fill in the `referrer`, `user_agent`, and `user_ip` params. If you're using Rails then add the middleware like this:
+
+```ruby
+Rails.application.config.middleware.use Spamster::Rack::Middleware
+```
+
+If you're using Rails, then a suggested location to keep the above configuration is in an initializer file.
### Sanity check
View
23 lib/spamster.rb
@@ -8,12 +8,24 @@ module Spamster
autoload :Rack, 'spamster/rack/middleware'
class <<self
- attr_accessor :blog, :debug_output, :key, :request_params
+ attr_accessor :api_host, :blog, :debug_output, :key, :request_params
+
+ def use_akismet(key, blog)
+ self.api_host = "rest.akismet.com"
+ self.blog = blog
+ self.key = key
+ end
+
+ def use_typepad(key, blog)
+ self.api_host = "api.antispam.typepad.com"
+ self.blog = blog
+ self.key = key
+ end
# see http://akismet.com/development/api/#verify-key
def key_valid?
params = {:blog => blog, :key => key}
- response = perform_post("http://rest.akismet.com/1.1/verify-key", params)
+ response = perform_post("http://#{api_host}/1.1/verify-key", params)
response.body == 'valid'
end
@@ -34,8 +46,9 @@ def ham!(params)
private
def perform_post(url, params)
- raise "'Spamster.blog' must be set" unless blog.present?
- raise "'Spamster.key' must be set" unless key.present?
+ [:api_host, :blog, :key].each do |param|
+ raise "'Spamster.#{param}' must be set" unless send(param).present?
+ end
uri = URI(url)
@@ -59,7 +72,7 @@ def perform_spam_post(method, params = {})
raise "required param #{param.inspect} is missing" unless params[param].present?
end
- response = perform_post("http://#{key}.rest.akismet.com/1.1/#{method}", params)
+ response = perform_post("http://#{key}.#{api_host}/1.1/#{method}", params)
response.body
end
end
View
10 spec/spamster_spec.rb
@@ -2,8 +2,7 @@
describe Spamster do
before(:each) do
- Spamster.blog = "http://example.com/"
- Spamster.key = "123abc"
+ Spamster.use_akismet("123abc", "http://example.com/")
end
describe "perform_post" do
@@ -11,6 +10,13 @@
stub_request(:post, "http://123abc.rest.akismet.com/1.1/comment-check")
end
+ it "should raise exception if :api_host is not configured" do
+ Spamster.api_host = ""
+ expect do
+ Spamster.send(:perform_post, "http://123abc.rest.akismet.com/1.1/comment-check", {})
+ end.to raise_exception{ |e| e.message.should == "'Spamster.api_host' must be set" }
+ end
+
it "should raise exception if :blog is not configured" do
Spamster.blog = ""
expect do
Please sign in to comment.
Something went wrong with that request. Please try again.