Skip to main content
Adaptive Insights
Knowledge and Support - Adaptive Insights

Uber Full Example

This example requires you to provide your own UberServerToken and Google API Key as Static Text. Your UberServerToken and Google API Key are entered in the Data Source Settings tab for the Data Designer.

This example also requires a StartAddress and EndAddress as parameterized text.

function testConnection(context) {
   return true;
}


function importStructure(context) {
   var builder = context.getStructureBuilder();
   var progressManager = context.getProgressManager();
   var progressIncrement = 100 / 2;
   createTable(builder);
   progressManager.setProgress(progressManager.getProgress() + progressIncrement);
   createTimesTable(builder);
   progressManager.setProgress(progressManager.getProgress() + progressIncrement);
}


function createTable(builder) {
   var tableId = 'prices';
   var tableDisplayName = 'Prices';
   var table = builder.addTable(tableId);
   table.setDisplayName(tableDisplayName);
   var i = 0;
   createColumn(table, "localized_display_name", "Localized Display Name", i++, false, "string");
   createColumn(table, "high_estimate", "High Estimate", i++, false, "number");
   createColumn(table, "minimum", "Minimum", i++, false, "number");
   createColumn(table, "duration", "Duration (seconds)", i++, false, "number");
   createColumn(table, "estimate", "Estimate", i++, false, "string");
   createColumn(table, "distance", "Distance (miles)", i++, false, "number");
   createColumn(table, "display_name", "Display Name", i++, false, "string");
   createColumn(table, "product_id", "Product Id", i++, false, "string");
   createColumn(table, "low_estimate", "Low Estimate", i++, false, "number");
   createColumn(table, "surge_multiplier", "Surge Multiplier", i++, false, "number");
   createColumn(table, "currency_code", "Currency Code", i++, false, "string");
}


function createTimesTable(builder) {
   var tableId = 'times';
   var tableDisplayName = 'Times';
   var table = builder.addTable(tableId);
   table.setDisplayName(tableDisplayName);
   var i = 0;
   createColumn(table, "localized_display_name", "Localized Display Name", i++, false, "string");
   createColumn(table, "estimate", "Estimate (seconds)", i++, false, "number");
   createColumn(table, "display_name", "Display Name", i++, false, "string");
   createColumn(table, "product_id", "Product Id", i++, false, "string");
}


function createColumn(table, columnId, columnDisplayName, displayOrder, mandatoryForImports, dataType) {
   var column = table.addColumn(columnId);
   column.setDisplayName(columnDisplayName);
   column.setDisplayOrder(displayOrder);
   column.setMandatoryForImports(mandatoryForImports);
   switch (dataType) {
       case "integer":
           column.setIntegerColumnType();
           break;
       case "string":
       case "base64_encoded":
           column.setTextColumnType(100);
           break;
       case "date":
           column.setDateTimeColumnType();
           break;
       case "datetime":
           column.setDateTimeColumnType();
           break;
       case "boolean":
           column.setBooleanColumnType();
           break;
       case "number":
       case "currency":
           column.setFloatColumnType();
           break;
       default:
           // unknown type
           ai.log.logInfo('Unable to create a column. Its type is unknown.', 'Column ' + columnId + ' could not be created because its data type was not known. Its data type was ' + dataType +
               '.');
           break;
   }
}


function convertAddressToGeoCode(address, key) {
   var url = 'https://maps.googleapis.com/maps/api.../json?address=' + encodeURI(address) + '&key=' + key;
   var method = 'GET';
   var headers = {
       'Content-Type': 'text/json'
   };
   var body = '';
   var response = JSON.parse(ai.https.request(url, method, body, headers).getBody());
   var long = response.results[0].geometry.location.lng;
   var lat = response.results[0].geometry.location.lat;
   return [lat, long];
}


function importData(context) {
   // Step 1: Make use of passed in contextual information to create a rowset object. Here, I am simply assigning tableId, maxRows and columnNames to variables to be used elsewhere in this script.
   var rowset = context.getRowset();
   rowset.setSmartParsingEnabled(true);
   var tableId = rowset.getTableId();
   var columns = rowset.getColumns();
   var columnRemoteIds = getColumnRemoteIds(columns);
   var dataSource = context.getDataSource();
   var googleKey = dataSource.getSetting("Google API Key").getValue();
   var uberKey = dataSource.getSetting("Uber Server Token").getValue();
   var startAddress = dataSource.getSetting("Start Address").getValue();
   var endAddress = dataSource.getSetting("End Address").getValue();
   var startGeoCode = convertAddressToGeoCode(startAddress, googleKey);
   var endGeoCode = convertAddressToGeoCode(endAddress, googleKey);
   if (tableId === 'prices') {
       // Step 2: Create a https request that will return data - in this case, Yahoo Finance will return JSON.
       var url = 'https://api.uber.com/v1/estimates/pr...tart_latitude=' + startGeoCode[0] + '&start_longitude=' + startGeoCode[1] + '&end_latitude=' + endGeoCode[0] + '&end_longitude=' +
           endGeoCode[1] + '&server_token=' + uberKey;
       var method = 'GET';
       var headers = {
           'Content-Type': 'text/json'
       };
       var body = '';
       // Step 3: Send https request and receive response. Normally you would want to check that the response contains a success message first before looking at the rows.
       var response = ai.https.request(url, method, body, headers);
       // Step 4: Parse the https response body.
       var data = parseData(response.getBody());
       var rows = parseRows(data);
       // Step 5: Process each row to extract the cell values for each column and add them as an array to the rowset in the expected column order.   
       addRows(rowset, rows);
   }
   if (tableId === 'times') {
       // Step 2: Create a https request that will return data - in this case, Yahoo Finance will return JSON.
       var url = 'https://api.uber.com/v1/estimates/ti...tart_latitude=' + startGeoCode[0] + '&start_longitude=' + startGeoCode[1] + '&server_token=' + uberKey;
       var method = 'GET';
       var headers = {
           'Content-Type': 'text/json'
       };
       var body = '';
       // Step 3: Send https request and receive response. Normally you would want to check that the response contains a success message first before looking at the rows.
       var response = ai.https.request(url, method, body, headers);
       // Step 4: Parse the https response body.
       var data = parseData(response.getBody());
       var rows = parseTimeRows(data);
       // Step 5: Process each row to extract the cell values for each column and add them as an array to the rowset in the expected column order.   
       addRows(rowset, rows);
   }
}


function getColumnRemoteIds(columns) {
   var columnIds = [];
  for (var i = 0; i < columns.length; i++) {
       columnIds.push(columns[i].getId());
   }
   return columnIds;
}


function parseData(responseJSON) {
   return JSON.parse(responseJSON);
}


function parseRows(json) {
   return json.prices;
}


function parseTimeRows(json) {
   return json.times;
}


function addRows(rowset, rows) {
   var columnRemoteIds = getColumnRemoteIds(rowset.getColumns());
   for (var i = 0; i < rows.length; i++) {
       var cells = [];
       var row = rows[i];
       for (var x = 0; x < columnRemoteIds.length; x++) {
           var propName = columnRemoteIds[x];
           cells.push(row[propName]);
       }
       rowset.addRow(cells);
   }
}
  • Was this article helpful?