Skip to content
This repository was archived by the owner on Aug 20, 2020. It is now read-only.

Commit 5bb4cca

Browse files
author
Matt Bernier
authored
Merge pull request #61 from mithunsasidharan/master
Idiomatic changes reported by CodeClimate
2 parents e41ab40 + 1638f25 commit 5bb4cca

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

examples/example.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var Client = require('../lib/client.js').Client
22

33
// These values persist across of subsequent calls, unless overidden.
44
var globalRequest = require('../lib/client.js').emptyRequest
5-
globalRequest.host = 'api.sendgrid.com';
5+
globalRequest.host = 'api.sendgrid.com'
66
// You must add your SendGrid API Key to your OS Environment
77
globalRequest.headers['Authorization'] = 'Bearer '.concat(process.env.SENDGRID_API_KEY)
88
var client = new Client(globalRequest)
@@ -39,17 +39,17 @@ function createAPIKey (callback) {
3939
console.log(response.body)
4040
console.log(response.headers)
4141
var body = JSON.parse(response.body)
42-
callback(body.api_key_id)
42+
callback(body.apiKeyId)
4343
})
4444
}
4545

46-
createAPIKey(function (returnValue) { // This ensures we POST a new key first, to get the api_key_id
47-
var api_key_id = '/'.concat(returnValue)
46+
createAPIKey(function (returnValue) { // This ensures we POST a new key first, to get the apiKeyId
47+
var apiKeyId = '/'.concat(returnValue)
4848

4949
// GET SINGLE
5050
var requestGetSingle = client.emptyRequest()
5151
requestGetSingle.method = 'GET'
52-
requestGetSingle.path = '/v3/api_keys'.concat(api_key_id)
52+
requestGetSingle.path = '/v3/api_keys'.concat(apiKeyId)
5353
client.API(requestGetSingle, function (response) {
5454
console.log(response.statusCode)
5555
console.log(response.body)
@@ -62,7 +62,7 @@ createAPIKey(function (returnValue) { // This ensures we POST a new key first, t
6262
}
6363
var requestPatch = client.emptyRequest()
6464
requestPatch.method = 'PATCH'
65-
requestPatch.path = '/v3/api_keys'.concat(api_key_id)
65+
requestPatch.path = '/v3/api_keys'.concat(apiKeyId)
6666
requestPatch.body = requestBody
6767
client.API(requestPatch, function (response) {
6868
console.log(response.statusCode)
@@ -80,7 +80,7 @@ createAPIKey(function (returnValue) { // This ensures we POST a new key first, t
8080
}
8181
var requestPut = client.emptyRequest()
8282
requestPut.method = 'PUT'
83-
requestPut.path = '/v3/api_keys'.concat(api_key_id)
83+
requestPut.path = '/v3/api_keys'.concat(apiKeyId)
8484
requestPut.body = requestBody
8585
client.API(requestPut, function (response) {
8686
console.log(response.statusCode)
@@ -91,7 +91,7 @@ createAPIKey(function (returnValue) { // This ensures we POST a new key first, t
9191
// DELETE
9292
var requestDelete = client.emptyRequest()
9393
requestDelete.method = 'DELETE'
94-
requestDelete.path = '/v3/api_keys'.concat(api_key_id)
94+
requestDelete.path = '/v3/api_keys'.concat(apiKeyId)
9595
client.API(requestDelete, function (response) {
9696
console.log(response.statusCode)
9797
console.log(response.body)

lib/client.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ var response = {
3131
function Client (globalRequest, maxSockets) {
3232
var emptyResponse = JSON.parse(JSON.stringify(response))
3333
var body = ''
34-
var httpAgent = null;
35-
var httpsAgent = null;
34+
var httpAgent = null
35+
var httpsAgent = null
3636

37-
if(maxSockets) {
38-
httpAgent = new http.Agent({ maxSockets: maxSockets });
39-
httpsAgent = new https.Agent({ maxSockets: maxSockets });
37+
if (maxSockets) {
38+
httpAgent = new http.Agent({ maxSockets: maxSockets })
39+
httpsAgent = new https.Agent({ maxSockets: maxSockets })
4040
}
4141

4242
// utility function to create an empty request object
@@ -89,25 +89,26 @@ function Client (globalRequest, maxSockets) {
8989
request.headers['Content-Type'] = 'application/json'
9090
}
9191

92-
//if maxsockets where provided use the apropriate agent
93-
if(maxSockets) {
94-
request.agent = endpointRequest.test == true ? httpAgent : httpsAgent
92+
// if maxsockets where provided use the apropriate agent
93+
if (maxSockets) {
94+
request.agent = endpointRequest.test === true ? httpAgent : httpsAgent
9595
}
9696
return request
9797
}
9898

9999
// API is the main interface to the API.
100100
this.API = function (endpointRequest, callback) {
101101
var request = buildRequest(globalRequest, endpointRequest)
102+
var requestProtocol = null;
102103

103-
if ( endpointRequest.test == true ) {
104-
var http_request = http
104+
if (endpointRequest.test === true) {
105+
requestProtocol = http
105106
request.port = endpointRequest.port
106107
} else {
107-
var http_request = https
108+
requestProtocol = https
108109
}
109110

110-
var httpRequest = http_request.request(request, function (httpResponse) {
111+
var httpRequest = requestProtocol.request(request, function (httpResponse) {
111112
var responseBody = ''
112113

113114
// cature the response from the API
@@ -131,7 +132,7 @@ function Client (globalRequest, maxSockets) {
131132
response.body = JSON.stringify({
132133
message: e.message,
133134
name: e.name,
134-
stack: e.stack,
135+
stack: e.stack
135136
})
136137
callback(response)
137138
})

0 commit comments

Comments
 (0)