Skip to content

Instantly share code, notes, and snippets.

@stuartlynn
Created June 16, 2015 01:49
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 stuartlynn/fc57e207358e222df77c to your computer and use it in GitHub Desktop.
Save stuartlynn/fc57e207358e222df77c to your computer and use it in GitHub Desktop.
Sample iffT carto integration server
#This is actually running and dumping data here http://cdb.io/1BeHh9S
express = require 'express'
app = express()
redis = require("redis")
url = require('url');
CartoDB = require('cartodb');
bodyParser = require('body-parser')
jsonParser = bodyParser.json()
chanKey = process.env.ifttt_chan_key
table = "untitled_table_2"
CartoClient = new CartoDB({user: process.env.cartoUser,api_key: process.env.cartoAPI});
if process.env.REDISCLOUD_URL
redisURL = url.parse(process.env.REDISCLOUD_URL);
client = redis.createClient(redisURL.port, redisURL.hostname, {no_ready_check: true});
client.auth(redisURL.auth.split(":")[1]);
else
client = redis.createClient()
IsValidChannel = (req)=>
return req.headers["ifttt-channel-key"] == chanKey
testData = {
"data": {
"samples": {
"actions": {
"add_to_map": {
"map_url": "https://maps.google.com/?q=40.7180724,-73.9520359&z=18"
"link_to_image": "http://instagr.am/p/CTGBw/"
}
},
"actionRecordSkipping": {
"add_to_map": {
"map_url": "https://maps.google.com/?q=40.7180724,-73.9520359&z=18"
"link_to_image": "http://instagr.am/p/CTGBw/"
}
}
}
}
}
postToCarto = (lat,lon, image_url)=>
query = "INSERT INTO #{table} (the_geom, image_url) VALUES (ST_GeomFromText('POINT(#{lon} #{lat})', 4326), '#{image_url}')"
console.log("query is ", query)
CartoClient.query query, (err,data)=>
if err
console.log err
else
console.log "success"
app.get "/", (req,res) =>
client.smembers "locations", (locs)=>
res.send(locs)
app.get "/ifttt/v1/status", (req,res)=>
if IsValidChannel(req)
res.status(200).end();
else
res.status(401).end()
app.post "/ifttt/v1/test/setup", (req,res)=>
if IsValidChannel(req)
res.status(200).json(testData)
else
res.status(401).end()
app.post "/ifttt/v1/actions/add_to_map", jsonParser, (req,res)=>
if IsValidChannel(req)
data = req.body
console.log('data', data)
if data.actionFields
map_url = data.actionFields.map_url
image_url = data.actionFields.link_to_image
ll = map_url.split("q=")[1].split("&")[0].split(",")
console.log("ll ", ll)
# newData = {date: new Date(), lat: ll[0], lng: ll[1], image_url: image_url}
postToCarto(ll[0], ll[1], image_url)
res.status(200).json({data: [{id: new Date(), url:'http://cartodb.com'}]})
else
res.status(400).json({errors:["Missing fields could not work :-("]})
else
res.status(401).json({errors:["That didn't work sorry :-("]})
server = app.listen (process.env.PORT || 3000), =>
host = server.address().address;
port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment