Skip to content
This repository has been archived by the owner on Apr 27, 2023. It is now read-only.

Commit

Permalink
refactor gists retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Jan 10, 2016
1 parent 5818733 commit dbaa15d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 56 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,3 @@ fetchGists(accessToken)
```

`fetchGists` will return a [promise](https://github.com/petkaantonov/bluebird). The promise will resolve once all gists for the account have been retrieved. Any errors that occur during the retrieval of the gists will cause the promise to reject.

## Debugging

If you are having issues with retrieving your gists, `fetch-gists` can output some debugging information that maybe useful:

```bash
DEBUG=fetch-gists node your-script.js
```
9 changes: 9 additions & 0 deletions foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var fetchGists = require('./index.js');

var accessToken = '3ed3386df5c36ae31eb90df73f75c3408729a3db';

fetchGists(accessToken).then(function(gists) {
console.log('gists = ' + gists.length)
}).catch(function(err) {
console.log(err);
});
76 changes: 29 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var debug = require('debug')('fetch-gists');
var Promise = require('bluebird');
var request = require('request');

Expand All @@ -9,37 +8,37 @@ var request = require('request');
module.exports = fetchGists;

/**
* Fetch all gists for an account
* Fetch gists for the given access token
*
* @param {string} accessToken
* @param {number} [page]
* @param {Object[]} [gists]
* @param {function} [onFetchedGists]
* @returns {Object[]}
* @param {string} accessToken
* @return {Object[]}
*/

function fetchGists(accessToken, page, gists, onFetchedGists) {
page = page || 1;
gists = gists || [];

return new Promise(function(resolve, reject) {
if (onFetchedGists) {
resolve = onFetchedGists;
}
function fetchGists(accessToken) {
return fetchAllGists([], 1, accessToken);
}

fetchPageOfGists(page, accessToken).then(function(result) {
result.gists.map(function(gist) {
gists.push(gist);
});
/**
* Recursively fetch all the gists for an account
*
* @param {Object[]} gists
* @param {number} page
* @param {string} accessToken
* @return {Object[]|Promise}
*/

if (result.nextPage) {
fetchGists(accessToken, result.nextPage, gists, resolve);
} else {
resolve(gists);
}
}).catch(function(error) {
reject(error);
function fetchAllGists(gists, page, accessToken) {
return fetchPageOfGists(page, accessToken).then(function(result) {
result.gists.forEach(function(gist) {
gists.push(gist);
});

if (result.moreToGet) {
page++;
return fetchAllGists(gists, page, accessToken);
}

return gists;
});
}

Expand All @@ -52,8 +51,6 @@ function fetchGists(accessToken, page, gists, onFetchedGists) {
*/

function fetchPageOfGists(page, accessToken) {
debug('Fetching gists from page ' + page);

// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
var opts = {
url: 'https://api.github.com/gists',
Expand Down Expand Up @@ -92,26 +89,11 @@ function fetchPageOfGists(page, accessToken) {
return reject(errorMessage);
}

var nextPage = (response.headers.link) ?
getNextPageFromLink(response.headers.link) :
null;
var moreToGet = (response.headers.link) ?
(response.headers.link.indexOf('rel="next"') > -1) :
false;

resolve({ gists: body, nextPage: nextPage });
resolve({ gists: body, moreToGet: moreToGet });
});
});
}

/**
* Extract the next page from the link header
*
* @param {string} link
* @returns {number}
*/

function getNextPageFromLink(link) {
if (link.indexOf('rel="next"') > -1) {
debug('Found link for next page');

return link.match(/[0-9]+/g)[0];
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
],
"dependencies": {
"bluebird": "^3.1.1",
"debug": "^2.2.0",
"request": "^2.67.0"
},
"devDependencies": {
Expand Down

0 comments on commit dbaa15d

Please sign in to comment.