Skip to content

Commit

Permalink
Updated the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Jul 10, 2017
1 parent ec6146b commit 8cb806c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
This file contains highlights of what changes on each version of the [Akismet for JS](https://github.com/cedx/akismet.js) library.

## Version 9.0.0
- Breaking change: reverted the API to an [Observable](http://reactivex.io/intro.html)-based one.
- Breaking change: reverted the API of the `Client` class to an [Observable](http://reactivex.io/intro.html)-based one.
- Added new unit tests.
- Updated the package dependencies.

## Version 8.0.1
- Fixed a code generation bug.
Expand Down
52 changes: 22 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,60 +39,52 @@ client.verifyKey().subscribe(isValid =>
```javascript
const {Author, Comment} = require('@cedx/akismet');

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

let isSpam = await client.checkComment(comment);
console.log(isSpam ? 'The comment is marked as spam.' : 'The comment is marked as ham.');
}
let comment = new Comment(
new Author('127.0.0.1', 'Mozilla/5.0'),
'A comment.'
);

catch (error) {
console.log(`An error occurred: ${error}`);
}
client.checkComment(comment).subscribe(isSpam =>
console.log(isSpam ? 'The comment is marked as spam.' : 'The comment is marked as ham.')
);
```

### Submit spam/ham

```javascript
try {
await client.submitSpam(comment);
console.log('Spam submitted.');

await client.submitHam(comment);
console.log('Ham submitted.');
}
client.submitSpam(comment).subscribe(() =>
console.log('Spam submitted.')
);

catch (error) {
console.log(`An error occurred: ${error}`);
}
client.submitHam(comment).subscribe(() =>
console.log('Ham submitted.')
);
```

## Events
The `Client` class is an [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter).
During its life cycle, it emits these 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.

You can subscribe to them using the `on()` method:
These events are exposed as [Observable](http://reactivex.io/intro.html), you can subscribe to them using the `on<EventName>` properties:

```javascript
client.onRequest.subscribe(
request => console.log(`Client request: ${request.url}`)
client.onRequest.subscribe(request =>
console.log(`Client request: ${request.url}`)
);

client.onResponse.subscribe(
response => console.log(`Server response: ${response.statusCode}`)
client.onResponse.subscribe(response =>
console.log(`Server response: ${response.statusCode}`)
);
```

## Promise support
If you require it, an `Observable` can be converted to a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) by using the `toPromise()` method:
If you require it, an `Observable` can be converted to a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) by using the `toPromise` operator:

```javascript
import 'rxjs/add/operator/toPromise';

try {
let isSpam = await client.checkComment(comment).toPromise();
console.log(isSpam ? 'The comment is marked as spam.' : 'The comment is marked as ham.');
Expand Down
24 changes: 12 additions & 12 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class Client {
checkComment(comment) {
let baseURL = `${this.endPoint.protocol}//${this.apiKey}.${this.endPoint.host}${this.endPoint.pathname}`;
let endPoint = new URL('1.1/comment-check', baseURL);
return this._fetch(endPoint.href, comment.toJSON()).map(response => response == 'true');
return this._fetch(endPoint, comment.toJSON()).map(res => res == 'true');
}

/**
Expand All @@ -117,7 +117,7 @@ export class Client {
submitHam(comment) {
let baseURL = `${this.endPoint.protocol}//${this.apiKey}.${this.endPoint.host}${this.endPoint.pathname}`;
let endPoint = new URL('1.1/submit-ham', baseURL);
return this._fetch(endPoint.href, comment.toJSON());
return this._fetch(endPoint, comment.toJSON());
}

/**
Expand All @@ -128,7 +128,7 @@ export class Client {
submitSpam(comment) {
let baseURL = `${this.endPoint.protocol}//${this.apiKey}.${this.endPoint.host}${this.endPoint.pathname}`;
let endPoint = new URL('1.1/submit-spam', baseURL);
return this._fetch(endPoint.href, comment.toJSON());
return this._fetch(endPoint, comment.toJSON());
}

/**
Expand Down Expand Up @@ -159,12 +159,12 @@ export class Client {
*/
verifyKey() {
let endPoint = new URL('1.1/verify-key', this.endPoint);
return this._fetch(endPoint.href, {key: this.apiKey}).map(response => response == 'valid');
return this._fetch(endPoint, {key: this.apiKey}).map(res => res == 'valid');
}

/**
* Queries the service by posting the specified fields to a given end point, and returns the response as a string.
* @param {string} endPoint The URL of the end point to query.
* @param {URL} endPoint The URL of the end point to query.
* @param {object} fields The fields describing the query body.
* @return {Observable<string>} The response as string.
* @emits {superagent.Request} The "request" event.
Expand All @@ -176,17 +176,17 @@ export class Client {
let bodyFields = Object.assign(this.blog.toJSON(), fields);
if (this.isTest) bodyFields.is_test = '1';

let request = superagent.post(endPoint)
let req = superagent.post(endPoint.href)
.type('form')
.set('User-Agent', this.userAgent)
.send(bodyFields);

this._onRequest.next(request);
return Observable.fromPromise(request).map(response => {
this._onResponse.next(response);
if (!response.ok) return Observable.throw(new Error(`${response.status} ${response.statusText}`));
if (Client.DEBUG_HEADER in response.header) return Observable.throw(new Error(response.header[Client.DEBUG_HEADER]));
return response.text;
this._onRequest.next(req);
return Observable.fromPromise(req).map(res => {
this._onResponse.next(res);
if (!res.ok) return Observable.throw(new Error(`${res.status} ${res.statusText}`));
if (Client.DEBUG_HEADER in res.header) return Observable.throw(new Error(res.header[Client.DEBUG_HEADER]));
return res.text;
});
}
}

0 comments on commit 8cb806c

Please sign in to comment.