Akismet for Dart

Release License Coverage Build

Prevent comment spam using the 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.

Requirements

The latest Dart SDK and Pub versions. If you plan to play with the sources, you will also need the latest Grinder version.

Installing via Pub

1. Depend on it

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

dependencies:
  akismet: *

2. Install it

Install this package and its dependencies from a command prompt:

$ pub get

3. Import it

Now in your Dart code, you can use:

import 'package:akismet/akismet.dart';

Usage

Key verification

try {
  var client = new Client('your API key', 'http://your.blog.url');
  var isValid = await client.verifyKey();
  print(isValid ? 'Your API key is valid.' : 'Your API key is invalid.');
}

catch (error) {
  print('An error occurred: $error');
}

Comment check

try {
  var comment = new Comment(
    new Author('127.0.0.1', 'Mozilla/5.0'),
    'A comment.'
  );

  var isSpam = await client.checkComment(comment);
  print(isSpam ? 'The comment is marked as spam.' : 'The comment is marked as ham.');
}

catch (error) {
  print('An error occurred: $error');
}

Submit spam/ham

try {
  await client.submitSpam(comment);
  print('Spam submitted.');

  await client.submitHam(comment);
  print('Ham submitted.');
}

catch (error) {
  print('An error occurred: $error');
}

Events

The Client class triggers some events during its life cycle:

  • request : emitted every time a request is made to the remote service.
  • response : emitted every time a response is received from the remote service.

These events are exposed as Stream, you can listen to them using the on<EventName> properties:

client.onRequest.listen(
  (request) => print('Client request: ${request.url}')
);

client.onResponse.listen(
  (response) => print('Server response: ${response.statusCode}')
);

Unit tests

In order to run the tests, you must set the AKISMET_API_KEY environment variable to the value of your Akismet API key:

$ export AKISMET_API_KEY="<YourAPIKey>"

Then, you can run the test script from the command prompt:

$ pub run test

See also

License

Akismet for Dart is distributed under the Apache License, version 2.0.

Libraries

akismet

Prevent comment spam using the Akismet service.