Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Beach safety web hook
module['exports'] = function udpateBeachStatus (hook) {
var got = require('got');
got('http://hawaiibeachsafety.com/rest/ratings.json')
.then(function (response) {
hook.res.end(response.body);
})
.catch(function (error) {
hook.res.end("Error fetching data: " + error);
});
};
module['exports'] = function udpateBeachStatus (hook) {
var got = require('got');
got('http://hawaiibeachsafety.com/rest/ratings.json')
.then(function (response) {
var ratings = JSON.parse(response.body);
for(var rating of ratings) {
if(rating.beach.match(/poipu/)) {
hook.res.end(JSON.stringify(rating));
}
}
})
.catch(function (error) {
hook.res.end("Error fetching data: " + error);
});
};
module['exports'] = function udpateBeachStatus (hook) {
var got = require('got');
got('http://hawaiibeachsafety.com/rest/ratings.json')
.then(function (response) {
var ratings = JSON.parse(response.body);
for(var rating of ratings) {
if(rating.beach.match(/poipu/)) {
var color;
switch(rating.nearshore) {
case "low": color = "green"; break;
case "high": color = "yellow"; break;
case "extreme": color = "red"; break;
}
hook.res.end(color);
}
}
})
.catch(function (error) {
hook.res.end("Error fetching data: " + error);
});
};
module['exports'] = function udpateBeachStatus (hook) {
var got = require('got');
var Particle = require('spark');
got('http://hawaiibeachsafety.com/rest/ratings.json')
.then(function (response) {
var ratings = JSON.parse(response.body);
for(var rating of ratings) {
if(rating.beach.match(/poipu/)) {
var color;
switch(rating.nearshore) {
case "low": color = "green"; break;
case "high": color = "yellow"; break;
case "extreme": color = "red"; break;
}
return color;
}
}
})
.then(function (color) {
hook.res.write("Updating device with color " + color + "\n");
return Particle.login({ accessToken: hook.env.PARTICLE_API_TOKEN })
.then(function () {
return Particle.getDevice('mydevicename');
})
.then(function (device) {
return device.callFunction("beach-status", color);
});
})
.then(function () {
hook.res.end("Done!");
})
.catch(function (error) {
hook.res.end("Error fetching data: " + error);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.