Permalink
Browse files

first version

  • Loading branch information...
hay committed Nov 27, 2012
0 parents commit e5500de27b6d73b06a60f23c6ca331e8d63691d0
Showing with 171 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +53 −0 README.md
  3. +114 −0 disqus.js
@@ -0,0 +1,4 @@
+api.js
+node_modules
+test.html
+web
@@ -0,0 +1,53 @@
+# Disqus - An API module for Node.js
+
+This module allows you to use the [Disqus API](http://disqus.com/api/docs/). The module is written for version 3.0 of the API and uses the 'Legacy admin authentication' instead of OAuth.
+
+## Install
+
+ npm install disqus
+
+## Use
+
+### Setup
+
+Here's how to set it up:
+
+ var Disqus = require('disqus');
+
+ var disqus = new Disqus({
+ api_secret : 'your_api_secret',
+ api_key : 'your_api_key',
+ access_token : 'your_access_token'
+ });
+
+From then on you can use the `request` method to make calls to the API. Note that the module knows if it should use GET or POST, so you don't need to worry about that.
+
+### Request method
+
+The `request` method has three parameters:
+
+ disqus.request(resource, parameters, callback);
+
+`resource` maps to one of the API methods, so for example the [list method for posts](http://disqus.com/api/docs/posts/list/) is written as `posts/list` in the argument.
+
+`parameters` are required but can an empty object (`{}`) if you don't have any.
+
+`callback` will contain either the response of the call if it went successful. If you got an error, it will contain an `error` property with error information.
+
+### Example
+
+Here's an example that will list all posts from a forum called 'foo'. This example requires that the `disqus` object is already setup with your credentials, as shown in the 'setup' example above.
+
+ disqus.request('posts/list', { forum : 'foo'}, function(data) {
+ if (data.error) {
+ console.log('Something went wrong...');
+ } else {
+ console.log(data);
+ }
+ });
+
+## Notes
+* This module is relased under the terms of the [MIT license](http://opensource.org/licenses/MIT).
+* [Source on Github](http://github.com/hay/node-disqus)
+* Written by [Hay Kranen](http://www.haykranen.nl)
+* [Follow me on Twitter](http://twitter.com/hayify)
114 disqus.js
@@ -0,0 +1,114 @@
+var _ = require('underscore');
+var util = require('util');
+var request = require('request');
+var querystring = require('querystring');
+
+// These methods require POST instead of GET
+var resourcesRequiringPost = [
+ 'blacklists/add',
+ 'blacklists/remove',
+ 'categories/create',
+ 'exports/exportForum',
+ 'forums/addModerator',
+ 'forums/create',
+ 'forums/removeModerator',
+ 'posts/approve',
+ 'posts/create',
+ 'posts/highlight',
+ 'posts/remove',
+ 'posts/report',
+ 'posts/restore',
+ 'posts/spam',
+ 'posts/unhighlight',
+ 'posts/update',
+ 'posts/vote',
+ 'reactions/remove',
+ 'reactions/restore',
+ 'threads/close',
+ 'threads/create',
+ 'threads/open',
+ 'threads/remove',
+ 'threads/restore',
+ 'threads/subscribe',
+ 'threads/unsubscribe',
+ 'threads/update',
+ 'threads/vote',
+ 'users/checkUsername',
+ 'users/follow',
+ 'users/unfollow',
+ 'whitelists/add',
+ 'whitelists/remove'
+];
+
+function Disqus( conf ) {
+ this.access_token = conf.access_token;
+ this.api_key = conf.api_key;
+ this.api_secret = conf.api_secret;
+}
+
+Disqus.prototype = {
+ API_ROOT : 'https://disqus.com/api/3.0/%s.json',
+
+ _getHttpVerb : function(resource) {
+ return resourcesRequiringPost.indexOf(resource) !== -1 ? 'post' : 'get';
+ },
+
+ _handleResponse : function(error, response, body, callback) {
+ if (response.statusCode === 200) {
+ return body;
+ } else {
+ return {
+ error : {
+ body : body,
+ error : error,
+ response : response
+ }
+ };
+ }
+ },
+
+ getRequestUrl : function(resource, params) {
+ params = params || {};
+
+ resource = resource.toLowerCase();
+
+ // Always extend with the authentication stuff
+ _.extend(params, {
+ api_secret : this.api_secret,
+ api_key : this.api_key,
+ access_token : this.access_token
+ });
+
+ var url = util.format(this.API_ROOT, resource);
+ url += '?' + querystring.stringify( params );
+
+ return url;
+ },
+
+ request : function(resource, params, callback ) {
+ params = params || {};
+
+ var method = this._getHttpVerb(resource);
+
+ if (method === 'get') {
+ url = this.getRequestUrl(resource, params);
+
+ request.get(url, function() {
+ callback( this._handleResponse.apply(this, arguments) );
+ }.bind(this));
+ }
+
+ if (method === 'post') {
+ url = this.getRequestUrl(resource);
+
+ request.post({
+ url : url,
+ body : querystring.stringify( params )
+ }, function() {
+ callback( this._handleResponse.apply(this, arguments) );
+ }.bind(this));
+ }
+ }
+};
+
+module.exports = Disqus;

0 comments on commit e5500de

Please sign in to comment.