Skip to content

Commit

Permalink
add search reddit functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostas Argy committed Aug 30, 2018
1 parent 5605f1b commit 772ce3f
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions lib/imgur.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ imgur._imgurRequest = function (operation, payload, extraFormParams) {
options.method = 'GET';
options.uri += '/gallery/search/' + payload
break;
case 'searchReddit':
options.method = 'GET';
options.uri += '/gallery/r/' + payload
break;
default:
deferred.reject(new Error('Invalid operation'));
return deferred.promise;
Expand Down Expand Up @@ -425,7 +429,7 @@ imgur.getAlbumInfo = function (id) {
imgur.search = function(query, options) {
var deferred = Q.defer();
var checkQuery = imgur.checkQuery(query)
var params;
var params;
options = options || {}
if(checkQuery.constructor === Error) {
deferred.reject(checkQuery)
Expand All @@ -442,15 +446,39 @@ imgur.search = function(query, options) {
deferred.reject(err);
});
}


return deferred.promise
}

imgur.searchReddit = function(query, options) {
var deferred = Q.defer();
var checkQuery = imgur.checkQuery(query)
var params;
options = options || {}
if(checkQuery.constructor === Error) {
deferred.reject(checkQuery)
}
else {
var params = imgur.initSearchParams(query, options);
imgur._imgurRequest('searchReddit', params.queryStr)
.then(function (json) {
var copyOfParams = params
delete copyOfParams['queryStr']
deferred.resolve({data: json.data, params: copyOfParams});
})
.catch(function (err) {
deferred.reject(err);
});
}

return deferred.promise
}

imgur.checkQuery = function(query) {
var errMsg;
var errMsg;
if(!query) {
errMsg = new Error("Search requires a query. Try searching with a query (e.g cats).")
}
}
else if(typeof query != 'string') {
errMsg = new Error("You did not pass a string as a query.")
}
Expand Down

0 comments on commit 772ce3f

Please sign in to comment.