Skip to content

Commit

Permalink
Add linter. Fix failures. Require linting to pass tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
SlexAxton committed Oct 22, 2015
1 parent d174d55 commit ecb72d4
Show file tree
Hide file tree
Showing 67 changed files with 697 additions and 1,464 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.npm-debug.log
8 changes: 8 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"plugins": ["stripe-javascript-style"],
"preset": "stripe",
"disallowKeywords": ["with"],
"requireSpread": false,
"requireTemplateStrings": false,
"requireCamelCaseOrUpperCaseIdentifiers": false
}
15 changes: 7 additions & 8 deletions lib/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ _Error.extend = utils.protoExtend;
var StripeError = _Error.StripeError = _Error.extend({
type: 'StripeError',
populate: function(raw) {

// Move from prototype def (so it appears in stringified obj)
this.type = this.type;

Expand All @@ -43,7 +42,7 @@ var StripeError = _Error.StripeError = _Error.extend({
this.raw = raw;
this.requestId = raw.requestId;
this.statusCode = raw.statusCode;
}
},
});

/**
Expand All @@ -62,9 +61,9 @@ StripeError.generate = function(rawStripeError) {
};

// Specific Stripe Error types:
_Error.StripeCardError = StripeError.extend({ type: 'StripeCardError' });
_Error.StripeInvalidRequestError = StripeError.extend({ type: 'StripeInvalidRequestError' });
_Error.StripeAPIError = StripeError.extend({ type: 'StripeAPIError' });
_Error.StripeAuthenticationError = StripeError.extend({ type: 'StripeAuthenticationError' });
_Error.StripeRateLimitError = StripeError.extend({ type: 'StripeRateLimitError' });
_Error.StripeConnectionError = StripeError.extend({ type: 'StripeConnectionError' });
_Error.StripeCardError = StripeError.extend({type: 'StripeCardError'});
_Error.StripeInvalidRequestError = StripeError.extend({type: 'StripeInvalidRequestError'});
_Error.StripeAPIError = StripeError.extend({type: 'StripeAPIError'});
_Error.StripeAuthenticationError = StripeError.extend({type: 'StripeAuthenticationError'});
_Error.StripeRateLimitError = StripeError.extend({type: 'StripeRateLimitError'});
_Error.StripeConnectionError = StripeError.extend({type: 'StripeConnectionError'});
10 changes: 6 additions & 4 deletions lib/MultipartDataGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ var _ = require('lodash');
// Method for formatting HTTP body for the multipart/form-data specification
// Mostly taken from Fermata.js
// https://github.com/natevw/fermata/blob/5d9732a33d776ce925013a265935facd1626cc88/fermata.js#L315-L343
module.exports = function multipartDataGenerator(method, data, headers) {
var segno = '' + Math.round(Math.random() * 1e16) + Math.round(Math.random() * 1e16);
function multipartDataGenerator(method, data, headers) {
var segno = (Math.round(Math.random() * 1e16) + Math.round(Math.random() * 1e16)).toString();
headers['Content-Type'] = ('multipart/form-data; boundary=' + segno);
var buffer = new Buffer(0);

Expand All @@ -16,14 +16,14 @@ module.exports = function multipartDataGenerator(method, data, headers) {
buffer = new Buffer(prevBuffer.length + newBuffer.length + 2);
prevBuffer.copy(buffer);
newBuffer.copy(buffer, prevBuffer.length);
buffer.write("\r\n", buffer.length - 2);
buffer.write('\r\n', buffer.length - 2);
}

function q(s) {
return '"' + s.replace(/"|"/g, '%22').replace(/\r\n|\r|\n/g, ' ') + '"';
}

_.forEach(data, function (v, k) {
_.forEach(data, function(v, k) {
push('--' + segno);
if (v.hasOwnProperty('data')) {
push('Content-Disposition: form-data; name=' + q(k) + '; filename=' + q(v.name || 'blob'));
Expand All @@ -40,3 +40,5 @@ module.exports = function multipartDataGenerator(method, data, headers) {

return buffer;
};

module.exports = multipartDataGenerator;
47 changes: 26 additions & 21 deletions lib/StripeMethod.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@ var utils = require('./utils');
module.exports = {

create: stripeMethod({
method: 'POST'
method: 'POST',
}),

list: stripeMethod({
method: 'GET'
method: 'GET',
}),

retrieve: stripeMethod({
method: 'GET',
path: '/{id}',
urlParams: ['id']
urlParams: ['id'],
}),

update: stripeMethod({
method: 'POST',
path: '{id}',
urlParams: ['id']
urlParams: ['id'],
}),

// Avoid 'delete' keyword in JS
del: stripeMethod({
method: 'DELETE',
path: '{id}',
urlParams: ['id']
urlParams: ['id'],
}),

setMetadata: function(id, key, value, auth, cb) {

var self = this;
var data = key;
var isObject = utils.isObject(key);
// We assume null for an empty object
var isNull = data === null || (isObject && !Object.keys(data).length);

// Allow optional passing of auth & cb:
if ((isNull || isObject) && typeof value == 'string') auth = value;
else if (typeof auth != 'string') {
if ((isNull || isObject) && typeof value == 'string') {
auth = value;
} else if (typeof auth != 'string') {
if (!cb && typeof auth == 'function') {
cb = auth;
}
Expand All @@ -64,28 +64,31 @@ module.exports = {
} else {
// Set entire metadata object after resetting it:
this._request('POST', path, {
metadata: null
metadata: null,
}, auth, {}, function(err, response) {
if (err) return deferred.reject(err);
if (err) {
return deferred.reject(err);
}
sendMetadata(data, auth);
});
}

function sendMetadata(metadata, auth) {
self._request('POST', path, {
metadata: metadata
metadata: metadata,
}, auth, {}, function(err, response) {
if (err) deferred.reject(err);
else deferred.resolve(response.metadata);
if (err) {
deferred.reject(err);
} else {
deferred.resolve(response.metadata);
}
});
}

return deferred.promise;

},

getMetadata: function(id, auth, cb) {

if (!cb && typeof auth == 'function') {
cb = auth;
auth = null;
Expand All @@ -96,14 +99,16 @@ module.exports = {
var path = this.createFullPath('/' + id, urlData);

this._request('GET', path, {}, auth, {}, function(err, response) {
if (err) deferred.reject(err);
else deferred.resolve(
response.metadata
);
if (err) {
deferred.reject(err);
} else {
deferred.resolve(
response.metadata
);
}
});

return deferred.promise;

}
},

};
11 changes: 5 additions & 6 deletions lib/StripeMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ var OPTIONAL_REGEX = /^optional!/;
* optionally passed through a hash (Object) as the penultimate argument
* (preceeding the also-optional callback argument
*/
module.exports = function stripeMethod(spec) {

function stripeMethod(spec) {
var commandPath = typeof spec.path == 'function' ? spec.path
: utils.makeURLInterpolator( spec.path || '' );
: utils.makeURLInterpolator(spec.path || '');
var requestMethod = (spec.method || 'GET').toUpperCase();
var urlParams = spec.urlParams || [];

Expand All @@ -32,7 +31,6 @@ module.exports = function stripeMethod(spec) {
var urlData = this.createUrlData();

for (var i = 0, l = urlParams.length; i < l; ++i) {

var arg = args[0];
var param = urlParams[i];

Expand Down Expand Up @@ -61,7 +59,7 @@ module.exports = function stripeMethod(spec) {
}

var requestPath = this.createFullPath(commandPath, urlData);
var requestCallback = function(err, response) {
function requestCallback(err, response) {
if (err) {
deferred.reject(err);
} else {
Expand All @@ -77,6 +75,7 @@ module.exports = function stripeMethod(spec) {
self._request(requestMethod, requestPath, data, opts.auth, options, requestCallback);

return deferred.promise;

};
};

module.exports = stripeMethod;
42 changes: 19 additions & 23 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var Error = require('./Error');

var hasOwn = {}.hasOwnProperty;


// Provide extension mechanism for Stripe Resource Sub-Classes
StripeResource.extend = utils.protoExtend;

Expand All @@ -23,7 +22,6 @@ StripeResource.BASIC_METHODS = require('./StripeMethod.basic');
* Encapsulates request logic for a Stripe Resource
*/
function StripeResource(stripe, urlData) {

this._stripe = stripe;
this._urlData = urlData || {};

Expand All @@ -37,7 +35,6 @@ function StripeResource(stripe, urlData) {
}

this.initialize.apply(this, arguments);

}

StripeResource.prototype = {
Expand Down Expand Up @@ -78,19 +75,19 @@ StripeResource.prototype = {
},

createDeferred: function(callback) {
var deferred = Promise.defer();
var deferred = Promise.defer();

if (callback) {
// Callback, if provided, is a simply translated to Promise'esque:
// (Ensure callback is called outside of promise stack)
deferred.promise.then(function(res) {
setTimeout(function(){ callback(null, res) }, 0);
if (callback) {
// Callback, if provided, is a simply translated to Promise'esque:
// (Ensure callback is called outside of promise stack)
deferred.promise.then(function(res) {
setTimeout(function() { callback(null, res) }, 0);
}, function(err) {
setTimeout(function(){ callback(err, null); }, 0);
setTimeout(function() { callback(err, null); }, 0);
});
}
}

return deferred;
return deferred;
},

_timeoutHandler: function(timeout, req, callback) {
Expand All @@ -106,7 +103,7 @@ StripeResource.prototype = {
self,
new Error.StripeConnectionError({
message: 'Request aborted due to timeout being reached (' + timeout + 'ms)',
detail: timeoutErr
detail: timeoutErr,
}),
null
);
Expand Down Expand Up @@ -149,7 +146,7 @@ StripeResource.prototype = {
message: 'Invalid JSON received from the Stripe API',
response: response,
exception: e,
requestId: headers['request-id']
requestId: headers['request-id'],
}),
null
);
Expand All @@ -162,12 +159,15 @@ StripeResource.prototype = {
_errorHandler: function(req, callback) {
var self = this;
return function(error) {
if (req._isAborted) return; // already handled
if (req._isAborted) {
// already handled
return;
}
callback.call(
self,
new Error.StripeConnectionError({
message: 'An error occurred with our connection to Stripe',
detail: error
detail: error,
}),
null
);
Expand All @@ -194,7 +194,7 @@ StripeResource.prototype = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': requestData.length,
'User-Agent': 'Stripe/v1 NodeBindings/' + this._stripe.getConstant('PACKAGE_VERSION')
'User-Agent': 'Stripe/v1 NodeBindings/' + this._stripe.getConstant('PACKAGE_VERSION'),
};

if (apiVersion) {
Expand All @@ -212,9 +212,7 @@ StripeResource.prototype = {
makeRequest();
});


function makeRequest() {

var timeout = self._stripe.getApiField('timeout');
var isInsecureConnection = self._stripe.getApiField('protocol') == 'http';

Expand All @@ -229,7 +227,7 @@ StripeResource.prototype = {
method: method,
agent: self._stripe.getApiField('agent'),
headers: headers,
ciphers: 'DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:!MD5'
ciphers: 'DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:!MD5',
});

req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback));
Expand All @@ -243,10 +241,8 @@ StripeResource.prototype = {
req.end();
});
});

}

}
},

};

Expand Down
Loading

0 comments on commit ecb72d4

Please sign in to comment.