Skip to content

Commit

Permalink
Switched JSHint for ESLint with SparkPost config (#159)
Browse files Browse the repository at this point in the history
* Switched JSHint for ESLint with SparkPost config

* modified lint npm script to remove node_modules path
  • Loading branch information
aydrian authored Aug 23, 2016
1 parent 23fdf26 commit abb4265
Show file tree
Hide file tree
Showing 27 changed files with 277 additions and 234 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "sparkpost/api",
"globals": {
"Promise": true
}
}
16 changes: 4 additions & 12 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ module.exports = function(grunt) {
grunt.initConfig({
config: config,
pkg: grunt.file.readJSON('package.json'),
jshint: {
files: [
'index.js',
'lib/{,*/}*.js',
'examples/{,*/}*.js'
],
options: {
reporter: require('jshint-stylish'),
jshintrc: './.jshintrc'
}
},
bump: {
options: {
files: [ 'package.json' ]
Expand All @@ -46,6 +35,9 @@ module.exports = function(grunt) {
}
},
shell: {
lint: {
command: 'npm run lint'
},
coverage: {
command : path.join(config.binPath, 'istanbul') + ' cover --report lcov --dir test/reports/ node_modules/mocha/bin/_mocha test/spec -- --reporter ' + reporter,
options : {
Expand All @@ -68,7 +60,7 @@ module.exports = function(grunt) {
});

// grunt lint - leverages grunt-contrib-jshint command, lints our code
grunt.registerTask('lint', [ 'jshint' ]);
grunt.registerTask('lint', [ 'shell:lint' ]);

// grunt test - runs linting and then our unit tests
grunt.registerTask('test', [
Expand Down
53 changes: 27 additions & 26 deletions lib/SendGridCompatibility/Email.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*
* @param options object that contains initial values
*/
function Email(options){
for (var option in options) {
function Email(options) {
var option;
for (option in options) {
this[option] = options[option];
}
}
Expand All @@ -17,85 +18,85 @@ function Email(options){
* @param Most have a single value to map
* Add functions will often contain a key / value pair
*/
Email.prototype.addTo = function (address){
if (this.to === undefined){
Email.prototype.addTo = function(address) {
if (this.to === undefined) {
this.to = address;
} else if (typeof this.to === 'string'){
} else if (typeof this.to === 'string') {
this.to = [this.to];
this.to.push(address);
} else {
this.to.push(address);
}
};
Email.prototype.setFrom = function (address){
Email.prototype.setFrom = function(address) {
this.from = address;
};
Email.prototype.setSubject = function (subject){
Email.prototype.setSubject = function(subject) {
this.subject = subject;
};
Email.prototype.setText = function (text){
Email.prototype.setText = function(text) {
this.text = text;
};
Email.prototype.setHtml = function (html){
Email.prototype.setHtml = function(html) {
this.html = html;
};
Email.prototype.addHeader = function (key, value){
if (this.headers === undefined){
Email.prototype.addHeader = function(key, value) {
if (this.headers === undefined) {
this.headers = {};
}
this.headers[key] = value;
};
Email.prototype.setHeaders = function (headers){
Email.prototype.setHeaders = function(headers) {
this.headers = headers;
};
Email.prototype.addSubstitution = function (key, value){
if (this.sub === undefined){
Email.prototype.addSubstitution = function(key, value) {
if (this.sub === undefined) {
this.sub = {};
}
if (typeof value === 'string'){
if (typeof value === 'string') {
this.sub[key] = [value];
} else {
this.sub[key] = value;
}
};
Email.prototype.setSubstitutions = function (substitutions){
Email.prototype.setSubstitutions = function(substitutions) {
this.sub = substitutions;
};
Email.prototype.addSection = function (key, value){
if (this.section === undefined){
Email.prototype.addSection = function(key, value) {
if (this.section === undefined) {
this.section = {};
}
this.section[key] = value;
};
Email.prototype.setSections = function (sections){
Email.prototype.setSections = function(sections) {
this.section = sections;
};
// SparkPost doesn't currently support addUniqueArg, throw an error
Email.prototype.addUniqueArg = function (){
Email.prototype.addUniqueArg = function() {
throw new Error('Unique Argument compatibility is not supported.');
};
// SparkPost doesn't currently support setUniqueArgs, throw an error
Email.prototype.setUniqueArgs = function (){
Email.prototype.setUniqueArgs = function() {
throw new Error('Unique Argument compatibility is not supported.');
};
// SparkPost doesn't currently support addCategory, throw an error
Email.prototype.addCategory = function (){
Email.prototype.addCategory = function() {
throw new Error('Category compatibility is not supported.');
};
// SparkPost doesn't currently support setCategories, throw an error
Email.prototype.setCategories = function (){
Email.prototype.setCategories = function() {
throw new Error('Category compatibility is not supported.');
};
// SparkPost doesn't currently support addFilter, throw an error
Email.prototype.addFilter = function (){
Email.prototype.addFilter = function() {
throw new Error('Filter compatibility is not supported.');
};
// SparkPost doesn't currently support setFilters, throw an error
Email.prototype.setFilters = function (){
Email.prototype.setFilters = function() {
throw new Error('Filter compatibility is not supported.');
};
// SparkPost doesn't currently support addFile, throw an error
Email.prototype.addFile = function (){
Email.prototype.addFile = function() {
throw new Error('File compatibility is not supported.');
};

Expand Down
54 changes: 28 additions & 26 deletions lib/SendGridCompatibility/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
var _ = require('lodash');
var url = require('url');
var SparkPost = require('../sparkpost');
var _ = require('lodash')
, url = require('url')
, SparkPost = require('../sparkpost')
, sendgrid, consolidateSubstitutionData, parseTo, translatePayload;

/**
* SendGrid compatibility constructor
Expand All @@ -13,14 +14,15 @@ var SparkPost = require('../sparkpost');
* @param apiKey: api key string
* @param options: optional additional options object
*/
var sendgrid = function(username, apiKey, options) {
sendgrid = function(username, apiKey, options) {
var urlOpts, opts;
options = options || {};
var urlOpts = {
urlOpts = {
protocol: options.protocol
, hostname: options.host
, port: options.port
}
, opts = {
};
opts = {
endpoint: url.format(urlOpts)
};

Expand All @@ -35,13 +37,13 @@ var sendgrid = function(username, apiKey, options) {
* be translated into a SparkPost payload
* @returns object: substitutionData object as per SparkPost payload format
*/
var consolidateSubstitutionData = function(payload) {
consolidateSubstitutionData = function(payload) {
var substitutionData = {};
if (payload.sub !== undefined && payload.section !== undefined){
if (payload.sub !== undefined && payload.section !== undefined) {
substitutionData = _.merge(payload.sub, payload.section);
} else if (payload.sub !== undefined){
} else if (payload.sub !== undefined) {
substitutionData = payload.sub;
} else if (payload.section !== undefined){
} else if (payload.section !== undefined) {
substitutionData = payload.section;
}
return substitutionData;
Expand All @@ -55,18 +57,18 @@ var consolidateSubstitutionData = function(payload) {
* be translated into a SparkPost payload
* @returns array: recipients array as per SparkPost payload format
*/
var parseTo = function(payload){
var recipients = [];
if (typeof payload.to === 'string'){
parseTo = function(payload) {
var recipients = [], to, i;
if (typeof payload.to === 'string') {
payload.to = [payload.to];
}
for (var i = 0; payload.to.length > i; i++){
var to = {
for (i = 0; payload.to.length > i; i++) {
to = {
address: {
email: payload.to[i]
}
};
if (payload.toname !== undefined && payload.toname[i] !== undefined){
if (payload.toname !== undefined && payload.toname[i] !== undefined) {
to.address.name = payload.toname[i];
}
recipients.push(to);
Expand All @@ -82,21 +84,21 @@ var parseTo = function(payload){
* be translated into a SparkPost payload
* @returns object: translation from SendGrid payload to SparkPost payload
*/
var translatePayload = function(payload) {
translatePayload = function(payload) {
var sub = consolidateSubstitutionData(payload)
, input = {
recipients: [],
from: '',
html: '',
text: '',
subject: ''
};
recipients: [],
from: '',
html: '',
text: '',
subject: ''
};

if (payload.to !== undefined){
if (payload.to !== undefined) {
input.recipients = parseTo(payload);
}
input.from = payload.from;
if (payload.fromname !== undefined){
if (payload.fromname !== undefined) {
input.from = input.from + ' <' + payload.fromname + '>';
}
input.subject = payload.subject;
Expand Down
13 changes: 9 additions & 4 deletions lib/inboundDomains.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var api = 'inbound-domains'
/* global -Promise */
, Promise = require('./Promise');

module.exports = function(client) {
Expand All @@ -13,6 +12,8 @@ module.exports = function(client) {
return client.get(options).asCallback(callback);
},
find: function(domain, callback) {
var options;

if(typeof domain === 'function') {
callback = domain;
domain = null;
Expand All @@ -22,12 +23,14 @@ module.exports = function(client) {
return Promise.reject(new Error('domain is required')).asCallback(callback);
}

var options = {
options = {
uri: api + '/' + domain
};
return client.get(options).asCallback(callback);
},
create: function(domain, callback) {
var options;

if(typeof domain === 'function') {
callback = domain;
domain = null;
Expand All @@ -37,7 +40,7 @@ module.exports = function(client) {
return Promise.reject(new Error('domain is required')).asCallback(callback);
}

var options = {
options = {
uri: api
, json: {
domain: domain
Expand All @@ -46,6 +49,8 @@ module.exports = function(client) {
return client.post(options, callback).asCallback(callback);
},
delete: function(domain, callback) {
var options;

if (typeof domain === 'function') {
callback = domain;
domain = null;
Expand All @@ -55,7 +60,7 @@ module.exports = function(client) {
return Promise.reject(new Error('domain is required')).asCallback(callback);
}

var options = {
options = {
uri: api + '/' + domain
};
return client.delete(options).asCallback(callback);
Expand Down
20 changes: 10 additions & 10 deletions lib/messageEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ var api = 'message-events';
* "Class" declaration, Message Events API exposes one function:
* - search: retrieves list of message events according to given params
*/
module.exports = function (client) {
module.exports = function(client) {
return {
search: function(parameters, callback) {
var arrayParams = [
'bounce_classes',
'campaign_ids',
'events',
'friendly_froms',
'message_ids',
'recipients',
'template_ids',
'transmission_ids'
]
'bounce_classes',
'campaign_ids',
'events',
'friendly_froms',
'message_ids',
'recipients',
'template_ids',
'transmission_ids'
]
, options;

arrayParams.forEach(function(paramname) {
Expand Down
Loading

0 comments on commit abb4265

Please sign in to comment.