xonecas.io
172
Dn

disqus-node / files

disqus-node / lib / disqus-node.js

4.4 KB
1/*jshint node:true, curly:true, eqeqeq:true, onevar:true
2   onevar:true */
3
4
5var 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
13function 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
21function 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
32Response.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
58function 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
89Request.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
153function 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
160DISQUS.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
183module.exports = function (key, format, version, isSecure) {
184   return new DISQUS(key, format, version, isSecure);
185};