xonecas.io
172
Dn

disqus-node / commit fa375c3

initial commit

sean2011-08-18 fa375c3c12ad31c557da4bf40ba71df3d8b31114

6 files changed

README.markdown+38−0
@@ -0,0 +1,38 @@
1+# Disqus node.js api client
2+
3+## Usage:
4+ var DISQUS = require('disqus-node'),
5+ api;
6+
7+ api = DISQUS(key, format, version, isSecure);
8+
9+ api.call(endpoint, method, options, callback);
10+
11+## Example
12+ var disqus = require('../lib/disqus-node.js'),
13+ dq;
14+
15+ dq = disqus(
16+ "PjBusPP4mtOe9VQDuDLe5DetbdLLLqjrhqosGnHrKS8JlBDpFrZg6yDnedBeL65E",
17+ 'json',
18+ '3.0',
19+ false
20+ );
21+
22+ dq.call('threads', 'list', {"forum": "xonecas"}, function (response) {
23+ console.log(response.toString());
24+
25+ response.iter(function (value, key) {
26+ console.log("\n"+ key +": "+ value.toString());
27+ });
28+
29+ console.log(response.len());
30+
31+ console.log(response.getSlice(0, 1));
32+
33+ console.log(response.getItem(0));
34+
35+ console.log(response.contains(0));
36+
37+ });
38+
index.js+1−0
@@ -0,0 +1 @@
1+module.exports = require('./lib/disqus-node.js');
lib/disqus-node.js+185−0
@@ -0,0 +1,185 @@
1+/*jshint node:true, curly:true, eqeqeq:true, onevar:true
2+ onevar:true */
3+
4+
5+var fs = require('fs'),
6+ http = require('http').request,
7+ https = require('https').request,
8+ ins = require('util').inspect,
9+ HOST = 'disqus.com',
10+ SSL_HOST = 'secure.disqus.com',
11+ INTERFACES = JSON.parse(fs.readFileSync('./lib/interfaces.json', 'utf8'));
12+
13+function APIError (code, message) {
14+ code = code || 500;
15+ message = message || "Unrecognized error";
16+ this.prototype = Error.prototype;
17+ this.name = "APIError";
18+ this.message = code +" "+ message;
19+}
20+
21+function Response (request, textResponse) {
22+ var response = JSON.parse(textResponse.replace("undefined",""));
23+
24+ this.response = response.response;
25+ this.request = request;
26+
27+ if (response.code !== 0) {
28+ throw new APIError(response.code, response.response);
29+ }
30+}
31+
32+Response.prototype = {
33+ toString: function () {
34+ return JSON.stringify(this.response);
35+ },
36+
37+ iter: function (fn) {
38+ return this.response.forEach(fn);
39+ },
40+
41+ len: function () {
42+ return this.response.length;
43+ },
44+
45+ getSlice: function (start, end) {
46+ return this.response.slice(start, end);
47+ },
48+
49+ getItem: function (key) {
50+ return this.response[key];
51+ },
52+
53+ contains: function (key) {
54+ return !!this.response[key];
55+ }
56+};
57+
58+function Request (api, endpoint, method) {
59+ var that = this;
60+
61+ function validateInterface (endpointKey, methodKey) {
62+ var endpoint = INTERFACES[endpointKey],
63+ method;
64+
65+ if (endpoint) {
66+ that.endpoint = endpointKey;
67+ method = endpoint[methodKey];
68+
69+ if (method) {
70+ that.method = methodKey;
71+ that.httpMethod = method.method;
72+ that.required = method.required;
73+ } else {
74+ throw new Error("Method not defined.");
75+ }
76+ } else {
77+ throw new Error("Interface not defined.");
78+ }
79+ }
80+
81+ this.key = api.key;
82+ this.version = api.version;
83+ this.isSecure = api.isSecure;
84+ this.format = api.format;
85+
86+ validateInterface(endpoint, method);
87+}
88+
89+Request.prototype = {
90+ call: function (params, userCallback) {
91+ var that = this,
92+ required = this.required,
93+ path = "",
94+ query = "?",
95+ key,
96+ callback;
97+
98+ if (required && required.length > 0) {
99+ this.required.forEach(function (requiredParam) {
100+ if (!params[requiredParam]) {
101+ throw new Error("Required Parameter missing.");
102+ }
103+ });
104+ }
105+
106+ path = "/api/" +
107+ [this.version, this.endpoint, this.method].join('/') +
108+ '.' + this.format;
109+
110+ for (key in params) {
111+ if (Object.prototype.hasOwnProperty.call(params, key)) {
112+ query += key +'='+ params[key] + '&';
113+ }
114+ }
115+ query += 'api_key='+ this.key;
116+
117+ options = {
118+ "path": path + query,
119+ "method": this.httpMethod,
120+ "headers": {
121+ "user-agent" : "disqus-node/"+this.version,
122+ "content-length" : 0
123+ }
124+ };
125+
126+ callback = function (response) {
127+ var body;
128+
129+ response.on('data', function (chunk) {
130+ body += chunk;
131+ });
132+
133+ response.on('end', function () {
134+ userCallback(new Response(that, body));
135+ });
136+ };
137+
138+ if (this.isSecure) {
139+ options.host = SSL_HOST;
140+ options.port = 443;
141+
142+ request = https(options, callback);
143+ } else {
144+ options.host = HOST;
145+ options.port = 80;
146+
147+ request = http(options, callback);
148+ }
149+ request.end();
150+ }
151+};
152+
153+function DISQUS (key, format, version, isSecure) {
154+ this.key = key;
155+ this.format = format || 'json';
156+ this.version = version || '3.0';
157+ this.isSecure = isSecure;
158+}
159+
160+DISQUS.prototype = {
161+ call: function (node, method, params, cb) {
162+ var resource = new Request(this, node, method);
163+ resource.call(params, cb);
164+ },
165+
166+ setKey: function (newKey) {
167+ this.key = newKey;
168+ },
169+
170+ setFormat: function (newFormat) {
171+ this.format = newFormat;
172+ },
173+
174+ setVersion: function (newVersion) {
175+ this.version = newVersion;
176+ },
177+
178+ setSecure: function (isSecure) {
179+ this.isSecure = isSecure;
180+ }
181+};
182+
183+module.exports = function (key, format, version, isSecure) {
184+ return new DISQUS(key, format, version, isSecure);
185+};
lib/interfaces.json+715−0
@@ -0,0 +1,715 @@
1+{
2+ "reactions": {
3+ "restore": {
4+ "required": [
5+ "reaction",
6+ "forum"
7+ ],
8+ "method": "POST",
9+ "formats": [
10+ "json",
11+ "jsonp"
12+ ]
13+ },
14+ "list": {
15+ "required": [
16+ "forum"
17+ ],
18+ "method": "POST",
19+ "formats": [
20+ "json",
21+ "jsonp"
22+ ]
23+ },
24+ "details": {
25+ "required": [
26+ "reaction",
27+ "forum"
28+ ],
29+ "method": "GET",
30+ "formats": [
31+ "json",
32+ "jsonp"
33+ ]
34+ },
35+ "remove": {
36+ "required": [
37+ "reaction",
38+ "forum"
39+ ],
40+ "method": "POST",
41+ "formats": [
42+ "json",
43+ "jsonp"
44+ ]
45+ }
46+ },
47+ "exports": {
48+ "exportForum": {
49+ "required": [
50+ "forum"
51+ ],
52+ "method": "POST",
53+ "formats": [
54+ "json",
55+ "jsonp"
56+ ]
57+ }
58+ },
59+ "users": {
60+ "listActiveForums": {
61+ "required": [],
62+ "method": "GET",
63+ "formats": [
64+ "json",
65+ "jsonp"
66+ ]
67+ },
68+ "listActiveThreads": {
69+ "required": [],
70+ "method": "GET",
71+ "formats": [
72+ "json",
73+ "jsonp",
74+ "rss"
75+ ]
76+ },
77+ "listFollowing": {
78+ "required": [],
79+ "method": "GET",
80+ "formats": [
81+ "json",
82+ "jsonp"
83+ ]
84+ },
85+ "listForums": {
86+ "required": [],
87+ "method": "GET",
88+ "formats": [
89+ "json",
90+ "jsonp"
91+ ]
92+ },
93+ "unfollow": {
94+ "required": [],
95+ "method": "POST",
96+ "formats": [
97+ "json",
98+ "jsonp"
99+ ]
100+ },
101+ "listPosts": {
102+ "required": [],
103+ "method": "GET",
104+ "formats": [
105+ "json",
106+ "jsonp",
107+ "rss"
108+ ]
109+ },
110+ "details": {
111+ "required": [],
112+ "method": "GET",
113+ "formats": [
114+ "json",
115+ "jsonp"
116+ ]
117+ },
118+ "listFollowers": {
119+ "required": [],
120+ "method": "GET",
121+ "formats": [
122+ "json",
123+ "jsonp"
124+ ]
125+ },
126+ "follow": {
127+ "required": [],
128+ "method": "POST",
129+ "formats": [
130+ "json",
131+ "jsonp"
132+ ]
133+ },
134+ "listActivity": {
135+ "required": [],
136+ "method": "GET",
137+ "formats": [
138+ "json",
139+ "jsonp"
140+ ]
141+ },
142+ "listMostActiveForums": {
143+ "required": [],
144+ "method": "GET",
145+ "formats": [
146+ "json",
147+ "jsonp"
148+ ]
149+ }
150+ },
151+ "imports": {
152+ "list": {
153+ "required": [
154+ "forum"
155+ ],
156+ "method": "GET",
157+ "formats": [
158+ "json",
159+ "jsonp"
160+ ]
161+ },
162+ "details": {
163+ "required": [
164+ "group"
165+ ],
166+ "method": "GET",
167+ "formats": [
168+ "json",
169+ "jsonp"
170+ ]
171+ }
172+ },
173+ "posts": {
174+ "restore": {
175+ "required": [
176+ "post"
177+ ],
178+ "method": "POST",
179+ "formats": [
180+ "json",
181+ "jsonp"
182+ ]
183+ },
184+ "spam": {
185+ "required": [
186+ "post"
187+ ],
188+ "method": "POST",
189+ "formats": [
190+ "json",
191+ "jsonp"
192+ ]
193+ },
194+ "unhighlight": {
195+ "required": [
196+ "post"
197+ ],
198+ "method": "POST",
199+ "formats": [
200+ "json",
201+ "jsonp"
202+ ]
203+ },
204+ "listPopular": {
205+ "required": [],
206+ "method": "GET",
207+ "formats": [
208+ "json",
209+ "jsonp",
210+ "rss"
211+ ]
212+ },
213+ "create": {
214+ "required": [
215+ "message"
216+ ],
217+ "method": "POST",
218+ "formats": [
219+ "json",
220+ "jsonp"
221+ ]
222+ },
223+ "list": {
224+ "required": [],
225+ "method": "GET",
226+ "formats": [
227+ "json",
228+ "jsonp",
229+ "rss"
230+ ]
231+ },
232+ "remove": {
233+ "required": [
234+ "post"
235+ ],
236+ "method": "POST",
237+ "formats": [
238+ "json",
239+ "jsonp"
240+ ]
241+ },
242+ "getContext": {
243+ "required": [
244+ "post"
245+ ],
246+ "method": "GET",
247+ "formats": [
248+ "json",
249+ "jsonp",
250+ "rss"
251+ ]
252+ },
253+ "vote": {
254+ "required": [
255+ "vote",
256+ "post"
257+ ],
258+ "method": "POST",
259+ "formats": [
260+ "json",
261+ "jsonp"
262+ ]
263+ },
264+ "details": {
265+ "required": [
266+ "post"
267+ ],
268+ "method": "GET",
269+ "formats": [
270+ "json",
271+ "jsonp"
272+ ]
273+ },
274+ "report": {
275+ "required": [
276+ "post"
277+ ],
278+ "method": "POST",
279+ "formats": [
280+ "json",
281+ "jsonp"
282+ ]
283+ },
284+ "highlight": {
285+ "required": [
286+ "post"
287+ ],
288+ "method": "POST",
289+ "formats": [
290+ "json",
291+ "jsonp"
292+ ]
293+ },
294+ "approve": {
295+ "required": [
296+ "post"
297+ ],
298+ "method": "POST",
299+ "formats": [
300+ "json",
301+ "jsonp"
302+ ]
303+ }
304+ },
305+ "blacklists": {
306+ "add": {
307+ "required": [
308+ "forum"
309+ ],
310+ "method": "POST",
311+ "formats": [
312+ "json",
313+ "jsonp"
314+ ]
315+ },
316+ "list": {
317+ "required": [
318+ "forum"
319+ ],
320+ "method": "GET",
321+ "formats": [
322+ "json",
323+ "jsonp"
324+ ]
325+ },
326+ "remove": {
327+ "required": [
328+ "forum"
329+ ],
330+ "method": "POST",
331+ "formats": [
332+ "json",
333+ "jsonp"
334+ ]
335+ }
336+ },
337+ "reports": {
338+ "domains": {
339+ "required": [],
340+ "method": "GET",
341+ "formats": [
342+ "json",
343+ "jsonp"
344+ ]
345+ },
346+ "ips": {
347+ "required": [],
348+ "method": "GET",
349+ "formats": [
350+ "json",
351+ "jsonp"
352+ ]
353+ },
354+ "threads": {
355+ "required": [],
356+ "method": "GET",
357+ "formats": [
358+ "json",
359+ "jsonp"
360+ ]
361+ },
362+ "users": {
363+ "required": [],
364+ "method": "GET",
365+ "formats": [
366+ "json",
367+ "jsonp"
368+ ]
369+ }
370+ },
371+ "whitelists": {
372+ "add": {
373+ "required": [
374+ "forum"
375+ ],
376+ "method": "POST",
377+ "formats": [
378+ "json",
379+ "jsonp"
380+ ]
381+ },
382+ "list": {
383+ "required": [
384+ "forum"
385+ ],
386+ "method": "GET",
387+ "formats": [
388+ "json",
389+ "jsonp"
390+ ]
391+ },
392+ "remove": {
393+ "required": [
394+ "forum"
395+ ],
396+ "method": "POST",
397+ "formats": [
398+ "json",
399+ "jsonp"
400+ ]
401+ }
402+ },
403+ "applications": {
404+ "listUsage": {
405+ "required": [],
406+ "method": "GET",
407+ "formats": [
408+ "json",
409+ "jsonp"
410+ ]
411+ }
412+ },
413+ "trends": {
414+ "listThreads": {
415+ "required": [],
416+ "method": "GET",
417+ "formats": [
418+ "json",
419+ "jsonp",
420+ "rss"
421+ ]
422+ }
423+ },
424+ "threads": {
425+ "restore": {
426+ "required": [
427+ "thread"
428+ ],
429+ "method": "POST",
430+ "formats": [
431+ "json",
432+ "jsonp"
433+ ]
434+ },
435+ "vote": {
436+ "required": [
437+ "vote",
438+ "thread"
439+ ],
440+ "method": "POST",
441+ "formats": [
442+ "json",
443+ "jsonp"
444+ ]
445+ },
446+ "open": {
447+ "required": [
448+ "thread"
449+ ],
450+ "method": "POST",
451+ "formats": [
452+ "json",
453+ "jsonp"
454+ ]
455+ },
456+ "create": {
457+ "required": [
458+ "forum",
459+ "title"
460+ ],
461+ "method": "POST",
462+ "formats": [
463+ "json",
464+ "jsonp"
465+ ]
466+ },
467+ "list": {
468+ "required": [],
469+ "method": "GET",
470+ "formats": [
471+ "json",
472+ "jsonp",
473+ "rss"
474+ ]
475+ },
476+ "listMostLiked": {
477+ "required": [],
478+ "method": "GET",
479+ "formats": [
480+ "json",
481+ "jsonp",
482+ "rss"
483+ ]
484+ },
485+ "listHot": {
486+ "required": [],
487+ "method": "GET",
488+ "formats": [
489+ "json",
490+ "jsonp",
491+ "rss"
492+ ]
493+ },
494+ "update": {
495+ "required": [
496+ "thread"
497+ ],
498+ "method": "POST",
499+ "formats": [
500+ "json",
501+ "jsonp"
502+ ]
503+ },
504+ "listSimilar": {
505+ "required": [
506+ "thread"
507+ ],
508+ "method": "GET",
509+ "formats": [
510+ "json",
511+ "jsonp",
512+ "rss"
513+ ]
514+ },
515+ "details": {
516+ "required": [
517+ "thread"
518+ ],
519+ "method": "GET",
520+ "formats": [
521+ "json",
522+ "jsonp"
523+ ]
524+ },
525+ "listByDate": {
526+ "required": [],
527+ "method": "GET",
528+ "formats": [
529+ "json",
530+ "jsonp",
531+ "rss"
532+ ]
533+ },
534+ "listPosts": {
535+ "required": [
536+ "thread"
537+ ],
538+ "method": "GET",
539+ "formats": [
540+ "json",
541+ "jsonp",
542+ "rss"
543+ ]
544+ },
545+ "close": {
546+ "required": [
547+ "thread"
548+ ],
549+ "method": "POST",
550+ "formats": [
551+ "json",
552+ "jsonp"
553+ ]
554+ },
555+ "remove": {
556+ "required": [
557+ "thread"
558+ ],
559+ "method": "POST",
560+ "formats": [
561+ "json",
562+ "jsonp"
563+ ]
564+ },
565+ "listPopular": {
566+ "required": [],
567+ "method": "GET",
568+ "formats": [
569+ "json",
570+ "jsonp",
571+ "rss"
572+ ]
573+ }
574+ },
575+ "forums": {
576+ "create": {
577+ "required": [
578+ "website",
579+ "name",
580+ "short_name"
581+ ],
582+ "method": "POST",
583+ "formats": [
584+ "json",
585+ "jsonp"
586+ ]
587+ },
588+ "listCategories": {
589+ "required": [
590+ "forum"
591+ ],
592+ "method": "GET",
593+ "formats": [
594+ "json",
595+ "jsonp"
596+ ]
597+ },
598+ "listThreads": {
599+ "required": [
600+ "forum"
601+ ],
602+ "method": "GET",
603+ "formats": [
604+ "json",
605+ "jsonp",
606+ "rss"
607+ ]
608+ },
609+ "listUsers": {
610+ "required": [
611+ "forum"
612+ ],
613+ "method": "GET",
614+ "formats": [
615+ "json",
616+ "jsonp"
617+ ]
618+ },
619+ "listMostLikedUsers": {
620+ "required": [
621+ "forum"
622+ ],
623+ "method": "GET",
624+ "formats": [
625+ "json",
626+ "jsonp"
627+ ]
628+ },
629+ "details": {
630+ "required": [
631+ "forum"
632+ ],
633+ "method": "GET",
634+ "formats": [
635+ "json",
636+ "jsonp"
637+ ]
638+ },
639+ "listPosts": {
640+ "required": [
641+ "forum"
642+ ],
643+ "method": "GET",
644+ "formats": [
645+ "json",
646+ "jsonp",
647+ "rss"
648+ ]
649+ },
650+ "listModerators": {
651+ "required": [
652+ "forum"
653+ ],
654+ "method": "GET",
655+ "formats": [
656+ "json",
657+ "jsonp"
658+ ]
659+ }
660+ },
661+ "categories": {
662+ "listPosts": {
663+ "required": [
664+ "category"
665+ ],
666+ "method": "GET",
667+ "formats": [
668+ "json",
669+ "jsonp",
670+ "rss"
671+ ]
672+ },
673+ "listThreads": {
674+ "required": [
675+ "category"
676+ ],
677+ "method": "GET",
678+ "formats": [
679+ "json",
680+ "jsonp",
681+ "rss"
682+ ]
683+ },
684+ "create": {
685+ "required": [
686+ "forum",
687+ "title"
688+ ],
689+ "method": "POST",
690+ "formats": [
691+ "json",
692+ "jsonp"
693+ ]
694+ },
695+ "list": {
696+ "required": [],
697+ "method": "GET",
698+ "formats": [
699+ "json",
700+ "jsonp"
701+ ]
702+ },
703+ "details": {
704+ "required": [
705+ "category"
706+ ],
707+ "method": "GET",
708+ "formats": [
709+ "json",
710+ "jsonp"
711+ ]
712+ }
713+ }
714+}
715+
package.json+0−0
tests/disqus-node-test.js+30−0
@@ -0,0 +1,30 @@
1+/*jshint node:true, curly:true, eqeqeq:true, onevar:true
2+ onevar:true */
3+
4+var disqus = require('../lib/disqus-node.js'),
5+ dq;
6+
7+dq = disqus(
8+ "PjBusPP4mtOe9VQDuDLe5DetbdLLLqjrhqosGnHrKS8JlBDpFrZg6yDnedBeL65E",
9+ 'json',
10+ '3.0',
11+ false
12+);
13+
14+dq.call('threads', 'list', {"forum": "xonecas"}, function (response) {
15+ console.log(response.toString());
16+
17+ response.iter(function (value, key) {
18+ console.log("\n"+ key +": "+ value.toString());
19+ });
20+
21+ console.log(response.len());
22+
23+ console.log(response.getSlice(0, 1));
24+
25+ console.log(response.getItem(0));
26+
27+ console.log(response.contains(0));
28+
29+});
30+