Skip to content

Commit d140cbd

Browse files
committed
Add options argument for notifications()
For the backward compatibility, options can be omitted
1 parent 9029554 commit d140cbd

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ user.gists(function(err, gists) {});
227227
List unread notifications for the authenticated user.
228228

229229
```js
230-
user.notifications(function(err, notifications) {});
230+
user.notifications(options, function(err, notifications) {});
231231
```
232232

233233
Show user information for a particular username. Also works for organizations.

github.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,41 @@
157157
// List authenticated user's unread notifications
158158
// -------
159159

160-
this.notifications = function(cb) {
161-
_request("GET", '/notifications', null, function(err, res) {
160+
this.notifications = function(options, cb) {
161+
if (arguments.length === 1 && typeof arguments[0] === 'function') {
162+
cb = options;
163+
options = {};
164+
}
165+
options = options || {};
166+
var url = '/notifications';
167+
var params = [];
168+
if (options.all) {
169+
params.push('all=true');
170+
}
171+
if (options.participating) {
172+
params.push('participating=true');
173+
}
174+
if (options.since) {
175+
var since = options.since;
176+
if (since.constructor === Date) {
177+
since = since.toISOString();
178+
}
179+
params.push('since=' + encodeURIComponent(since));
180+
}
181+
if (options.before) {
182+
var before = options.before;
183+
if (before.constructor === Date) {
184+
before = before.toISOString();
185+
}
186+
params.push('before=' + encodeURIComponent(before));
187+
}
188+
if (options.page) {
189+
params.push('page=' + encodeURIComponent(options.page));
190+
}
191+
if (params.length > 0) {
192+
url += '?' + params.join('&');
193+
}
194+
_request("GET", url, null, function(err, res) {
162195
cb(err,res);
163196
});
164197
};

test/test.user.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ test("User API", function(t) {
3434
});
3535
});
3636

37+
t.test('user.notifications with options', function(q) {
38+
var options = {
39+
all: true,
40+
participating: true,
41+
since: '2015-01-01T00:00:00Z',
42+
before: '2015-02-01T00:00:00Z'
43+
};
44+
user.notifications(options, function(err) {
45+
q.error(err, 'user notifications');
46+
q.end();
47+
});
48+
});
49+
3750
t.test('user.show', function(q) {
3851
user.show('ingalls', function(err) {
3952
q.error(err, 'user show');

0 commit comments

Comments
 (0)