aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValentin Popov <info@valentineus.link>2017-10-09 22:47:44 +0300
committerValentin Popov <info@valentineus.link>2017-10-09 22:47:44 +0300
commit1b31b0beec9d3011c444c70ec11f4da134570cb0 (patch)
treedadba90ae9381d494b725ebaaa486ac2c2fdd0b6
parent38d1a0eab11062da2d900d8d0fac787688d359ea (diff)
downloadiii-client-1b31b0beec9d3011c444c70ec11f4da134570cb0.tar.xz
iii-client-1b31b0beec9d3011c444c70ec11f4da134570cb0.zip
General code refactoring
-rw-r--r--CHANGELOG.md4
-rw-r--r--package.json2
-rw-r--r--src/index.js67
-rw-r--r--src/test.js52
4 files changed, 55 insertions, 70 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b46ba56..df04813 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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.
diff --git a/package.json b/package.json
index 94f2825..99352b2 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/src/index.js b/src/index.js
index 869d2de..29ee2be 100644
--- a/src/index.js
+++ b/src/index.js
@@ -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 query = {
- path: `/api/2.0/json/Chat.init/${uuid}`,
- hostname: 'iii.ru',
- method: 'GET',
- 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();
+ var pkg = createPackage(uuid, null);
+ forward(pkg, 'init', callback);
}
/**
- * @param {String} cuid - Session ID
- * @param {String} text - Send messages
+ * @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(cuid, text, callback) {
- if (!isVerification(cuid)) {
- throw new Error('\'cuid\' invalid variable.');
+function send(uuid, text, callback) {
+ if (!isVerification(uuid)) {
+ throw new Error('The variable \'uuid\' is not valid.');
}
if (!isString(text)) {
- throw new Error('\'text\' invalid variable.');
+ 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.request',
+ path: `/api/2.0/json/Chat.${path}`,
hostname: 'iii.ru',
method: 'POST',
- port: 80,
+ port: 80
};
var request = http.request(query, (response) => {
var json = null;
+
response.on('data', (raw) => {
json = decryptJSON(raw);
});
+
response.on('end', () => {
- callback(json.result);
+ callback(json);
});
});
+
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);
}
diff --git a/src/test.js b/src/test.js
index 1c34e5f..56fd03e 100644
--- a/src/test.js
+++ b/src/test.js
@@ -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 = () => {
- connect(uuid, (request) => {
- if (!typeof request === 'object' && value++ < 5) {
- test();
- }
- else {
- assert.isObject(request);
- done();
- }
- });
- }
-
- test();
+ it('connect()', (done) => {
+ connect(uuid, (request) => {
+ assert.isObject(request);
+ done();
+ });
});
- it('send():', (done) => {
- var value = 0;
- var test = () => {
- send(uuid, text, (request) => {
- if (!typeof request === 'object' && value++ < 5) {
- test();
- }
- else {
- assert.isObject(request);
- done();
- }
- });
- }
-
- test();
+ it('send()', (done) => {
+ send(uuid, text, (request) => {
+ assert.isObject(request);
+ done();
+ });
});
}); \ No newline at end of file