Akismet.dart

Prevent comment spam using Akismet service, in Dart.

Features

  • Key Verification: checks an Akismet API key and gets a value indicating whether it is valid.
  • Comment Check: checks a comment and gets a value indicating whether it is spam.
  • Submit Spam: submits a comment that was not marked as spam but should have been.
  • Submit Ham: submits a comment that was incorrectly marked as spam but should not have been.

Documentation

Installing via Pub

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  akismet: any

2. Install it

If you're using the Dart Editor, choose:

Menu > Tools > Pub Get

Or if you want to install from the command line, run:

$ pub get

3. Import it

Now in your Dart code, you can use:

import 'package:akismet/html.dart'; // In browser applications.
import 'package:akismet/io.dart'; // In console applications.

Usage

Key Verification

var client = new Client('YourAPIKey', Uri.parse('http://your.blog.url'));
client.verifyKey().then((isValid) =>
  print(isValid ? 'Your API key is valid.' : 'Your API key is invalid.')
);

Comment Check

var author = new Author('An author')..ipAddress='127.0.0.1';
var comment = new Comment('A comment.', author);
client.checkComment(comment).then((isSpam) =>
  print(isSpam ? 'The comment is marked as spam.' : 'The comment is marked as ham.')
);

Submit Spam/Ham

client.submitSpam(comment).then((_) => print('Spam submitted.'));
client.submitHam(comment).then((_) => print('Ham submitted.'));

Implementations

Client

The Akismet client comes in two flavors: a first one based on dart:io for server/console applications, and a second one based on dart:html for client/browser applications.

Their usage is the same, but the HTML client is limited by security restrictions in a browser context. Unfortunately, the Akismet service does not support CORS headers. So, the HTML client can't be used directly with the official service.

Server

To be able to use the HTML client, we must rely on a proxy server adding CORS headers to service responses.

This is why a server implementation is provided with this package. To facilitate its usage, a command line interface is available in the bin folder.

From a command prompt, run the cli.dart script:

$ dart bin/cli.dart --help

Starts an Akismet.dart server.

Usage:
    cli.dart [options]

Options:
-h, --help        output usage information
-V, --version     output the version number
-a, --address     address that the server should run on
                  (defaults to "0.0.0.0")

-p, --port        port that the server should run on
                  (defaults to "3000")

-r, --redirect    the URL to redirect when a request is unhandled
    --silent      silence the log output from the server

Unit Tests

To test the server/console implementation, run the test/io_test.dart script from a command prompt:

$ dart test/io_test.dart <YourAPIKey> [http://your.blog.url]

To test the client/browser implementation, launch a server instance, and points your browser to this link: Unit Tests of HTML Client

License

Akismet.dart is distributed under the MIT License.

Libraries

akismet.core

Base implementation of the package. @module core

akismet.html

dart:html implementation of the package. Import this module in browser applications. @module html

akismet.io

dart:io implementation of the package. Import this module in console applications. @module io