Prevent comment spam using Akismet service, in Dart.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

Akismet for Dart

Runtime Release License Coverage Build

Prevent comment spam using the Akismet service, in Dart.

Documentation

Development

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.

Usage

Key verification

try {
  final client = Client('123YourAPIKey', Blog(Uri.https('www.yourblog.com', '/')));
  final isValid = await client.verifyKey();
  print(isValid ? 'The API key is valid' : 'The API key is invalid');
}

on ClientException catch (err) {
  print('An error occurred: ${err.message}');
}

Comment check

try {
  final comment = Comment(
    Author('127.0.0.1', 'Mozilla/5.0'),
    content: 'A user comment',
    date: DateTime.now()
  );

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

on ClientException catch (err) {
  print('An error occurred: ${err.message}');
}

Submit spam / ham

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

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

on ClientException catch (err) {
  print('An error occurred: ${err.message}');
}

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<RequestEvent>, you can listen to them using the on<EventName> properties:

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

client.onResponse.listen(
  (event) => print('Server response: ${event.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="<123YourAPIKey>"

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

pub run test

License

Akismet for Dart is distributed under the MIT License.