Browse files

implemented error callback for send_message

  • Loading branch information...
1 parent fe0dcfe commit 7ce075732d6c444315d793e1ca9c7dbe43982bd7 @derek-watson derek-watson committed Feb 14, 2013
Showing with 60 additions and 24 deletions.
  1. +20 −17 README.md
  2. +33 −6 lib/postageapp.js
  3. +7 −1 test/postageapp.js
View
37 README.md
@@ -39,15 +39,15 @@ Sending a Message
When you require the library, make sure to specify your API key:
var postageapp = require('postageapp')('YOUR API KEY HERE');
-
+
After that, you should be good to go. Load the module in your app and call the `sendMessage` function. Here is a sample of how to use it:
var postageapp = require('postageapp')('YOUR API KEY HERE');
postageapp.sendMessage(options, function callback() {});
The `options` parameter on the `sendMessage()` function is a hash that contains all of the arguments that you will be using in your API call. Here is an example API call:
- var options = {
+ var options = {
recipients: "email@address.com",
subject: "Subject Line",
@@ -60,55 +60,58 @@ The `options` parameter on the `sendMessage()` function is a hash that contains
}
You can use any of the arguments available to [send_message.json](http://help.postageapp.com/kb/api/send_message) when creating this hash.
-
+
Recipients can be passed along as a single string or as an array.
recipients: "email@address.com";
recipients: ["email1@address.com", "email2@address.com"];
-
+
If you wish to set Message Variables for each individual recipient, you just have to pass an array for each recipient.
recipients: {
"email@example.com": {
'variable': 'Value'
- },
+ },
"email2@example.com": {
'variable': 'Another Value'
}
};
-
+
Content will accept an array for HTML and plain text content.
content: {
'text/html': '<strong>Sample bold content.</strong>',
'text/plain': 'Plain text goes here'
};
-
+
Subject and from can be simple strings.
subject: 'Subject Line';
from: 'sender@example.org';
-
+
Templates can be called by using the template slug from your PostageApp Projects.
template: 'sample_template';
-
+
Message Variables needs to have an array passed into it with the global variable names and values.
variables: {
'variable': 'Variable value',
'variable2': 'Another variable'
};
-Using the Callback Function
+Success and Error Callbacks
-----
-The `sendMessage()` function in the PostageApp Node.JS plugin includes a callback argument. Callbacks are used to tell the PostageApp Node.JS plugin to execute some other code as soon as it is finished with sending your emails to PostageApp.
+The `sendMessage()` function takes two callback arguments - success and error. Callbacks are used to tell the PostageApp Node.JS plugin to execute some other code as soon as it is finished with sending your emails to PostageApp (or when an error occurs).
var postageapp = require('postageapp')('YOUR API KEY HERE');
- postageapp.sendMessage(options, function callback() {
- // Additional code to execute after mail is delivered
+ postageapp.sendMessage(options, function (response, object) {
+ console.log('HTTP Status code: ', response.statusCode);
+ console.log('Message UID', object.response.uid);
+ }, function (message, object) {
+ console.log('Ack! An error has occurred: ', message);
});
-
+
Getting Account Info
-----
You can get your PostageApp account info through the Node.JS plugin by using the `acountInfo()` function, which can be used as such:
@@ -127,12 +130,12 @@ You can the status of an individual message sent through PostageApp using the UI
The `options` parameter in the `messageStatus()` function is a hash that contains one thing: the UID of the message you wish you retrieve.
- var options = {
+ var options = {
desiredUID: 'message UID here',
}
You will receive a JSON string back from the API server that should look something like this:
{"response":{"status":"ok","uid":"message UID here"},"data":{"message_status":{"completed":1}}}
-
-For more information about formatting of recipients, templates and variables please see the [PostageApp documentation](http://help.postageapp.com/kb/api/send_message).
+
+For more information about formatting of recipients, templates and variables please see the [PostageApp documentation](http://help.postageapp.com/kb/api/send_message).
View
39 lib/postageapp.js
@@ -7,7 +7,7 @@ module.exports = function(apiKey) {
return {
version: function() { return postageVersion; },
- sendMessage: function (options, callback) {
+ sendMessage: function (options, success, error) {
var recipients = options.recipients;
var subject = options.subject;
@@ -19,6 +19,10 @@ module.exports = function(apiKey) {
var template = options.template;
var variables = options.variables;
+ // no-op callback defaults
+ if (typeof(success) !== 'function') success = function() {};
+ if (typeof(error) !== 'function') error = function() {};
+
/*
Creates a string of numbers to be used for the UID, which has to be a unique identifier in order
to prevent duplicate sending through PostageApp.
@@ -63,12 +67,35 @@ module.exports = function(apiKey) {
request.on('response', function (response) {
response.setEncoding('utf8');
- if (typeof callback !== 'undefined') {
- response.on('data', function (chunk) {
- callback(null, chunk);
- });
- }
+
+ var json;
+ response.on('data', function (data) {
+
+ if (response.headers['content-type'].indexOf("application/json") === 0) {
+ json = JSON.parse(data);
+ }
+ else {
+ json = {
+ response: {
+ status: "fail",
+ message: "Received an unexpected non-json response"
+ }
+ }
+ }
+
+ if (response.statusCode >= 400 && response.statusCode <= 600) {
+ error(json.response.message, json);
+ }
+ else {
+ success(response, json);
+ }
+ });
});
+
+ request.on('error', function (err) {
+ error(err, {});
+ });
+
request.end(payload);
},
View
8 test/postageapp.js
@@ -15,10 +15,16 @@ describe('postageapp', function () {
content: "hello world",
recipients: 'test@null.postageapp.com'
}, function(throwaway, r) {
- var r = JSON.parse(r);
assert.equal('ok', r.response.status);
done();
});
});
+
+ it('should invoke the error callback for a bogus request', function(done) {
+ postageapp.sendMessage({}, function() {}, function(err, r) {
+ assert.equal('precondition_failed', r.response.status);
+ done();
+ });
+ });
});
});

0 comments on commit 7ce0757

Please sign in to comment.