-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.js
97 lines (85 loc) · 2.71 KB
/
news.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
const https = require('https');
const api = 'https://corporateclash.net/api/v1/'; // Subject to change in the future...? Yell at me if this breaks
/**
* Creates new News entry.
*
* @class
*/
class NewsEntry {
/** @lends NewsEntry.prototype */
constructor(entry) {
/** @constructs */
this.id = entry.id || 0;
this.author = entry.author || 'Unknown';
this.posted = entry.posted || 0;
this.image = entry.image_url || 'https://sitecdn.corporateclash.net/logo1.1/icon-focused-300x300.png';
this.title = entry.title || 'Unknown';
this.summary = entry.summary || "A very special news entry... though we don't know what it's about!";
this.category = entry.category || 'Unknown';
this.url = `https://corporateclash.net/news/article/${this.id || 3}`;
}
// News entries are static, so there's nothing to update with them. We will, however, set up a getter for the URL!
/**
* Returns the URL to the news entry.
*
* @returns {string} URL to news entry
*/
get entryURL() {
return this.url;
}
}
/**
* Base class for getting news entries
*
* @class
*/
class News {
/** @lends News.prototype */
constructor(customUserAgent) {
/** @constructs */
this.rawList = [];
this.newsList = [];
this.agent = customUserAgent || "CorporateClash.JS/1.0.1";
}
/**
* Gets the current news entries from the Corporate Clash API.
*
* @returns {Promise<Object[]>} Array of news entries from Corporate Clash's API.
*/
getEntries = () => {
const newsURL = api + 'launcher/news';
this.rawList = [];
let entries = '';
return new Promise(resolve => {
https.get(newsURL, {
headers: {
'User-Agent': this.agent
}
}, res => {
res.on('data', data => {
entries += data;
});
res.on('end', () => {
if(!res.complete) throw new Error("Couldn't complete call to News API. Is Corporate Clash online?");
this.rawList = JSON.parse(entries);
resolve(this.rawList);
});
});
});
}
/**
* Parses the raw entries into programmer-friendly array.
*
* @returns {Promise<NewsEntry[]>} Array of programmer-friendly news entries
*/
parseEntries = () => {
this.newsList = [];
return new Promise(resolve => {
for(const entry of this.rawList) {
this.newsList.push(new NewsEntry(entry));
}
resolve(this.newsList);
})
}
}
module.exports = News;