Last active
April 29, 2021 07:34
-
-
Save soukron/766db7f7a90f4213bdde to your computer and use it in GitHub Desktop.
Simple plugin for Baasbox to act as an API gateway for Foursquare venues API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* script: venues.fs.search */ | |
/* This script endpoint: /plugin/venues.fs.search */ | |
on('install',function(installEvent){ | |
var success = Box.DB.createCollection('venues-cache-queries'); | |
var success = Box.DB.createCollection('venues-cache-answers'); | |
return true; | |
}); | |
http().get(function(req){ | |
// parse parameter in format "lat,long" to array | |
var ll = req.queryString.ll.toString(); | |
var llarray = ll.split(','); | |
// very basic check: we need two coordinates | |
if (llarray.length == 2) { | |
// query in cache collection for previous queries with 100m of distance | |
var queries = Box.Documents.find('venues-cache-queries', { | |
where: 'endpoint = "search" and distance(lat, long, ' + | |
llarray[0] + ', ' + llarray[1] + ') < 0.1', | |
orderBy: '_creation_date desc' | |
}); | |
// if there's a previous query, let's answer its content | |
if (queries.length > 0) { | |
var answer = Box.Documents.find('venues-cache-answers', { | |
where: 'id = "'+queries[0]['answer-id'].toString() + '"' | |
}); | |
return {status: answer[0].meta.code, content: answer[0]}; | |
} | |
// otherwise, call to FS api endpoint | |
else { | |
var result = Box.WS.get('https://api.foursquare.com/v2/venues/search/', | |
{ | |
params:{ | |
ll: ll, | |
intent: "checkin", | |
radius: 250, | |
limit: 20, | |
locale: "es", | |
v: "20150827", | |
m: "swarm", | |
client_id: "YOURCLIENTID", | |
client_secret: "YOURCLIENTSECRET" | |
} | |
} | |
); | |
// create document with query information | |
var answer=Box.Documents.save('venues-cache-answers', result.body); | |
// create document with answer from FS | |
var query=Box.Documents.save('venues-cache-queries', { | |
"endpoint": "search", | |
"lat": llarray[0], | |
"long": llarray[1], | |
"answer-id": answer.id | |
}); | |
// set permissions for both previous documents so all registered | |
// users can read its content | |
Box.Documents.grant('venues-cache-answers',answer.id, | |
{ | |
roles: { | |
read:["registered"], | |
}, | |
}); | |
Box.Documents.grant('venues-cache-queries',query.id, | |
{ | |
roles: { | |
read:["registered"], | |
}, | |
}); | |
// return FS information | |
return {status: result.body.meta.code, content: result.body}; | |
} | |
} | |
// otherwise return not found | |
else { | |
return {status: 404}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh my god. I never answered to your suggestions. Thanks so much. I had a good time with Baasbox.