Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@evaristoc
Created November 28, 2018 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evaristoc/0bef106beea100091851f6334591c53a to your computer and use it in GitHub Desktop.
Save evaristoc/0bef106beea100091851f6334591c53a to your computer and use it in GitHub Desktop.
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
Copy link

/* eslint-env es6 */
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();
    },
    convertToPlaces: response => {
      return response.json.results
        .map(e => {
          return {
            source: "Google",
            name: e.name,
            id: e.place_id,
            query: q,
            address: e.formatted_address,
            status: "OPEN"
          };
      });
    },
    getNewPlaces: places => {
        return places.filter(
          place => {
            return user.SearchParamsDB.placeids.indexOf(place.id) == -1;
          }
        );
    },
    PaginationFunc: async (res, user) => {
      let extract = function(response) {
        if (!response) return; //this go to catch, isn't it?

        console.log(
          "IN googlemapsPagination",
          response.status,
          response.json.status,
          response.json.results[0].formatted_address
        );

        let responseWasSuccessFull = response.json.status === "OK" && user.SearchParamsDB.queries.length > 0;

        if (responseWasSuccessFull) {
          res.status(200).send([{ status: "CLOSED" }]); //send the chunk to the corresponding endpoint
        }
        //TODO: assert for visibility of data, there is an error here!!
        let newPlaces = this.getNewPlaces(this.convertToPlaces(response.json.results));

        if (newPlaces.length === 0) {
          // aliasing
          let qControl = user.searchParamDB.querycontrol;
          let queries = user.searchParamDB.queries;

          //if we are to end of page for that query, use the next one
          qControl = queries.indexOf(q) > -1
              ? queries.indexOf(q) + 1
              : queries.length + 1;
          if (
            queries.length <=
            qControl
          ) {
            let q = queries[qControl];
            console.log(queries);
            this.PaginationFunc(user.searchParamDB, q, null); //if there is still a query we havent explored, call it
          } else {
            qControl = 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 = response.json.next_page_token; //if there is a next page token, use it
          places.forEach(v => {
            user.SearchParamsDB.placeids.push(v.id);
          }); //deduplicate the data
          res.status(response.status).send(places);
        }
      };

      let q = user.searchParamDB.queries[user.searchParamDB.querycontrol];

      var user; //TODO
      var response = await this.SearchFunc(
        user.SearchParamsDB,
        q,
        user.SearchParamsDB.pagetoken
      );
      extract(response);
    }
  }
};

@evaristoc
Copy link
Author

evaristoc 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