From 385ae26a5f0be2969399485d00134927f2871a02 Mon Sep 17 00:00:00 2001 From: Mike Helgeson Date: Wed, 10 Feb 2016 15:03:04 -0500 Subject: [PATCH] added `b9.user()` method --- README.md | 18 +++++++++++++----- index.js | 16 ++++++++++++++++ test.js | 6 ++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index da2cd4e..86f0e9b 100644 --- a/README.md +++ b/README.md @@ -6,26 +6,34 @@ A [b9](https://github.com/mhelgeson/b9) slack bot plugin, which provides and maintains an array of team users. +## Methods + +#### `b9.user( key )` +Returns a user object. + +- **`key`** *`{String}`*
+The `id` or `name` of a user to find. + ## Properties #### `b9.users` *`{Array}`* -A list of user objects, one for every member of the team. +A list of user objects, one for every member of the team.
https://api.slack.com/types/user ## Listeners #### `"rtm.start"` -Initializes the `users` list. +Initializes the `users` list.
https://api.slack.com/methods/rtm.start #### `"team_join"` -Adds a new user to the `users` list. +Adds a new user to the `users` list.
https://api.slack.com/events/team_join #### `"user_change"` -Updates an item in the `users` list. +Updates an item in the `users` list.
https://api.slack.com/events/user_change #### `"presence_change"` -Updates an item in the `users` list. +Updates an item in the `users` list.
https://api.slack.com/events/presence_change diff --git a/index.js b/index.js index 6f5abf1..93a860a 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,21 @@ module.exports = function( b9 ){ + // define public property + b9.users = []; + + // define public method + b9.user = function( key ){ + var found; + b9.users.every(function( user ){ + if ( user.id === key || user.name === key ){ + found = user; + } + return !found; + }); + return found; + }; + + // initialize the users list b9.on('rtm.start', function( arg ){ b9.users = arg.users; }); diff --git a/test.js b/test.js index 54582e3..8887c99 100644 --- a/test.js +++ b/test.js @@ -58,4 +58,10 @@ describe('src/index', function(){ assert.equal( bot.users[2].presence, 'away' ); }); + it('finds a user by name or id',function(){ + assert.equal( bot.user('U002').name, 'John Adams' ); + assert.equal( bot.user('Thomas Jefferson').id, 'U003' ); + assert.equal( bot.user('U007'), undefined ); + }); + });