-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
220 lines (169 loc) · 4.9 KB
/
index.js
File metadata and controls
220 lines (169 loc) · 4.9 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"use strict";
const request = require('request')
, qs = require('qs')
, _ = require('underscore');
const util = require('util');
function WP_Node() {
//Defaults
this.TTL = 86400;
this.logger = false;
this.endpoint = '';
this.db = null;
this.endpoint = '';
this.url = '';
this.secret = '12345';
this.flush_all = false;
}
WP_Node.prototype._isEmptyObject = function(obj) {
return !Object.keys(obj).length;
}
WP_Node.prototype.log = function(msg) {
if (this.logger)
console.log(msg);
}
WP_Node.prototype.setGlobalOptions = function(options) {
for(var key in options)
this[key] = options[key];
}
WP_Node.prototype.generateSiteMap = function(options, cb){
var self = this
, options = options || {}
, endpoint = options.endpoint || self.endpoint
, pre_link = options.pre_link || ''
, priority = options.priority || 0.5
, sitemap = [];
if (!endpoint
|| typeof endpoint === 'undefined') {
cb({
code : 120,
message : 'A WordPress endpoint is required to generate a WP Site Map'
}, null
);
return;
}
self.log('Generating WordPress sitemap from ' + endpoint);
request({
url : endpoint,
qs : {
post_type : 'post'
}
}, function(e, r, posts){
if (typeof posts === 'string')
posts = JSON.parse(posts);
_.each(posts, function(post){
sitemap.push({
url : pre_link + '/' + post.slug + '/' + post.id,
changefreq : 'daily',
priority : priority
});
});
cb(e, sitemap);
})
}
WP_Node.prototype.cache = function(options, fn) {
var self = this;
var TTL = options.TTL || self.TTL;
self.log('Cache TTL is ' + TTL);
var url = options.url || self.endpoint
, db = options.db || self.db
, _qs = options.qs || {}
, _id = '';
if (!self._isEmptyObject(_qs)) {
self.log('_qs defined');
_id = qs.stringify(_qs);
}
if (!url) {
fn(
null, {
code : 100,
message : "No URL endpoint provided."
}
);
return;
}
_id = url + ( (url.indexOf('?') > -1) ? _id : '?' + _id );
self.log('Final ID to mongo is: ' + _id);
//Let's force the wordpress feed channel
if (options.forceWPJSONFeed)
_qs.feed = 'json';
//Go Straight to mongo
db.collection('cache', function(err, collection) {
collection.findOne({ id: _id, invalid : undefined }, function(err, item) {
if (!item) {
self.log('Getting fresh content for ' + _id);
self.processRequest({
request:{
url: url,
qs: _qs
},
id: _id,
db: db,
callback: fn
});
} else {
//Check if we are over the cache limit
var currentTime = +new Date();
if ( ((currentTime - item.wp_timestamp)/1000) > TTL) {
self.log('Removing and getting fresh content for ' + _id);
db.collection('cache', function(err, collection) {
collection.update({ id: _id, invalid : undefined }, { $set : { invalid : true } }, {multi : true}, function(err, result) {
if (err)
self.log(err);
self.processRequest({
request:{
url: url,
qs: _qs
},
id: _id,
db: db,
callback: fn
});
})
});
} else {
self.log('Getting cache for ' + _id);
self.log(item.wp_timestamp);
fn(item.content, null);
}
}
});
});
}
WP_Node.prototype.processRequest = function(obj) {
// request start
var self = this;
request.get(obj.request, function(e, r, b){
try {
b = JSON.parse(b);
} catch (ex) {
self.log(ex);
//TODO: This need to change to our error model
obj.callback({error: ex});
return;
}
var cache_object = {
id : obj.id,
content : b,
wp_timestamp : +new Date()
}
obj.db.collection('cache', function(err, collection) {
collection.insert(cache_object, { safe:true }, function(err, result) {
if (err) {
self.log(err);
obj.callback(
null, {
code : 110,
message : "MongoDB error",
extra_information : err.code
}
);
} else {
obj.callback(cache_object.content, null);
}
});
});
}); //request end
}
var finalObject = new WP_Node();
finalObject.wordpress = require('./lib/wordpress.js')(finalObject);
module.exports = finalObject;