From d140cbdb343a9a8338542424ae3cf9d02f328f75 Mon Sep 17 00:00:00 2001 From: Yuya Takeyama Date: Sun, 6 Sep 2015 18:42:53 +0900 Subject: [PATCH] Add options argument for notifications() For the backward compatibility, options can be omitted --- README.md | 2 +- github.js | 37 +++++++++++++++++++++++++++++++++++-- test/test.user.js | 13 +++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 84d870dc..162417e7 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,7 @@ user.gists(function(err, gists) {}); List unread notifications for the authenticated user. ```js -user.notifications(function(err, notifications) {}); +user.notifications(options, function(err, notifications) {}); ``` Show user information for a particular username. Also works for organizations. diff --git a/github.js b/github.js index 6f8dc215..cdf9ed74 100644 --- a/github.js +++ b/github.js @@ -157,8 +157,41 @@ // List authenticated user's unread notifications // ------- - this.notifications = function(cb) { - _request("GET", '/notifications', null, function(err, res) { + this.notifications = function(options, cb) { + if (arguments.length === 1 && typeof arguments[0] === 'function') { + cb = options; + options = {}; + } + options = options || {}; + var url = '/notifications'; + var params = []; + if (options.all) { + params.push('all=true'); + } + if (options.participating) { + params.push('participating=true'); + } + if (options.since) { + var since = options.since; + if (since.constructor === Date) { + since = since.toISOString(); + } + params.push('since=' + encodeURIComponent(since)); + } + if (options.before) { + var before = options.before; + if (before.constructor === Date) { + before = before.toISOString(); + } + params.push('before=' + encodeURIComponent(before)); + } + if (options.page) { + params.push('page=' + encodeURIComponent(options.page)); + } + if (params.length > 0) { + url += '?' + params.join('&'); + } + _request("GET", url, null, function(err, res) { cb(err,res); }); }; diff --git a/test/test.user.js b/test/test.user.js index 7d0f11f8..78eef663 100644 --- a/test/test.user.js +++ b/test/test.user.js @@ -34,6 +34,19 @@ test("User API", function(t) { }); }); + t.test('user.notifications with options', function(q) { + var options = { + all: true, + participating: true, + since: '2015-01-01T00:00:00Z', + before: '2015-02-01T00:00:00Z' + }; + user.notifications(options, function(err) { + q.error(err, 'user notifications'); + q.end(); + }); + }); + t.test('user.show', function(q) { user.show('ingalls', function(err) { q.error(err, 'user show');