You are here:
< Back

Sample datasource connectors

(Facebook, Instagram, Feedly, Twitter, FB-workplace, spreadsheet)

Connectors are NodeJs scripts that collects, refines and then returns data form a third-party API.

All the connectors are built the same way, the only thing that is different between them is the URL where they are making the request to.

  • The script first makes a request to the API and collects data.
const getData= (url, callback) => {
http.get(url, function(res) {
let body = '';
res.on('data', function(chunk) {
body += chunk;
});

res.on('end', function() {
let response = JSON.parse(body);
callback(response);
});
});
};
  • After that it sorts and modifies the collected data, to be left with only the desired elements.
getData (url, (resp) => {

delete resp.pagination;
delete resp.meta;

if (resp.hasOwnProperty("data")){
for (let i=0; i < resp['data'].length; i++){
let title, likes;

//titles
if (resp["data"][i]["caption"] != null){
title = {caption : resp["data"][i]["caption"]["text"]};
}else{
title = {caption : ""};
}

//likes
if (resp["data"][i]["likes"] != null){
likes = {likes: resp["data"][i]["likes"]["count"]};
}else{
likes = {likes: ""};
}

let post = {
title: title,
like: likes
};
posts.push(post);
}
}
});
  • Than the refined data is returned in JSON format
res.setHeader('Content-Type', 'application/json');
res.send({posts: posts});

Sample widgets

(Facebook, Instagram, Feedly, Twitter, FB-workplace, spreadsheet)

Widgets are basically web-applications that displays the returned data by the connectors.

They can be displayed in a content with the custom element in our editor.

Sample Customer Calling System

The customer caller system has three endpoints (newCustomer, listCustomers, callCustomer) .

  • It stores the customer list in a JSON file.
function writeFile(customer){
     let data = JSON.stringify(customer);  
     fs.writeFile('customers.json', data, (err) => {  
            if (err) throw err;
         console.log('Data written to file');
     });
 }

function readFile(callback){
	fs.readFile('customers.json', (err, data) => {  
    	    if (err) throw err;
    	    let customers = JSON.parse(data);
    	    callback(customers);
	});
}
  • The newCustomer endpoint receives the persons information as URL parameters, and saves it to the customers file.
newCustomer: function(req,res){
  const person;

  getCustomers('all',(customers)=>{
  	customers.customerList.push({type: person.type,
                                     name: person.name,
                                     gender: person.gender,
                                     region: person.region
         });
  	writeFile(customers);
  });
}
  • The listCustomers returns either customers from a type, or all of them.
function getCustomers(type,callback){
	readFile((customers)=>{
		let response = {customerList: []};

    	for (let i=0; i < customers.customerList.length; i++){
    		if (customers.customerList[i].type ==  type){
    			response.customerList.push(customers.customerList[i]);
    		}
    	}

    	if (type = "all"){
	    callback(customers);
	}else{
	    callback(response);
	}
	});
}

listCustomers: function(req,res){
     const type = req.query.type;

      getCustomers(type,(customers)=>{
    	  res.setHeader('Content-Type', 'application/json');
    	  res.send({customers: customers});
 })},
  • The callCustomer endpoint returns the next customer form a type and deletes the current one form the file.
callCustomer: function(req,res){
    	const type = req.query.type;

    	getCustomers(type,(customers)=>{
    		let nextCustomer = null;

    		for (let i=0; i<customers.customerList.length; i++){
    			if (customers.customerList[i].type == type && nextCustomer == null){
    				customers.customerList.splice(i,1);
    				nextCustomer = customers.customerList[i];
    				writeFile(customers);
    			}
    		}

    		if (nextCustomer != null){
    			res.setHeader('Content-Type', 'application/json');
    			res.send({nextCustomer: nextCustomer});
    		}else{
    			res.setHeader('Content-Type', 'application/json');
    			res.send({nextCustomer: "No more customers in that type!"});
    		}
    })}

Sample wayfinding system

The wayfindig system can guide visitors trough a building to find an event.

It collects the events from a google calendar, and information about the building and the screens from a google spreadsheet.

Then it calculates the routs and returns with data for each screen about the directions that they have to show.

Comments are closed.