diff --git a/lib/cache.js b/lib/cache.js index 252eb7b..8686337 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -4,7 +4,7 @@ var errors = require('./errors'); var Cache = function Cache() { - if (Cache.caller != Cache.getInstance) { + if (Cache.caller !== Cache.getInstance) { throw new Error("This object cannot be instanciated"); } diff --git a/lib/cache.spec.js b/lib/cache.spec.js deleted file mode 100644 index f83fb82..0000000 --- a/lib/cache.spec.js +++ /dev/null @@ -1,66 +0,0 @@ -var chai = require('chai'); -var should = chai.should(); -var expect = chai.expect; -var nock = require('nock'); - -var Cache = require('./cache'); -var slackType = require('./slack/types'); - -describe('cache', function () { - - - - it('should be a singleton', function (done) { - var cache1 = require('./cache'); - var cache2 = require('./cache'); - expect(cache1).to.equal(cache2); - done(); - }); - - // function get(name, callback) - describe('#search', function () { - - var cache = Cache; - cache.data.users = [ - { - "id": "U0BC6D9V1", - "name": "john", - "deleted": false, - "status": null, - "color": "9f69e7", - "real_name": "John Doe", - "tz": "America/Chicago", - "tz_label": "Central Daylight Time", - "tz_offset": -18000, - "profile": { - "first_name": "John", - "last_name": "Doe", - "title": "Just a slack user", - "skype": "", - "phone": "111-111-111", - "real_name": "John Doe", - "real_name_normalized": "John Doe", - "email": "john@somedomain.com"} - }] - - - it('it should return a user by e-mail address', function (done) { - cache.search(slackType.USER,'john@somedomain.com', function name(err, result) { - expect(result.name).to.equal('john'); - }) - done(); - }); - it('it should return a user by name', function (done) { - cache.search(slackType.USER,'john', function name(err, result) { - expect(result.profile.email).to.equal('john@somedomain.com'); - }) - done(); - }); - it('it should return a user by user id', function (done) { - cache.search(slackType.USER,'U0BC6D9V1', function name(err, result) { - expect(result.profile.real_name).to.equal('John Doe'); - }) - done(); - }); - }); -}); \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 0dbde0b..f1a563e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -35,20 +35,19 @@ function SmartSlack(options) { var accessToken = options.token; // Need a valid Slack token to authenticate - if (!accessToken || typeof accessToken !== 'string' || - !accessToken.match(/^([a-z]{4})-([0-9]{11})-([0-9a-zA-Z]{24})$/)) { + if (!accessToken || typeof accessToken !== 'string' || !accessToken.match(/^([a-z]{4})-([0-9]{11})-([0-9a-zA-Z]{24})$/)) { throw new Error(errors.invalid_token); } // Save the cache data this.cache = Cache; this.cache.defaults(options); - this.cache.add({ hostname: 'slack.com' }); + this.cache.add({hostname: 'slack.com'}); // API references this.channels = slack.channels; this.chat = slack.chat; - this.emoji = slack.emoji + this.emoji = slack.emoji; this.groups = slack.groups; this.im = slack.im; this.reactions = slack.reactions; @@ -124,7 +123,6 @@ SmartSlack.prototype.postDirectMessage = function (user, text, args, callback) { }; - /** * Post a message via the API * @description Convenience function so you can call @@ -184,7 +182,8 @@ SmartSlack.prototype.sendToGroup = function (group, text, callback) { * @param {function} callback(err,result) */ SmartSlack.prototype.sendToUser = function (username, text, callback) { - callback = (_.isFunction(callback)) ? callback : function () { }; + callback = (_.isFunction(callback)) ? callback : function () { + }; if (!_.isString(username) && !_.isString(text)) { return callback(new Error(errors.missing_required_arg)); @@ -312,7 +311,9 @@ SmartSlack.prototype._onRtmEvent = function (message) { this._lastPong = _.now(); // Start pings every five seconds - this._pingInterval = setInterval(function () { _this._ping(); }, 5000); + this._pingInterval = setInterval(function () { + _this._ping(); + }, 5000); break; case 'message': @@ -320,10 +321,8 @@ SmartSlack.prototype._onRtmEvent = function (message) { uptime = this.getUptime(); if (message.text === 'naomi uptime') { - this._send(message.channel,uptime); + this._send(message.channel, uptime); } - - break; case 'pong': @@ -333,7 +332,7 @@ SmartSlack.prototype._onRtmEvent = function (message) { break; case 'presence_change': - _.find(this.cache.data.users, { id: message.user }).presence = message.presence; + _.find(this.cache.data.users, {id: message.user}).presence = message.presence; this.log.info('User presence changed, updating user presence...'); break; @@ -368,7 +367,7 @@ SmartSlack.prototype._onRtmEvent = function (message) { SmartSlack.prototype._ping = function () { var _this = this; var result; - var ping = { type: "ping" }; + var ping = {type: "ping"}; ping.id = _this._messageId; ping.time = _.now(); result = JSON.stringify(ping); @@ -391,7 +390,9 @@ SmartSlack.prototype._reconnect = function () { _this._startTime = null; // Attempt to resolve slack.com and call login when available - _this._reconnecting = setInterval(function () { _this._canResolve(); }, 5000); + _this._reconnecting = setInterval(function () { + _this._canResolve(); + }, 5000); _this.log.info('Connection lost, waiting to reconnect...'); }; @@ -437,7 +438,8 @@ SmartSlack.prototype._sendToType = function (slackType, typeName, text, callback var entityId; - callback = (_.isFunction(callback)) ? callback : function () { }; + callback = (_.isFunction(callback)) ? callback : function () { + }; switch (slackType) { case slack.types.CHANNEL: diff --git a/package.json b/package.json index cc46506..2ff02a8 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,8 @@ }, "scripts": { "test": "npm run test-unit", - "test-unit": "mocha -R spec $(find ./lib -name \"*.spec.js\" -not -path \"./node_modules/*\")", - "test-travis": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec $(find ./lib -name \"*.spec.js\" -not -path \"./node_modules/*\")", + "test-unit": "mocha -R spec $(find ./test -name \"*.spec.js\" -not -path \"./node_modules/*\")", + "test-travis": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec $(find ./test -name \"*.spec.js\" -not -path \"./node_modules/*\")", "travis": "npm test" } } diff --git a/lib/slack/api.spec.js b/test/api.spec.js similarity index 93% rename from lib/slack/api.spec.js rename to test/api.spec.js index 24f1819..088adbc 100644 --- a/lib/slack/api.spec.js +++ b/test/api.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var api = require('./api'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var api = require('./../lib/slack/api'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('api', function () { diff --git a/test/cache.spec.js b/test/cache.spec.js new file mode 100644 index 0000000..599a38e --- /dev/null +++ b/test/cache.spec.js @@ -0,0 +1,76 @@ +var chai = require('chai'); +var should = chai.should(); +var expect = chai.expect; +var nock = require('nock'); + +var cache = require('../lib/cache'); +var slackType = require('../lib/slack/types'); + + +describe('cache', function () { + + it('should be a singleton', function (done) { + var cache1 = require('./../lib/cache'); + var cache2 = require('./../lib/cache'); + expect(cache1).to.equal(cache2); + done(); + }); + + // function get(name, callback) + describe('#search', function () { + + before(function () { + + cache.data.users = [ + { + "id": "U0BC6D9V1", + "name": "john", + "real_name": "John Doe", + "profile": { + "first_name": "John", + "last_name": "Doe", + "title": "Just a slack user", + "real_name": "John Doe", + "real_name_normalized": "John Doe", + "email": "john@somedomain.com" + } + }]; + }); + + it('it should return a user by e-mail address', function (done) { + cache.data.users = [ + { + "id": "U0BC6D9V1", + "name": "john", + "real_name": "John Doe", + "profile": { + "first_name": "John", + "last_name": "Doe", + "title": "Just a slack user", + "real_name": "John Doe", + "real_name_normalized": "John Doe", + "email": "john@somedomain.com" + } + }]; + cache.search(slackType.USER, 'john@somedomain.com', function name(err, result) { + expect(result.name).to.equal('john'); + done(); + }); + + }); + + it('it should return a user by name', function (done) { + cache.search(slackType.USER, 'john', function name(err, result) { + expect(result.profile.email).to.equal('john@somedomain.com'); + }); + done(); + }); + + it('it should return a user by user id', function (done) { + cache.search(slackType.USER, 'U0BC6D9V1', function name(err, result) { + expect(result.profile.real_name).to.equal('John Doe'); + }); + done(); + }); + }); +}); \ No newline at end of file diff --git a/lib/slack/channels.spec.js b/test/channels.spec.js similarity index 98% rename from lib/slack/channels.spec.js rename to test/channels.spec.js index 3c3d95e..9f320d2 100644 --- a/lib/slack/channels.spec.js +++ b/test/channels.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var channels = require('./channels'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var channels = require('./../lib/slack/channels'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('channels', function () { diff --git a/lib/slack/chat.spec.js b/test/chat.spec.js similarity index 97% rename from lib/slack/chat.spec.js rename to test/chat.spec.js index 161b54f..a630bba 100644 --- a/lib/slack/chat.spec.js +++ b/test/chat.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var chat = require('./chat'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var chat = require('./../lib/slack/chat'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('chat', function () { diff --git a/lib/slack/emoji.spec.js b/test/emoji.spec.js similarity index 90% rename from lib/slack/emoji.spec.js rename to test/emoji.spec.js index e6c33a8..c9c3092 100644 --- a/lib/slack/emoji.spec.js +++ b/test/emoji.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var emoji = require('./emoji'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var emoji = require('./../lib/slack/emoji'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('emoji', function () { diff --git a/lib/slack/groups.spec.js b/test/groups.spec.js similarity index 97% rename from lib/slack/groups.spec.js rename to test/groups.spec.js index cae70a2..bd80190 100644 --- a/lib/slack/groups.spec.js +++ b/test/groups.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var groups = require('./groups'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var groups = require('./../lib/slack/groups'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('groups', function () { diff --git a/lib/slack/im.spec.js b/test/im.spec.js similarity index 97% rename from lib/slack/im.spec.js rename to test/im.spec.js index 8a3947c..eae9caf 100644 --- a/lib/slack/im.spec.js +++ b/test/im.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var im = require('./im'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var im = require('./../lib/slack/im'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('im', function () { diff --git a/lib/index.spec.js b/test/index.spec.js similarity index 99% rename from lib/index.spec.js rename to test/index.spec.js index fa45f71..4794d34 100644 --- a/lib/index.spec.js +++ b/test/index.spec.js @@ -3,7 +3,7 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var Cache = require('./cache'); +var Cache = require('./../lib/cache'); var SmartSlack = require('../lib/index.js'); describe('SmartSlack', function () { diff --git a/lib/slack/reactions.spec.js b/test/reactions.spec.js similarity index 97% rename from lib/slack/reactions.spec.js rename to test/reactions.spec.js index 2515124..4d55965 100644 --- a/lib/slack/reactions.spec.js +++ b/test/reactions.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var reactions = require('./reactions'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var reactions = require('./../lib/slack/reactions'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('reactions', function () { diff --git a/lib/slack/rtm.spec.js b/test/rtm.spec.js similarity index 94% rename from lib/slack/rtm.spec.js rename to test/rtm.spec.js index e627ff6..39ff931 100644 --- a/lib/slack/rtm.spec.js +++ b/test/rtm.spec.js @@ -3,8 +3,8 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var rtm = require('./rtm'); -var Cache = require('../cache'); +var rtm = require('./../lib/slack/rtm'); +var Cache = require('../lib/cache'); describe('rtm', function () { diff --git a/lib/slack/team.spec.js b/test/team.spec.js similarity index 90% rename from lib/slack/team.spec.js rename to test/team.spec.js index b57a01b..bc89821 100644 --- a/lib/slack/team.spec.js +++ b/test/team.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var team = require('./team'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var team = require('./../lib/slack/team'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('team', function () { diff --git a/lib/slack/test.spec.js b/test/test.spec.js similarity index 95% rename from lib/slack/test.spec.js rename to test/test.spec.js index ef9733c..2c8cb55 100644 --- a/lib/slack/test.spec.js +++ b/test/test.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var test = require('./test'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var test = require('./../lib/slack/test'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('test', function () { diff --git a/lib/slack/users.spec.js b/test/users.spec.js similarity index 98% rename from lib/slack/users.spec.js rename to test/users.spec.js index 3a8a829..ed93626 100644 --- a/lib/slack/users.spec.js +++ b/test/users.spec.js @@ -3,10 +3,10 @@ var should = chai.should(); var expect = chai.expect; var nock = require('nock'); -var users = require('./users'); -var errors = require('../errors'); -var Cache = require('../cache'); -var slackTypes = require('./types'); +var users = require('./../lib/slack/users'); +var errors = require('../lib/errors'); +var Cache = require('../lib/cache'); +var slackTypes = require('./../lib/slack/types'); describe('users', function () {