This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (97 loc) · 2.77 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var pjson = require('./package.json');
var http = require('http')
var request = require('request')
var bodyParser = require('body-parser')
var fs = require ('fs')
var marked = require('marked');
var express = require('express')
var app = express()
var host = process.env.VCAP_APP_HOST || 'localhost'
var port = process.env.VCAP_APP_PORT || 1337
app.use(express.static('public'));
app.set('views', './views')
app.set('view engine', 'jade');
app.get('/', function(req, res) {
// res.setHeader('Content-Type', 'text/html')
var path = __dirname + '/README.md';
var file = fs.readFile(path, 'utf8', function(err, data) {
if(err) {
console.log(err);
}
res.render('index', { title: 'slack-uinames', repository: pjson.repository.url, markdown: marked(data.toString())});
});
})
app.post('/slack', bodyParser.urlencoded({extended: true}), function(req, res) {
res.setHeader('Content-Type', 'text/plain')
var text = req.body.text
var qs = {}
if (text) {
var args = text.split(/\s+/)
if (args.length > 0) {
if (!isNaN(args[0])) {
qs.amount = Math.min(100, +args.shift())
}
}
if (args.length > 0) {
if (args[0] === "male" || args[0] === "female") {
qs.gender = args.shift()
var omitGender = true
}
}
if (args.length > 0) {
qs.country = args.join(" ")
var omitCountry = true
}
}
console.log("Requesting names from uinames.com. Options: " + JSON.stringify(qs))
request({
uri: 'http://api.uinames.com',
json: true,
qs: qs
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (body.error) {
if (body.error === "No matching pool found") {
res.send("The country '" + qs.country + "' couldn't be found.")
} else {
res.send("There was an error talking to uinames.com (" + body.error + ")")
}
} else {
if (Array.isArray(body)) {
res.send(body.map(function(person) {
return formatPerson(person, omitGender, omitCountry)
}).join("\n"))
} else {
res.send(formatPerson(body, omitGender, omitCountry))
}
}
}
})
})
function formatPerson(person, omitGender, omitCountry) {
var additionalInfo = ""
if (!omitGender || !omitCountry) {
additionalInfo += " ("
if (!omitGender) {
if (person.gender === "male") {
var genderSymbol = "\u2642"
} else {
var genderSymbol = "\u2640"
}
additionalInfo += genderSymbol
}
if (!omitGender && !omitCountry) {
additionalInfo += " "
}
if (!omitCountry) {
var country = person.country.toLowerCase().replace( /\b\w/g, function (m) {
return m.toUpperCase()
})
additionalInfo += country
}
additionalInfo += ")"
}
return person.name + " *" + person.surname + "*" + additionalInfo
}
app.listen(port)
console.log('Server running at http://' + host + ':' + port + '/')