restaurants
Version:
Search a service and get a list of restaurants.
62 lines (51 loc) • 1.29 kB
JavaScript
/**
* Dependencies
*/
var findAll = require('./find-all')
, get = require('superagent').get;
/**
* FourSquare constants
*/
var CLIENT_ID = process.env.FOURSQUARE_CLIENT_ID
, CLIENT_SECRET = process.env.FOURSQUARE_CLIENT_SECRET
, ENDPOINT = 'https://api.foursquare.com/v2/venues/search'
, MAX_LIMIT = 50
, MAX_OFFSET = 0;
/**
* Expose `search`
*/
module.exports = function(lat, lng, radius, all, callback) {
if (!CLIENT_ID) throw new Error('Environment variable FOURSQUARE_CLIENT_ID must be defined.');
if (!CLIENT_SECRET) throw new Error('Environment variable FOURSQUARE_CLIENT_SECRET must be defined.');
// Run the query
var query = {
intent: 'browse'
, query: 'restaurant'
, ll: lat + ',' + lng
, radius: radius
, client_id: CLIENT_ID
, client_secret: CLIENT_SECRET
, v: '20130419'
};
if (all) {
findAll(query, MAX_LIMIT, MAX_OFFSET, getRestaurants, callback);
} else {
getRestaurants(query, 10, 0, callback);
}
};
/**
* Get the getRestaurants
*/
function getRestaurants(query, limit, offset, callback) {
query.limit = limit;
query.offset = offset;
get(ENDPOINT)
.query(query)
.end(function(err, res) {
if (err) {
callback(err);
} else {
callback(err, res.body.response.venues);
}
});
}