Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ function convertSign(sign) {

// adds parameters that describe how the results are displayed to the end of the url
function appendResultParams(params, url) {
// add the page number to the end of the query, if a page is specified
// add the page number to the end of the query, if a page is specified
if (typeof params['page'] !== 'undefined') {
url += '&page=' + params['page'];
}

if (typeof params['per_page'] !== 'undefined') {
url += '&per_page=' + params['per_page'];
}

// add sort if exists
if (typeof params['sort'] !== 'undefined') {
url += '&sort=' + params['sort'];
Expand Down Expand Up @@ -86,6 +90,7 @@ function GithubSearcher(options) {
url: '',
headers: {
'User-Agent': this.auth.username,
'Accept': 'application/vnd.github.v3.text-match+json',
// add authorization to header so that you can send authenticated headers
'Authorization': 'Basic ' + new Buffer(this.auth.username + ':' + this.auth.password).toString('base64')
}
Expand All @@ -101,7 +106,7 @@ function GithubSearcher(options) {
}

// method to search for users
GithubSearcher.prototype.searchUsers = function(params, callback) {
GithubSearcher.prototype.searchUsers = function (params, callback) {

// throw error if the callback is not a function
if (typeof callback !== 'function') {
Expand All @@ -121,7 +126,7 @@ GithubSearcher.prototype.searchUsers = function(params, callback) {
if (typeof params === 'string') {
url += params;

// if an object, then create the url string by iterating through the object
// if an object, then create the url string by iterating through the object
} else if (typeof params === 'object') {

// add the term part first, since it is added differently
Expand All @@ -134,8 +139,8 @@ GithubSearcher.prototype.searchUsers = function(params, callback) {
if (k === 'repos' || k === 'followers') {
// repos and followers allows user to pass in comparison operators >, <, =
url += addFirstPlus(url) + k + ':' + convertSign(params[k][0]) + params[k].substring(1, params[k].length);
} else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') {
// term and page are handled separately/ differently
} else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order' && k !== 'per_page') {
// term and page are handled separately/ differently
url += addFirstPlus(url) + k + ':' + params[k];
}
}
Expand All @@ -151,7 +156,7 @@ GithubSearcher.prototype.searchUsers = function(params, callback) {
this.options.url = url;

// Use the request module to ping the url
request.get(this.options, function(error, response, body) {
request.get(this.options, function (error, response, body) {
if (!error && response.statusCode === 200) {
// pass our result to the callback
callback(JSON.parse(body));
Expand All @@ -163,7 +168,7 @@ GithubSearcher.prototype.searchUsers = function(params, callback) {
};

// method to query repositories
GithubSearcher.prototype.searchRepos = function(params, callback) {
GithubSearcher.prototype.searchRepos = function (params, callback) {
// throw error if the callback is not a function
if (typeof callback !== 'function') {
throw new Error('Callback is not a function!');
Expand Down Expand Up @@ -191,9 +196,9 @@ GithubSearcher.prototype.searchRepos = function(params, callback) {
// forks, stars and size allows user to pass in comparison operators >, <, =
// convert first element in string, and then concatenate the remainder of the string
url += addFirstPlus(url) + k + ':' + convertSign(params[k][0]) + params[k].substring(1, params[k].length);
} else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') {
// term, page, sort and order are handled separately/ differently
url += addFirstPlus(url) + k + ':' + params[k];
} else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order' && k !== 'per_page') {
// term, page, sort and order are handled separately/ differently
url += addFirstPlus(url) + k + ':' + params[k];
}
}

Expand All @@ -208,7 +213,7 @@ GithubSearcher.prototype.searchRepos = function(params, callback) {
this.options.url = url;

// use request module to ping url
request.get(this.options, function(error, response, body) {
request.get(this.options, function (error, response, body) {
if (!error && response.statusCode === 200) {
// pass our result to the callback
callback(JSON.parse(body));
Expand All @@ -219,7 +224,7 @@ GithubSearcher.prototype.searchRepos = function(params, callback) {
return this;
};

GithubSearcher.prototype.searchCode = function(params, callback) {
GithubSearcher.prototype.searchCode = function (params, callback) {

// throw error if the callback is not a function
if (typeof callback !== 'function') {
Expand Down Expand Up @@ -248,9 +253,9 @@ GithubSearcher.prototype.searchCode = function(params, callback) {
// size allows user to pass in comparison operators >, <, =
// convert first element in string, and then concatenate the remainder of the string
url += addFirstPlus(url) + k + ':' + convertSign(params[k][0]) + params[k].substring(1, params[k].length);
} else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') {
// term, page, sort and order are handled separately/ differently
url += addFirstPlus(url) + k + ':' + params[k];
} else if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order' && k !== 'per_page') {
// term, page, sort and order are handled separately/ differently
url += addFirstPlus(url) + k + ':' + params[k];
}
}

Expand All @@ -266,7 +271,7 @@ GithubSearcher.prototype.searchCode = function(params, callback) {
this.options.url = url;

// use request module to ping url
request.get(this.options, function(error, response, body) {
request.get(this.options, function (error, response, body) {
if (!error && response.statusCode === 200) {
// pass our result to the callback
callback(JSON.parse(body));
Expand All @@ -278,7 +283,7 @@ GithubSearcher.prototype.searchCode = function(params, callback) {

};

GithubSearcher.prototype.searchIssues = function(params, callback) {
GithubSearcher.prototype.searchIssues = function (params, callback) {

// throw error if the callback is not a function
if (typeof callback !== 'function') {
Expand All @@ -305,7 +310,7 @@ GithubSearcher.prototype.searchIssues = function(params, callback) {
for (var k in params) {
// just add everything to the query
// there are no cases where <, >, = are used
if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order') {
if (k !== 'term' && k !== 'page' && k !== 'sort' && k !== 'order' && k !== 'per_page') {
url += addFirstPlus(url) + k + ':' + params[k];
}
}
Expand All @@ -322,7 +327,7 @@ GithubSearcher.prototype.searchIssues = function(params, callback) {
this.options.url = url;

// use request module to ping url
request.get(this.options, function(error, response, body) {
request.get(this.options, function (error, response, body) {
if (!error && response.statusCode === 200) {
// pass our result to the callback
callback(JSON.parse(body));
Expand All @@ -333,4 +338,4 @@ GithubSearcher.prototype.searchIssues = function(params, callback) {
return this;
};

module.exports = GithubSearcher;
module.exports = GithubSearcher;