Skip to content

Commit

Permalink
Merge pull request #12 from mrz1277/master
Browse files Browse the repository at this point in the history
support geo search api & image array parameter
  • Loading branch information
ro-ka committed Dec 19, 2014
2 parents ab25d25 + 33fcbff commit 78eb90b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -57,6 +57,7 @@ Choose on of the funtions to get photos:
* `getFreshWeek (arguments, callback)`
* `searchByTag (tag, arguments, callback)`
* `searchByTerm (term, arguments, callback)`
* `searchByGeo (geo, arguments, callback)`

For more information on the possible arguments, please check the [500px.com photos API](http://developer.500px.com/docs/photos-index)

Expand Down
13 changes: 12 additions & 1 deletion lib/photos.js
Expand Up @@ -164,4 +164,15 @@ Photos.prototype.searchByTerm = function(term, parameters, callback) {
this._request.executeRequest('photos/search', parameters, callback);
};

module.exports = Photos;
Photos.prototype.searchByGeo = function(geo, parameters, callback) {
if (!parameters) {
parameters = {};
}

parameters.geo = geo;

this._request.executeRequest('photos/search', parameters, callback);
};


module.exports = Photos;
18 changes: 4 additions & 14 deletions lib/request.js
Expand Up @@ -3,6 +3,7 @@
*/

var https = require('https'),
querystring = require('querystring'),
Request = function Request(consumer_key) {
this._configure(consumer_key);
};
Expand All @@ -14,26 +15,15 @@ Request.prototype._configure = function(consumer_key) {
};

Request.prototype.executeRequest = function(method, parameters, callback) {
var parameterString = '',
operator = '?';

if (parameters === undefined) {
parameters = {};
}

parameters.consumer_key = this.consumer_key;

for (var key in parameters) { // jshint ignore:line
if (key) {
parameterString += (operator + key + '=' + parameters[key]);
if (operator === '?') {
operator = '&';
}
}
}

https.get(
{'host': this.host, 'path': this.baseUrl + method + parameterString },
https.get({
'host': this.host,
'path': this.baseUrl + method + '?' + querystring.stringify(parameters) },
function(res) {
var chunks = '';
res.on('data', function(resultData) {
Expand Down
31 changes: 30 additions & 1 deletion test/photos-test.js
Expand Up @@ -206,4 +206,33 @@ describe('Testing GET photos:', function() {
});
});
});
});

describe('#searchByGeo()', function() {
it('should return a json with photos', function(done) {
photos.searchByGeo('37,126,100km', {'rpp': 7}, function(err, result) {
should.not.exist(err);
should.exist(result);
result.should.have.property('photos');
result.photos.should.have.lengthOf(7);

done();
});
});
});

describe('#searchWithMultiImageSize()', function() {
it('should return a json with photos', function(done) {
photos.searchByGeo('37,126,100km', {
'rpp': 7, 'image_size[]': [1, 4]
}, function(err, result) {
should.not.exist(err);
should.exist(result);
result.should.have.property('photos');
result.photos.should.have.lengthOf(7);
result.photos[0].images.should.have.lengthOf(2);

done();
});
});
});
});

0 comments on commit 78eb90b

Please sign in to comment.