Skip to content

Commit

Permalink
added b9.user() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mhelgeson committed Feb 10, 2016
1 parent 46642cf commit 385ae26
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}`* <br />
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. <br />
https://api.slack.com/types/user

## Listeners

#### `"rtm.start"`
Initializes the `users` list.
Initializes the `users` list. <br />
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. <br />
https://api.slack.com/events/team_join

#### `"user_change"`
Updates an item in the `users` list.
Updates an item in the `users` list. <br />
https://api.slack.com/events/user_change

#### `"presence_change"`
Updates an item in the `users` list.
Updates an item in the `users` list. <br />
https://api.slack.com/events/presence_change
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
});
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
});

});

0 comments on commit 385ae26

Please sign in to comment.