0
mirror of https://github.com/valentineus/iii-client.git synced 2025-04-29 01:51:24 +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 # Description of releases
## 3.2.0 (10-10-2017)
Features:
- General code refactoring.
## 3.1.0 (09-10-2017) ## 3.1.0 (09-10-2017)
Features: Features:
- Added support for older platforms. - Added support for older platforms.

View File

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

View File

@ -7,66 +7,63 @@ import http from 'http';
*/ */
function connect(uuid, callback) { function connect(uuid, callback) {
if (!isVerification(uuid)) { 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 = { var query = {
path: `/api/2.0/json/Chat.init/${uuid}`, path: `/api/2.0/json/Chat.${path}`,
hostname: 'iii.ru', hostname: 'iii.ru',
method: 'GET', method: 'POST',
port: 80 port: 80
}; };
var request = http.request(query, (response) => { var request = http.request(query, (response) => {
var json = null; var json = null;
response.on('data', (raw) => { response.on('data', (raw) => {
json = decryptJSON(raw); json = decryptJSON(raw);
}); });
response.on('end', () => { response.on('end', () => {
callback(json); callback(json);
}); });
}); });
request.on('error', (error) => { request.on('error', (error) => {
callback(error); callback(error);
}); });
request.end();
}
/** request.write(pkg);
* @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.end(); request.end();
} }
@ -119,15 +116,13 @@ function mergerString(data) {
} }
/** /**
* @param {String} cuid - Session ID * @param {String} uuid - Session ID
* @param {String} text - Source string * @param {String} text - Source string
* @returns {String} Packed request * @returns {String} Packed request
* @description Creates a package to send. * @description Creates a package to send.
*/ */
function createPackage(cuid, text) { function createPackage(uuid, text) {
var data = []; var data = [uuid, text];
data.push(cuid);
data.push(text.toString());
var json = JSON.stringify(data); var json = JSON.stringify(data);
return encrypt(json); return encrypt(json);
} }

View File

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