Skip to content
Browse files

changed callbacks

  • Loading branch information...
1 parent bc522e1 commit 9c47af9a4f3b9419c25c86fe4c1439fb3fbbae2a @pdfcrowd committed Apr 28, 2011
Showing with 18 additions and 30 deletions.
  1. +18 −30 lib/pdfcrowd.js
View
48 lib/pdfcrowd.js
@@ -13,44 +13,24 @@ var Pdfcrowd = function(username, apikey) {
this.username = username;
this.apikey = apikey;
-
- this.callbacks = {
- error: function(status, err) {
- console.warn("WARNING: [Pdfcrowd] Unhandled error: %d - %s", status, err);
- },
- pdf: function(rstream) {
- rstream.destroy();
- console.log("WARNING: [Pdfcrowd] Unhandled PDF generation.");
- }
- };
-};
-
-
-
-Pdfcrowd.prototype.on = function(event, callback) {
-
- if (event !== 'error' && event !== 'pdf')
- throw new Error("Supported events: pdf, error")
-
- this.callbacks[event] = callback;
};
-Pdfcrowd.prototype.convertHtml = function(html, options) {
- convertInternal(this, html, options, '/api/pdf/convert/html/');
+Pdfcrowd.prototype.convertHtml = function(html, callbacks, options) {
+ convertInternal(this, html, '/api/pdf/convert/html/', callbacks, options);
}
-Pdfcrowd.prototype.convertURI = function(uri, options) {
- convertInternal(this, uri, options, '/api/pdf/convert/uri/');
+Pdfcrowd.prototype.convertURI = function(uri, callbacks, options) {
+ convertInternal(this, uri, '/api/pdf/convert/uri/', callbacks, options);
}
-Pdfcrowd.prototype.convertFile = function(fname, options) {
+Pdfcrowd.prototype.convertFile = function(fname, callbacks, options) {
var that = this;
fs.readFile(fname, function(err, data) {
if (err) throw err;
- convertInternal(that, data.toString(), options, '/api/pdf/convert/html/');
+ convertInternal(that, data.toString(), '/api/pdf/convert/html/', callbacks, options);
});
}
@@ -62,7 +42,7 @@ module.exports = Pdfcrowd;
// private
-var convertInternal = function(that, src, options, endpoint) {
+var convertInternal = function(that, src, endpoint, callbacks, options) {
// form POST data
var postData = clone_object(options);
@@ -84,21 +64,29 @@ var convertInternal = function(that, src, options, endpoint) {
var req = http.request(httpOptions, function(res) {
if (res.statusCode < 300) {
- that.callbacks['pdf'](res);
+ res.on('end', function() {
+ callbacks.end();
+ });
+
+ res.on('error', function(exc) {
+ callbacks.error(exc.toString());
+ });
+
+ callbacks.pdf(res);
}
else {
var err = [];
res.on('data', function(chunk) {
err.push(chunk.toString());
});
res.on('end', function() {
- that.callbacks['error'](err.join(''), res.statusCode);
+ callbacks.error(err.join(''), res.statusCode);
});
}
});
req.on('error', function(res) {
- that.callbacks['error'](res.toString(), res.statusCode);
+ callbacks.error(res.toString(), res.statusCode);
});
req.write(postData);

0 comments on commit 9c47af9

Please sign in to comment.
Something went wrong with that request. Please try again.