/
stalkernet.js
86 lines (75 loc) · 2.2 KB
/
stalkernet.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
import qs from 'querystring'
import uniqBy from 'lodash/uniqBy'
import xml2js from 'xml2js'
import toPairs from 'lodash/toPairs'
import fromPairs from 'lodash/fromPairs'
import camelCase from 'lodash/camelCase'
import toUpper from 'lodash/toUpper'
import flatten from 'lodash/flatten'
xml2js.parseStringAsync = function(...args) {
return new Promise((resolve, reject) => {
xml2js.parseString(...args, (err, data) => {
if (err) reject(err)
resolve(data)
})
})
}
const STALKERNET_URL = 'https://www.stolaf.edu/personal/directory/index.cfm'
export function cache() {
let {AsyncStorage} = require('react-native')
let promises = ['a', 'e', 'i', 'o', 'u'].map(l => query({lastName: l}))
return Promise.all(promises).then(data => {
data = flatten(data)
data = uniqBy(
data,
item => `${item.lastName}-${item.firstName}-${item.email}`,
)
return AsyncStorage.setItem('@AllAboutOlaf:Stalkernet', data)
})
}
// cacheStalkernet().then(console.log)
function buildStalkernetQueryString(query) {
let q = {
fuseaction: 'SearchResults',
format: 'xml',
}
if (query['lastName']) {
q['lastname'] = query.lastName
}
if (query['firstName']) {
q['firstname'] = query.firstName
}
if (query['email']) {
q['email'] = query.email
}
return qs.stringify(q)
}
function simplifySingleStalkernetResult(person) {
let pairs = toPairs(person)
// camel-case the keys (the come in uppercased)
// and return the first element of the arrays
// (xml2js returns tag contents as arrays)
.map(([key, val]) => [camelCase(key), val[0]])
// and remove the keys with no value
.filter(pair => pair[1].trim())
return fromPairs(pairs)
}
function removeSillyStalkernetResults(person) {
return toUpper(person.lastName) !== person.lastName
}
function parseStalkernetResults(data) {
return xml2js
.parseStringAsync(data)
.then(results => results['Results']['Person'])
.then(people => people.map(simplifySingleStalkernetResult))
.then(people => people.filter(removeSillyStalkernetResults))
}
export function query(query) {
let queryString = buildStalkernetQueryString(query)
return fetch(`${STALKERNET_URL}?${queryString}`)
.then(r => r.text())
.then(parseStalkernetResults)
.catch(err => {
console.error(err)
})
}