Permalink
Browse files

Made sendMessage take a callback function, so it can be used asynchro…

…nously, and the response can be captured. Left error always "null" for now.
  • Loading branch information...
1 parent 0bb4131 commit fb02d65db25f4258ac62c7b3d1641e9985a7f5ad skendall committed Feb 20, 2012
Showing with 82 additions and 86 deletions.
  1. +82 −86 lib/postageapp.js
View
@@ -1,94 +1,90 @@
var http = require('http');
-var fs = require('fs');
var postageVersion = '1.0.0';
module.exports = function(apiKey) {
- return {
- sendMessage: function (options) {
- var api = http.createClient(80, 'api.postageapp.com');
-
- var request = api.request('POST', '/v.1.0/send_message.json',
- {
- 'host': 'api.postageapp.com',
- 'content-type': 'application/json',
- 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')'
- });
-
- var recipients = options.recipients;
-
- var subject = options.subject;
- var from = options.from;
-
- var content = options.content;
-
- var template = options.template;
- var variables = options.variables;
-
- /*
- 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.
- */
- var date = new Date;
- var epochDate = date.getTime();
-
- /*
- Payload is the aggregated data that will be passed to the API server including all of the parameters
- required to send an email through PostageApp.
- */
- var payload = {
- api_key: apiKey,
- uid: epochDate,
- arguments: {
- recipients: recipients,
-
- headers: {
- subject: subject,
- from: from
- },
-
- content: content,
-
- template: template,
-
- variables: variables,
-
+ return {
+ sendMessage: function (options, callback) {
+ var api = http.createClient(80, 'api.postageapp.com');
+
+ var request = api.request('POST', '/v.1.0/send_message.json',
+ {
+ 'host': 'api.postageapp.com',
+ 'content-type': 'application/json',
+ 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')'
+ });
+
+ var recipients = options.recipients;
+
+ var subject = options.subject;
+ var from = options.from;
+
+ var content = options.content;
+
+ var template = options.template;
+ var variables = options.variables;
+
+ /*
+ 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.
+ */
+ var date = new Date;
+ var epochDate = date.getTime();
+
+ /*
+ Payload is the aggregated data that will be passed to the API server including all of the parameters
+ required to send an email through PostageApp.
+ */
+ var payload = {
+ api_key: apiKey,
+ uid: epochDate,
+ arguments: {
+ recipients: recipients,
+
+ headers: {
+ subject: subject,
+ from: from
+ },
+
+ content: content,
+
+ template: template,
+
+ variables: variables
+ }
+ };
+
+ request.on('response', function (response) {
+ response.setEncoding('utf8');
+ response.on('data', function (chunk) {
+ callback(null, chunk);
+ });
+ });
+ request.end(JSON.stringify(payload));
+ },
+
+ accountInfo: function () {
+ var api = http.createClient(80, 'api.postageapp.com');
+
+ var request = api.request('POST', '/v.1.0/get_account_info.json',
+ {
+ 'host': 'api.postageapp.com',
+ 'content-type': 'application/json',
+ 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')'
+ });
+
+ var payload = {
+ api_key: apiKey
+ };
+
+ request.on('response', function (response) {
+ console.log('STATUS: ' + response.statusCode);
+ response.setEncoding('utf8');
+ response.on('data', function (chunk) {
+ console.log('BODY: ' + chunk);
+ });
+ });
+ request.end(JSON.stringify(payload));
}
- }
-
- request.on('response', function (response) {
- console.log('STATUS: ' + response.statusCode);
- response.setEncoding('utf8');
- response.on('data', function (chunk) {
- console.log('BODY: ' + chunk);
- });
- });
- request.end(JSON.stringify(payload));
-
- },
-
- accountInfo: function () {
- var api = http.createClient(80, 'api.postageapp.com');
-
- var request = api.request('POST', '/v.1.0/get_account_info.json',
- {
- 'host': 'api.postageapp.com',
- 'content-type': 'application/json',
- 'user-agent': 'PostageApp Node.JS ' + postageVersion + ' (Node.JS ' + process.version + ')'
- });
-
- var payload = {
- api_key: apiKey
- }
-
- request.on('response', function (response) {
- console.log('STATUS: ' + response.statusCode);
- response.setEncoding('utf8');
- response.on('data', function (chunk) {
- console.log('BODY: ' + chunk);
- });
- });
- request.end(JSON.stringify(payload));
}
- }
};

0 comments on commit fb02d65

Please sign in to comment.