Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sacenox committed Aug 19, 2011
0 parents commit fa375c3
Show file tree
Hide file tree
Showing 6 changed files with 969 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.markdown
@@ -0,0 +1,38 @@
# Disqus node.js api client

## Usage:
var DISQUS = require('disqus-node'),
api;

api = DISQUS(key, format, version, isSecure);

api.call(endpoint, method, options, callback);

## Example
var disqus = require('../lib/disqus-node.js'),
dq;

dq = disqus(
"PjBusPP4mtOe9VQDuDLe5DetbdLLLqjrhqosGnHrKS8JlBDpFrZg6yDnedBeL65E",
'json',
'3.0',
false
);

dq.call('threads', 'list', {"forum": "xonecas"}, function (response) {
console.log(response.toString());

response.iter(function (value, key) {
console.log("\n"+ key +": "+ value.toString());
});

console.log(response.len());

console.log(response.getSlice(0, 1));

console.log(response.getItem(0));

console.log(response.contains(0));
});

1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = require('./lib/disqus-node.js');
185 changes: 185 additions & 0 deletions lib/disqus-node.js
@@ -0,0 +1,185 @@
/*jshint node:true, curly:true, eqeqeq:true, onevar:true
onevar:true */


var fs = require('fs'),
http = require('http').request,
https = require('https').request,
ins = require('util').inspect,
HOST = 'disqus.com',
SSL_HOST = 'secure.disqus.com',
INTERFACES = JSON.parse(fs.readFileSync('./lib/interfaces.json', 'utf8'));

function APIError (code, message) {
code = code || 500;
message = message || "Unrecognized error";
this.prototype = Error.prototype;
this.name = "APIError";
this.message = code +" "+ message;
}

function Response (request, textResponse) {
var response = JSON.parse(textResponse.replace("undefined",""));

this.response = response.response;
this.request = request;

if (response.code !== 0) {
throw new APIError(response.code, response.response);
}
}

Response.prototype = {
toString: function () {
return JSON.stringify(this.response);
},

iter: function (fn) {
return this.response.forEach(fn);
},

len: function () {
return this.response.length;
},

getSlice: function (start, end) {
return this.response.slice(start, end);
},

getItem: function (key) {
return this.response[key];
},

contains: function (key) {
return !!this.response[key];
}
};

function Request (api, endpoint, method) {
var that = this;

function validateInterface (endpointKey, methodKey) {
var endpoint = INTERFACES[endpointKey],
method;

if (endpoint) {
that.endpoint = endpointKey;
method = endpoint[methodKey];

if (method) {
that.method = methodKey;
that.httpMethod = method.method;
that.required = method.required;
} else {
throw new Error("Method not defined.");
}
} else {
throw new Error("Interface not defined.");
}
}

this.key = api.key;
this.version = api.version;
this.isSecure = api.isSecure;
this.format = api.format;

validateInterface(endpoint, method);
}

Request.prototype = {
call: function (params, userCallback) {
var that = this,
required = this.required,
path = "",
query = "?",
key,
callback;

if (required && required.length > 0) {
this.required.forEach(function (requiredParam) {
if (!params[requiredParam]) {
throw new Error("Required Parameter missing.");
}
});
}

path = "/api/" +
[this.version, this.endpoint, this.method].join('/') +
'.' + this.format;

for (key in params) {
if (Object.prototype.hasOwnProperty.call(params, key)) {
query += key +'='+ params[key] + '&';
}
}
query += 'api_key='+ this.key;

options = {
"path": path + query,
"method": this.httpMethod,
"headers": {
"user-agent" : "disqus-node/"+this.version,
"content-length" : 0
}
};

callback = function (response) {
var body;

response.on('data', function (chunk) {
body += chunk;
});

response.on('end', function () {
userCallback(new Response(that, body));
});
};

if (this.isSecure) {
options.host = SSL_HOST;
options.port = 443;

request = https(options, callback);
} else {
options.host = HOST;
options.port = 80;

request = http(options, callback);
}
request.end();
}
};

function DISQUS (key, format, version, isSecure) {
this.key = key;
this.format = format || 'json';
this.version = version || '3.0';
this.isSecure = isSecure;
}

DISQUS.prototype = {
call: function (node, method, params, cb) {
var resource = new Request(this, node, method);
resource.call(params, cb);
},

setKey: function (newKey) {
this.key = newKey;
},

setFormat: function (newFormat) {
this.format = newFormat;
},

setVersion: function (newVersion) {
this.version = newVersion;
},

setSecure: function (isSecure) {
this.isSecure = isSecure;
}
};

module.exports = function (key, format, version, isSecure) {
return new DISQUS(key, format, version, isSecure);
};

0 comments on commit fa375c3

Please sign in to comment.