Skip to content

Commit

Permalink
Added getPresence method and test
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphenslee committed Oct 14, 2015
1 parent 8e5148d commit b750501
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Current Version](https://img.shields.io/badge/version-0.2.0-blue.svg)](https://github.com/philliphenslee/smartslack)

##Overview
***SmartSlack*** is a node.js module for [*Slack's*](https://slack.com) Real Time Messaging API.
***SmartSlack*** is a Node.JS module for [*Slack's*](https://slack.com) Real Time Messaging API.

## Installation

Expand Down
58 changes: 51 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* @copyright Phillip J. Henslee II 2015
* @module smartslack
*/

'use strict';

var EventEmitter = require('events').EventEmitter;
Expand Down Expand Up @@ -263,6 +262,22 @@ SmartSlack.prototype.getGroupByName = function (name) {
}
}

/**
* Gets the users presence
* @param {string} auto || away
*/
SmartSlack.prototype.getPresence = function (user,callback) {
if (user && typeof user === 'string') {
this._apiCall('users.getPresence', { user: user },function(data) {
if (callback) {
callback(data);
}
})
} else {
throw new Error(errors.missing_required_arg);
}
}

/**
* Gets a user by name
* @return {object} user object
Expand All @@ -288,6 +303,29 @@ SmartSlack.prototype.getUserById = function (id) {
}
}

/**
* Gets users list from API
* @param {funciton} callback
* @return {array} the user list
*/
SmartSlack.prototype.getUsersList = function (callback) {
var _this = this;
this._apiCall('users.list', function (data) {

if (data.ok) {
// Save the users list
_this.users = data.users;
if (callback) {
callback(data.users);
}
} else {
if (callback) {
callback(data);
}
}
});
}

/**
* Login to Slack
*/
Expand Down Expand Up @@ -324,7 +362,7 @@ SmartSlack.prototype.login = function () {
}

/**
* Handle slack RTM event messages
* Handle slack RTM event message
* @param The slack RTM message
*/
SmartSlack.prototype.onRtmEvent = function (message) {
Expand Down Expand Up @@ -443,19 +481,25 @@ SmartSlack.prototype.setPresence = function (presence,callback) {
* Calls login() if resolved
* @returns {boolean}
*/
SmartSlack.prototype._canResolve = function () {
SmartSlack.prototype._canResolve = function (callback) {
var _this = this;
this.log.debug('Attempting to resolve ' + this.hostname);

dns.resolve4(_this.hostname, function (err, addresses) {
if (!err) {
_this.log.debug('Resolved ' + _this.hostname + ' (' + addresses[0] + ')');
clearInterval(_this._reconnecting);
_this.login();
return true;
if (_this.reconnecting) {
clearInterval(_this._reconnecting);
_this.login();
}
if (callback) {
callback(true);
}
} else {
_this.log.debug('DNS resolution failed. Error Code: ' + err.code);
return false;
if (callback) {
callback(false);
}
}
});
}
Expand Down
109 changes: 90 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('SmartSlack', function () {
var slackClient = new SmartSlack(mockopts);
slackClient.should.be.an('object');
done();

})

it('should require an options object', function (done) {
Expand All @@ -34,7 +33,6 @@ describe('SmartSlack', function () {
}).to.throw('Error missing required argument object options');

done();

});

it('should respect its options', function (done) {
Expand All @@ -53,7 +51,6 @@ describe('SmartSlack', function () {
}).to.throw('Error invalid access token, please provide a valid token.');

done();

});
});

Expand All @@ -76,10 +73,9 @@ describe('SmartSlack', function () {
slackClient.authTest(function (data) {
expect(data).to.be.an('object');
expect(data.ok).to.equal(true);
done();
});
done();
});

});

describe('#apiTest', function () {
Expand All @@ -102,11 +98,10 @@ describe('SmartSlack', function () {

slackClient.apiTest(null, function (data) {
expect(data).to.be.an('object');
expect(data.args.foo).to.equal('bar');
done();
expect(data.args.foo).to.equal('bar');
});
done();
});

});

describe('#addReaction', function () {
Expand Down Expand Up @@ -134,10 +129,9 @@ describe('SmartSlack', function () {
slackClient.addReaction('emoji','channel','timestamp', function (data) {
expect(data).to.be.an('object');
expect(data.ok).to.equal(true);
done();
});
done();
});

});

describe('#getActiveChannels', function () {
Expand Down Expand Up @@ -171,9 +165,9 @@ describe('SmartSlack', function () {

slackClient.getActiveChannels(function (data) {
expect(data).to.be.an('array');
expect(data[0].name).to.equal('fun');
done();
expect(data[0].name).to.equal('fun');
});
done();
});

});
Expand Down Expand Up @@ -210,8 +204,8 @@ describe('SmartSlack', function () {
slackClient.getActiveGroups(function (data) {
expect(data).to.be.an('array');
expect(data[0].name).to.equal('special group');
done();
});
done();
});

});
Expand Down Expand Up @@ -356,6 +350,34 @@ describe('SmartSlack', function () {
})
});

describe('#getPresence', function () {

var slackClient = new SmartSlack(mockopts);

it('should require valid user {object} argument', function(done){

expect(function () {
slackClient.getPresence();
}).to.throw('Error missing or invalid required argument');
done();
})

it('should return API message response', function (done) {

var scope = nock('https://slack.com')
.post('/api/users.getPresence')
.reply(200, { ok: true, presence: 'active' });

slackClient.getPresence('away', function (data) {
expect(data).to.be.an('object');
expect(data.ok).to.equal(true);
expect(data.presence).to.equal('active');
});
done();
});

});

describe('#getUserByName', function () {

var slackClient = new SmartSlack(mockopts);
Expand Down Expand Up @@ -416,6 +438,43 @@ describe('SmartSlack', function () {
})
});

describe('#getUserList', function () {

var response = {
"ok": true,
"members": [
{
"id": "U023BECGF",
"name": "john",
"deleted": false,
"color": "9f69e7",
}
// ...
]
}

var slackClient = new SmartSlack(mockopts);

it('exists as a public method on SmartSlack', function (done) {
expect(typeof slackClient.getUsersList).to.equal('function');
done();
})

it('should return an array of active Slack users', function (done) {

var scope = nock('https://slack.com')
.post('/api/users.list')
.reply(200, response);

slackClient.getUsersList(function (data) {
expect(data).to.be.an('array');
expect(data[0].name).to.equal('john');
});
done();
});

});

describe('#login', function () {

response = {
Expand Down Expand Up @@ -489,8 +548,8 @@ describe('SmartSlack', function () {
expect(data).to.be.an('object');
expect(data.ok).to.equal(true);
expect(data.channel).to.equal('C024BE91L');
done();
});
done();
});

});
Expand Down Expand Up @@ -521,9 +580,8 @@ describe('SmartSlack', function () {
expect(data).to.be.an('object');
expect(data.ok).to.equal(true);
expect(data.channel.id).to.equal('D0BMZB9V3');
done();
});

done();
});

})
Expand Down Expand Up @@ -551,12 +609,25 @@ describe('SmartSlack', function () {
slackClient.setPresence('away', function (data) {
expect(data).to.be.an('object');
expect(data.ok).to.equal(true);
done();
});
done();
});

});

describe('#_canResolve', function () {

var slackClient = new SmartSlack(mockopts);

it('should return a boolean', function (done) {
slackClient._canResolve(function (value) {
expect(value).to.equal(true);
});
done();
})

})

describe('#_apiCall', function () {

var slackClient = new SmartSlack(mockopts);
Expand All @@ -573,9 +644,9 @@ describe('SmartSlack', function () {

slackClient.apiTest(null, function (data) {
expect(data).to.be.an('object');
expect(data.ok).to.equal(false);
done();
expect(data.ok).to.equal(false);
});
done();
});

});
Expand Down

0 comments on commit b750501

Please sign in to comment.