From 1638f25f982dc2bbe76db6fd8791c9368204cb88 Mon Sep 17 00:00:00 2001 From: Mithun Sasidharan Date: Sun, 29 Oct 2017 20:26:19 +0530 Subject: [PATCH] Idiomatic changes reported by CodeClimate --- examples/example.js | 16 ++++++++-------- lib/client.js | 27 ++++++++++++++------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/examples/example.js b/examples/example.js index 0ec49de..f3d74ac 100644 --- a/examples/example.js +++ b/examples/example.js @@ -2,7 +2,7 @@ var Client = require('../lib/client.js').Client // These values persist across of subsequent calls, unless overidden. var globalRequest = require('../lib/client.js').emptyRequest -globalRequest.host = 'api.sendgrid.com'; +globalRequest.host = 'api.sendgrid.com' // You must add your SendGrid API Key to your OS Environment globalRequest.headers['Authorization'] = 'Bearer '.concat(process.env.SENDGRID_API_KEY) var client = new Client(globalRequest) @@ -39,17 +39,17 @@ function createAPIKey (callback) { console.log(response.body) console.log(response.headers) var body = JSON.parse(response.body) - callback(body.api_key_id) + callback(body.apiKeyId) }) } -createAPIKey(function (returnValue) { // This ensures we POST a new key first, to get the api_key_id - var api_key_id = '/'.concat(returnValue) +createAPIKey(function (returnValue) { // This ensures we POST a new key first, to get the apiKeyId + var apiKeyId = '/'.concat(returnValue) // GET SINGLE var requestGetSingle = client.emptyRequest() requestGetSingle.method = 'GET' - requestGetSingle.path = '/v3/api_keys'.concat(api_key_id) + requestGetSingle.path = '/v3/api_keys'.concat(apiKeyId) client.API(requestGetSingle, function (response) { console.log(response.statusCode) console.log(response.body) @@ -62,7 +62,7 @@ createAPIKey(function (returnValue) { // This ensures we POST a new key first, t } var requestPatch = client.emptyRequest() requestPatch.method = 'PATCH' - requestPatch.path = '/v3/api_keys'.concat(api_key_id) + requestPatch.path = '/v3/api_keys'.concat(apiKeyId) requestPatch.body = requestBody client.API(requestPatch, function (response) { console.log(response.statusCode) @@ -80,7 +80,7 @@ createAPIKey(function (returnValue) { // This ensures we POST a new key first, t } var requestPut = client.emptyRequest() requestPut.method = 'PUT' - requestPut.path = '/v3/api_keys'.concat(api_key_id) + requestPut.path = '/v3/api_keys'.concat(apiKeyId) requestPut.body = requestBody client.API(requestPut, function (response) { console.log(response.statusCode) @@ -91,7 +91,7 @@ createAPIKey(function (returnValue) { // This ensures we POST a new key first, t // DELETE var requestDelete = client.emptyRequest() requestDelete.method = 'DELETE' - requestDelete.path = '/v3/api_keys'.concat(api_key_id) + requestDelete.path = '/v3/api_keys'.concat(apiKeyId) client.API(requestDelete, function (response) { console.log(response.statusCode) console.log(response.body) diff --git a/lib/client.js b/lib/client.js index ad954c1..023a18d 100644 --- a/lib/client.js +++ b/lib/client.js @@ -31,12 +31,12 @@ var response = { function Client (globalRequest, maxSockets) { var emptyResponse = JSON.parse(JSON.stringify(response)) var body = '' - var httpAgent = null; - var httpsAgent = null; + var httpAgent = null + var httpsAgent = null - if(maxSockets) { - httpAgent = new http.Agent({ maxSockets: maxSockets }); - httpsAgent = new https.Agent({ maxSockets: maxSockets }); + if (maxSockets) { + httpAgent = new http.Agent({ maxSockets: maxSockets }) + httpsAgent = new https.Agent({ maxSockets: maxSockets }) } // utility function to create an empty request object @@ -89,9 +89,9 @@ function Client (globalRequest, maxSockets) { request.headers['Content-Type'] = 'application/json' } - //if maxsockets where provided use the apropriate agent - if(maxSockets) { - request.agent = endpointRequest.test == true ? httpAgent : httpsAgent + // if maxsockets where provided use the apropriate agent + if (maxSockets) { + request.agent = endpointRequest.test === true ? httpAgent : httpsAgent } return request } @@ -99,15 +99,16 @@ function Client (globalRequest, maxSockets) { // API is the main interface to the API. this.API = function (endpointRequest, callback) { var request = buildRequest(globalRequest, endpointRequest) + var requestProtocol = null; - if ( endpointRequest.test == true ) { - var http_request = http + if (endpointRequest.test === true) { + requestProtocol = http request.port = endpointRequest.port } else { - var http_request = https + requestProtocol = https } - var httpRequest = http_request.request(request, function (httpResponse) { + var httpRequest = requestProtocol.request(request, function (httpResponse) { var responseBody = '' // cature the response from the API @@ -131,7 +132,7 @@ function Client (globalRequest, maxSockets) { response.body = JSON.stringify({ message: e.message, name: e.name, - stack: e.stack, + stack: e.stack }) callback(response) })