Last active
March 24, 2017 14:02
-
-
Save leemeichin/c1bd4146c03d7e7d4b148fadd62c9d9a to your computer and use it in GitHub Desktop.
tf-liedetector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ApiBuilder = require('claudia-api-builder') | |
var pg = require('pg') | |
var request = require('request-promise-native') | |
var find = require('lodash.find') | |
var api = new ApiBuilder() | |
var sentimentUrl = "http://sentiment.vivekn.com/api/text/" | |
// Check for auth as a strange kind of middleware thing | |
function requiresAuth(req, fn) { | |
if (process.env.KEY == req.queryString.key) { | |
return fn(req) | |
} else { | |
return new api.ApiResponse({error: '403 Forbidden'}, {'Content-Type': 'application/json'}, 403) | |
} | |
} | |
/** | |
* configuration settings come from environment: | |
* PGUSER * PGDATABASE * PGPASSWORD * PGPORT * PGHOST | |
*/ | |
var db = new pg.Client() | |
api.get('/', requiresAuth(req, (req) => { | |
var answers = req.body.form_response.answers | |
var rating = find(answers, ['type', 'number']) | |
var explanation = find(answers, ['type', 'text']) | |
var sentiment | |
request({ | |
uri: sentimentUrl, | |
method: 'POST', | |
form: { txt: explanation } | |
}).then(body => sentiment = body.result) | |
.catch(err => console.log(err)) | |
var insertQuery = 'INSERT INTO responses (rating, explanation, sentiment, confidence) VALUES ($1, $2, $3, $4)' | |
return db.query(insertQuery, [], (err, result) => { | |
if (err) { | |
return new ApiResponse({error: err.message}, {'Content-Type': 'application/json'}, 500) | |
} | |
return new ApiResponse({}, {'Content-Type': 'application/json'}, 200) | |
}) | |
})) | |
module.exports = api |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment