Skip to content

Commit

Permalink
Added addReaction method and more test
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphenslee committed Oct 14, 2015
1 parent fba36d6 commit 8e5148d
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 77 deletions.
120 changes: 83 additions & 37 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ util.inherits(SmartSlack, EventEmitter)
SmartSlack.prototype.authTest = function (callback) {
this._apiCall('auth.test', null, function test(data) {
if (data) {
if (data.ok) {
callback(data);
} else {
if (callback) {
callback(data);
}
}
Expand All @@ -92,17 +90,38 @@ SmartSlack.prototype.authTest = function (callback) {
* @return {object} passed args
*/
SmartSlack.prototype.apiTest = function (params, callback) {

this._apiCall('api.test', params, function test(data) {
if (data) {
if (data.ok) {
callback(data);
} else {
if (callback) {
callback(data);
}
}
})
}

/**
* Add a reaction to a message
* @param emojiName i.e. thumbsup
* @returns {object} JSON response
*/
SmartSlack.prototype.addReaction = function (emojiName, channel, timestamp, callback) {

if (emojiName && channel && timestamp && typeof emojiName === 'string'
&& typeof channel === 'string' && typeof timestamp == 'string') {

this._apiCall('reactions.add', { name: emojiName,
channel: channel,
timestamp: timestamp}, function (data) {
if (callback) {
callback(data);
}
});
} else {
throw new Error(errors.missing_required_arg);
};
};

/**
* Connect a WebSocket
*/
Expand Down Expand Up @@ -155,6 +174,29 @@ SmartSlack.prototype.getActiveChannels = function (callback) {
});
}

/**
* Gets group list
* @param {function} callback
* @return {array} the group list
*/
SmartSlack.prototype.getActiveGroups = function (callback) {
var _this = this;
this._apiCall('groups.list', { "exclude_archived": "1" }, function (data) {

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

/**
* Gets a channel by name
* @return {object} channel object
Expand All @@ -180,6 +222,23 @@ SmartSlack.prototype.getChannelById = function (id) {
}
}

/**
* Gets group list
* @param {function} callback
* @return {array} the group list
*/
SmartSlack.prototype.getLastChannelMessage = function (channel, callback) {
if (channel && typeof channel === 'string') {
this._apiCall('channels.history', { channel: channel, count: 1 }, function (data) {
if (callback) {
callback(data);
}
});
} else {
throw new Error(errors.missing_required_arg);
}
}

/**
* Gets a group by id
* @return {object} group object
Expand All @@ -204,29 +263,6 @@ SmartSlack.prototype.getGroupByName = function (name) {
}
}

/**
* Gets group list
* @param {function} callback
* @return {array} the group list
*/
SmartSlack.prototype.getActiveGroups = function (callback) {
var _this = this;
this._apiCall('groups.list', { "exclude_archived": "1" }, function (data) {

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

/**
* Gets a user by name
* @return {object} user object
Expand Down Expand Up @@ -352,12 +388,14 @@ SmartSlack.prototype.postMessage = function (channelId, text, params, callback)
params = _.extend({
channel: channelId,
text: text,
username: this.name,
as_user: true
username: this.name
}, params || {});

this._apiCall('chat.postMessage', params, function (data) {
callback(data);
if (callback) {
callback(data);
}

});
} else {
throw new Error(errors.missing_required_arg);
Expand All @@ -367,7 +405,7 @@ SmartSlack.prototype.postMessage = function (channelId, text, params, callback)
/**
* Sends a message via the RTM socket
*/
SmartSlack.prototype.sendSocket = function (channel, text) {
SmartSlack.prototype.send = function (channel, text) {
var msg;
var data;
if (channel && text) {
Expand All @@ -390,7 +428,10 @@ SmartSlack.prototype.sendSocket = function (channel, text) {
SmartSlack.prototype.setPresence = function (presence,callback) {
if (presence && presence === 'away' || presence === 'auto') {
this._apiCall('users.setPresence', { "presence": presence },function(data) {
callback(data);
if (callback) {
callback(data);
}

})
} else {
throw new Error(errors.missing_required_arg);
Expand Down Expand Up @@ -451,7 +492,7 @@ SmartSlack.prototype._reconnect = function () {
}

/**
* Makes a methos call to the Slack API
* Makes a method call to the Slack API
*/
SmartSlack.prototype._apiCall = function (method, params, callback) {

Expand Down Expand Up @@ -486,9 +527,14 @@ SmartSlack.prototype._apiCall = function (method, params, callback) {
if (callback) {
if (res.statusCode === 200) {
value = JSON.parse(output)
callback(value);
if (callback){
callback(value);
}

} else {
callback({ 'ok': false, 'error': 'API response: ' + res.statusCode })
if (callback) {
callback({ 'ok': false, 'error': 'API response: ' + res.statusCode })
}
}
}

Expand Down
120 changes: 80 additions & 40 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,37 @@ describe('SmartSlack', function () {
});

});

describe('#addReaction', function () {

var slackClient = new SmartSlack(mockopts);

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

it('should check for a valid arguments', function(done){
expect(function () {
slackClient.addReaction();
}).to.throw('Error missing or invalid required argument');
done();
})

it('should return api response', function (done) {

var scope = nock('https://slack.com')
.post('/api/reactions.add')
.reply(200,{ok: true});

slackClient.addReaction('emoji','channel','timestamp', function (data) {
expect(data).to.be.an('object');
expect(data.ok).to.equal(true);
done();
});
});

});

describe('#getActiveChannels', function () {

Expand Down Expand Up @@ -147,6 +178,7 @@ describe('SmartSlack', function () {

});


describe('#getActiveGroups', function () {

var response = {
Expand Down Expand Up @@ -193,21 +225,6 @@ describe('SmartSlack', function () {
{
"id": "C024BE91L",
"name": "fun",
"created": 1360782804,
"creator": "U024BE7LH",
"is_archived": false,
"is_member": false,
"num_members": 6,
"topic": {
"value": "Fun times",
"creator": "U024BE7LV",
"last_set": 1369677212
},
"purpose": {
"value": "This channel is for fun",
"creator": "U024BE7LH",
"last_set": 1360782804
}
},
]

Expand Down Expand Up @@ -240,21 +257,6 @@ describe('SmartSlack', function () {
{
"id": "C024BE91L",
"name": "fun",
"created": 1360782804,
"creator": "U024BE7LH",
"is_archived": false,
"is_member": false,
"num_members": 6,
"topic": {
"value": "Fun times",
"creator": "U024BE7LV",
"last_set": 1369677212
},
"purpose": {
"value": "This channel is for fun",
"creator": "U024BE7LH",
"last_set": 1360782804
}
},
]

Expand Down Expand Up @@ -285,16 +287,7 @@ describe('SmartSlack', function () {
slackClient.groups = [ { id: 'G0BC7NYJ0',
name: 'groupname',
is_group: true,
created: 1443296143,
creator: 'U0BC6D9V1',
is_archived: false,
is_mpim: false,
members: [ 'U0BC6D9V1', 'U0BN3JFH7' ],
topic: { value: '', creator: '', last_set: 0 },
purpose:
{ value: 'A private group',
creator: 'U0BC6D9V1',
last_set: 1443296143 } } ]
} ]

it('exists as public method on SmartSlack', function (done) {
expect(typeof slackClient.getGroupById).to.equal('function');
Expand All @@ -316,6 +309,53 @@ describe('SmartSlack', function () {
})
});

describe('#getLastChannelMessage', function () {

var slackClient = new SmartSlack(mockopts);

var response = {
"ok": true,
"latest": "1358547726.000003",
"messages": [
{
"type": "message",
"ts": "1358546515.000008",
"user": "U2147483896",
"text": "Hello"
}
],
"has_more": false
}


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

it('should check for a valid channel argument', function(done){
expect(function () {
slackClient.getLastChannelMessage();
}).to.throw('Error missing or invalid required argument');
done();
})

it('should return a channel object', function (done) {

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

var channel = slackClient.getLastChannelMessage('C024BE91L', function(data) {
expect(data).to.be.an('object');
expect(data.ok).to.equal(true);
expect(data.messages[0].text).to.equal('Hello');
});

done();
})
});

describe('#getUserByName', function () {

var slackClient = new SmartSlack(mockopts);
Expand Down

0 comments on commit 8e5148d

Please sign in to comment.