Created
November 28, 2018 18:43
-
-
Save evaristoc/0bef106beea100091851f6334591c53a to your computer and use it in GitHub Desktop.
This file contains 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
const utilities = { | |
apis: { | |
SearchParamsDB: { | |
queries: ["vegan", "organic cafes"], | |
querycontrol: 0, | |
location: [52.370216, 4.895168], | |
type: "restaurant", | |
radius: 1000, | |
region: "Amsterdam", | |
pagetoken: null, | |
placeids: [] | |
}, | |
SearchFunc: (prms, q, pgtkn) => { | |
//https://developers.google.com/maps/documentation/javascript/examples/place-search-pagination | |
//A constructor would be better here | |
let googleMapsClient = require('@google/maps').createClient({ | |
key: configGoogleMaps.confignode.google.browser.MapsAPIKEY, | |
Promise: Promise | |
}) | |
prms.pagetoken = pgtkn; | |
let paramqs = { | |
queries: q, | |
location: prms.location, | |
type: prms.type, | |
radius: prms.radius, | |
region: prms.region, | |
pagetoken: prms.pagetoken, | |
}; | |
return googleMapsClient | |
.places(paramqs) | |
.asPromise() | |
}, | |
PaginationFunc: async(res, user) => { | |
let eXtracting = (d) => { | |
if (!d) { | |
return //this go to catch, isn't it? | |
} | |
console.log("IN googlemapsPagination", d.status, d.json.status, d.json.results[0].formatted_address); | |
//o.inside = false; | |
let pl = []; | |
if ((d.json.status == 'OK') && (user.SearchParamsDB.queries.length > 0)) { | |
//TODO: assert for visibility of data, there is an error here!! | |
pl = d.json.results.map((e) => { return { source: "Google", name: e.name, id: e.place_id, query: q, address: e.formatted_address, status: 'OPEN' } }).filter((v) => { return user.SearchParamsDB.placeids.indexOf(v.id) == -1 }); | |
//check if we got to the end of the page for a query | |
console.log('pl', pl.length) | |
if (pl.length == 0) { | |
//if we are to end of page for that query, use the next one | |
user.searchParamDB.querycontrol = user.searchParamDB.queries.indexOf(q) > -1 ? user.searchParamDB.queries.indexOf(q) + 1 : user.searchParamDB.queries.length + 1; | |
if (user.searchParamDB.queries.length <= user.searchParamDB.querycontrol) { | |
let q = user.searchParamDB.queries[user.searchParamDB.querycontrol]; | |
console.log(user.searchParamDB.queries); | |
this.PaginationFunc(user.searchParamDB, q, null); //if there is still a query we havent explored, call it | |
} else { | |
user.searchParamDB.querycontrol = 0; | |
user.searchParamDB.stopApirequest = true; //otherwise, say to client there is no more to explore; OJO: should NOT be in the database but in memory! | |
res.status(200).send([{ status: 'CLOSED' }]); | |
} | |
} else { | |
//PAGINATION | |
user.SearchParamsDB.pagetoken = d.json.next_page_token; //if there is a next page token, use it | |
pl.forEach((v) => { user.SearchParamsDB.placeids.push(v.id) }); //deduplicate the data | |
res.status(d.status).send(pl); | |
} | |
} else { | |
res.status(200).send([{ status: 'CLOSED' }]); //send the chunk to the corresponding endpoint | |
}; | |
} | |
let q = user.searchParamDB.queries[user.searchParamDB.querycontrol]; | |
var user; //TODO | |
var response = await this.SearchFunc(user.SearchParamsDB, q, user.SearchParamsDB.pagetoken); | |
eXtracting(response); | |
} | |
} | |
} | |
}; |
nielsbom
commented
Nov 28, 2018
-- Separation of functions
-- Naming of variables, aliasing
-- Naming of functions (more active)
-- Linting, tabbing (shorter)
-- Improving conditional statement logic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment