Skip to content

Commit 8ff8b42

Browse files
Refactor response functions
1 parent b5fc4e4 commit 8ff8b42

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

tasks/grunt-postmark.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = function(grunt) {
2929
'Subject': _data.subject || options.subject
3030
};
3131

32+
// Send batch API request based off number of emails
3233
if (this.filesSrc.length > 1) {
3334
// Send batch messages
3435
var messages = [];
@@ -40,36 +41,40 @@ module.exports = function(grunt) {
4041
});
4142

4243
postmark.batch(messages, function(err, response) {
43-
if (err) {
44-
grunt.log.warn('Error response: ' + JSON.stringify(err));
45-
} else {
46-
grunt.log.writeln('Email sent successfully:');
47-
grunt.log.writeln(JSON.stringify(response));
48-
}
49-
done();
44+
handleResponse(err, response, done);
5045
});
5146

5247
} else {
5348
// Send single message
5449
message.HtmlBody = grunt.file.read(this.filesSrc);
5550

5651
postmark.send(message, function(err, response) {
57-
if (err) {
58-
grunt.log.warn('Error response: ' + JSON.stringify(err));
59-
} else {
60-
grunt.log.writeln('Email sent successfully:');
61-
grunt.log.writeln(JSON.stringify(response));
62-
}
63-
done();
52+
handleResponse(err, response, done);
6453
});
6554
}
6655

6756
} else {
57+
// Warn about no files being passed to task
6858
grunt.log.warn('No src file found \n');
6959
}
7060

7161
});
7262

63+
64+
function handleResponse(err, response, done) {
65+
err ? errorMessage(err) : successMessage(response);
66+
done();
67+
}
68+
69+
function errorMessage(response) {
70+
grunt.log.warn('Error response: ' + JSON.stringify(response));
71+
}
72+
73+
function successMessage(response) {
74+
grunt.log.writeln('Email sent successfully:');
75+
grunt.log.writeln(JSON.stringify(response));
76+
}
77+
7378
function clone(obj) {
7479
return JSON.parse(JSON.stringify(obj));
7580
}

0 commit comments

Comments
 (0)