Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@monkbroc
Last active December 13, 2015 16:03
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 monkbroc/2ee20c7a5d4d3a0d2327 to your computer and use it in GitHub Desktop.
Save monkbroc/2ee20c7a5d4d3a0d2327 to your computer and use it in GitHub Desktop.
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