-
Notifications
You must be signed in to change notification settings - Fork 0
/
wfhlib.js
184 lines (153 loc) · 4.29 KB
/
wfhlib.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';
var colors = require('colors/safe');
var marked = require('marked');
var moment = require('moment');
var request = require('request');
var Table = require('cli-table2');
var TerminalRenderer = require('marked-terminal');
marked.setOptions({
// Define custom renderer
renderer: new TerminalRenderer()
});
var displayId = function(id) {
return '(' + id + ')';
};
var getCountry = function(country) {
if (country !== undefined) {
return country.name;
}
return 'Anywhere';
};
var getUrl = function(site, uri) {
return site + uri;
};
var horiTable = function(data) {
var table = new Table({
head: data[0],
style: {compact: true}
});
if (data.length > 1) {
for (var i = 1; i < data.length; i++) {
table.push(data[i]);
}
} else {
table.push([ { colSpan: data[0].length, content: 'Oopsies, no results found' } ]);
}
return table.toString();
};
var makeRequest = function(site, uri, callback) {
request(getUrl(site, uri), function(error, response, body) {
if (!error) {
if (response.statusCode === 200) {
callback(body);
} else {
console.log('Oopsies, we got a ' + response.statusCode + ' from: ' + uri);
process.exit(1);
}
} else {
console.log('Oopsies, we got an error: ' + error);
process.exit(1);
}
});
};
var showCategories = function(body) {
var data = [];
var json = JSON.parse(body);
data.push(['ID', 'Name']);
for (var i = 0; i < json.length; i++) {
data.push([json[i].id, json[i].name]);
}
console.log(horiTable(data));
};
var showCompanies = function(body) {
var data = [];
var json = JSON.parse(body);
data.push(['ID', 'Name']);
for (var i = 0; i < json.length; i++) {
data.push([json[i].id, json[i].name]);
}
console.log(horiTable(data));
};
var showCompany = function(body) {
var data = [];
var json = JSON.parse(body);
data.push(
['Name', json.name],
['URL', json.url]
);
if (json.hasOwnProperty('country')) {
data.push(['Headquarters', json.country.name]);
}
data.push(['Twitter', json.twitter]);
console.log(vertTable(data));
};
var showJobs = function(body) {
var data = [];
var json = JSON.parse(body);
data.push(['ID', 'Posted', 'Category', 'Company', 'Title', 'Country']);
for (var i = 0; i < json.length; i++) {
var date = moment(json[i].created_at).format('YYYY-MM-DD');
data.push(
[
json[i].id,
date,
json[i].category.name + ' ' + displayId(json[i].category.id),
json[i].company.name + ' ' + displayId(json[i].company.id),
json[i].title,
getCountry(json[i].country)
]
);
}
console.log(horiTable(data));
};
var showJob = function(body) {
var data = [];
var json = JSON.parse(body);
var date = moment(json.created_at).format('YYYY-MM-DD HH:mm');
data.push(
['Title', json.title + ' @ ' + json.company.name + ' ' + displayId(json.company.id)],
['Category', json.category.name + ' ' + displayId(json.category.id)],
['Posted', date],
['Description', marked(json.description)],
['Application Info', marked(json.application_info)],
['Country', getCountry(json.country)]
);
if (json.location !== '') {
data.push(['Location', json.location]);
};
data.push(['Source', json.source.name + ' ' + displayId(json.source.id)]);
console.log(vertTable(data));
};
var showSources = function(body) {
var data = [];
var json = JSON.parse(body);
data.push(['ID', 'Name', 'URL']);
for (var i = 0; i < json.length; i++) {
data.push([json[i].id, json[i].name, json[i].url]);
}
console.log(horiTable(data));
};
// A vertical table doesn't create headers in the left-most column, so we use
// a standard horizontal table but set the colour of the left-most values to
// red.
var vertTable = function(data) {
var table = new Table({colWidths: [20, 80], wordWrap: true});
for (var i = 0; i < data.length; i++) {
table.push([colors.red(data[i][0]), data[i][1]]);
}
return table.toString();
};
module.exports = {
displayId: displayId,
getCountry: getCountry,
getUrl: getUrl,
horiTable: horiTable,
makeRequest: makeRequest,
showCategories: showCategories,
showCompanies: showCompanies,
showCompany: showCompany,
showJobs: showJobs,
showJob: showJob,
showSources: showSources,
vertTable: vertTable
};