Upgrade node environment to Node.js 7.6.0+ (#151)

Upgrades the node environment to NodeJS 7.6.0.  Functions can now use async/await and promises; they can return a promise instead of using a callback.  The change preserves compatibility with the callback style.  

* Upgrade node environment to Node.js 7.6.0+

* Bump package version

* Preserve compatibility for callbacks

* Cleanup

* Re-add hello callback example

* Make sure not returning won't blow everything up

* Update stock example with request promise
This commit is contained in:
Robert Herhold
2017-03-14 16:00:26 -07:00
committed by Soam Vasani
parent e1cb5f6e09
commit 9bdaf3c74e
9 changed files with 112 additions and 76 deletions
+29 -23
View File
@@ -1,31 +1,37 @@
'use strict';
var http = require('http');
const rp = require('request-promise-native');
module.exports = function (context, callback) {
let body = context.request.body;
console.log(`body text: ${body['text']}`);
module.exports = async function (context) {
const body = context.request.body;
const symbol = body.symbol
var symbol = body['text'].split(' ')[1];
console.log(`Got symbol: ${symbol}`);
http.get({
host: 'finance.google.com',
path: `/finance/info?q=NYSE:${symbol}`
}, function(response) {
var resp = '';
response.on('data', function(d) {
resp += d;
});
response.on('end', function() {
try {
var parsed = JSON.parse(resp.slice(3));
var lastTrade = parsed[0]['l_cur']
callback(200, `{ "text": "${symbol} last traded at ${lastTrade}" }`);
} catch (e) {
callback(200, `{ "text": "Error (invalid NYSE symbol?)" }`);
if (!symbol) {
return {
status: 400,
body: {
text: 'You must provide a stock symbol.'
}
});
});
};
}
try {
const response = await rp(`http://finance.google.com/finance/info?q=NYSE:${symbol}`);
const parsed = JSON.parse(response.slice(3));
const lastTrade = parsed[0]['l_cur'];
return {
status: 200,
body: {
text: `${symbol} last traded at ${lastTrade}`
}
};
} catch (e) {
console.error(e);
return {
status: 500,
body: e
};
}
}