0
mirror of https://github.com/valentineus/iii-client.git synced 2025-04-28 01:41:25 +03:00

General code refactoring

This commit is contained in:
Valentin Popov 2017-10-09 23:47:44 +04:00
parent 38d1a0eab1
commit 1b31b0beec
4 changed files with 64 additions and 79 deletions

View File

@ -1,4 +1,8 @@
# Description of releases
## 3.2.0 (10-10-2017)
Features:
- General code refactoring.
## 3.1.0 (09-10-2017)
Features:
- Added support for older platforms.

View File

@ -1,6 +1,6 @@
{
"name": "iii-client",
"version": "3.1.0",
"version": "3.2.0",
"description": "Simple API for communicating with the bot of the \"iii.ru\" service.",
"homepage": "https://github.com/valentineus/iii-client",
"license": "MIT",

View File

@ -7,66 +7,63 @@ import http from 'http';
*/
function connect(uuid, callback) {
if (!isVerification(uuid)) {
throw new Error('\'uuid\' invalid variable.');
throw new Error('The variable \'uuid\' is not valid.');
}
var pkg = createPackage(uuid, null);
forward(pkg, 'init', callback);
}
/**
* @param {String} uuid - Session ID
* @param {String} text - The message you are sending
* @param {Function} callback - Function handler
* @description Sends a message to bot and returns a response.
*/
function send(uuid, text, callback) {
if (!isVerification(uuid)) {
throw new Error('The variable \'uuid\' is not valid.');
}
if (!isString(text)) {
throw new TypeError('\'text\' is not a string');
}
var pkg = createPackage(uuid, text);
forward(pkg, 'request', callback);
}
/**
* @param {String} pkg - The package to send
* @param {String} path - The final delivery address
* @param {Function} callback - Function handler
* @description Send the final packet to the server and return the response.
*/
function forward(pkg, path, callback) {
var query = {
path: `/api/2.0/json/Chat.init/${uuid}`,
path: `/api/2.0/json/Chat.${path}`,
hostname: 'iii.ru',
method: 'GET',
method: 'POST',
port: 80
};
var request = http.request(query, (response) => {
var json = null;
response.on('data', (raw) => {
json = decryptJSON(raw);
});
response.on('end', () => {
callback(json);
});
});
request.on('error', (error) => {
callback(error);
});
request.end();
}
/**
* @param {String} cuid - Session ID
* @param {String} text - Send messages
* @param {Function} callback - Function handler
* @description Sends a message to bot and returns a response.
*/
function send(cuid, text, callback) {
if (!isVerification(cuid)) {
throw new Error('\'cuid\' invalid variable.');
}
if (!isString(text)) {
throw new Error('\'text\' invalid variable.');
}
var query = {
path: '/api/2.0/json/Chat.request',
hostname: 'iii.ru',
method: 'POST',
port: 80,
};
var request = http.request(query, (response) => {
var json = null;
response.on('data', (raw) => {
json = decryptJSON(raw);
});
response.on('end', () => {
callback(json.result);
});
});
request.on('error', (error) => {
callback(error);
});
request.write(createPackage(cuid, text));
request.write(pkg);
request.end();
}
@ -119,15 +116,13 @@ function mergerString(data) {
}
/**
* @param {String} cuid - Session ID
* @param {String} uuid - Session ID
* @param {String} text - Source string
* @returns {String} Packed request
* @description Creates a package to send.
*/
function createPackage(cuid, text) {
var data = [];
data.push(cuid);
data.push(text.toString());
function createPackage(uuid, text) {
var data = [uuid, text];
var json = JSON.stringify(data);
return encrypt(json);
}

View File

@ -2,6 +2,7 @@ import { assert } from 'chai';
import generator from 'uuid';
import {
isVerification,
decryptJSON,
connect,
decrypt,
@ -14,51 +15,36 @@ describe('iii-client:', () => {
var text = 'Hello, World!';
var data = JSON.stringify({ text });
it('encrypt():', () => {
it('isVerification()', () => {
assert.isFalse(isVerification(text));
assert.isTrue(isVerification(uuid));
});
it('encrypt()', () => {
assert.notEqual(text, encrypt(text));
});
it('decrypt():', () => {
it('decrypt()', () => {
var encrypted = encrypt(text);
assert.equal(text, decrypt(encrypted));
});
it('decryptJSON():', () => {
it('decryptJSON()', () => {
var encrypted = encrypt(data);
assert.equal(data, decrypt(encrypted).toString());
});
it('connect():', (done) => {
var value = 0;
var test = () => {
it('connect()', (done) => {
connect(uuid, (request) => {
if (!typeof request === 'object' && value++ < 5) {
test();
}
else {
assert.isObject(request);
done();
}
});
}
test();
});
it('send():', (done) => {
var value = 0;
var test = () => {
it('send()', (done) => {
send(uuid, text, (request) => {
if (!typeof request === 'object' && value++ < 5) {
test();
}
else {
assert.isObject(request);
done();
}
});
}
test();
});
});
});