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

chore: Idiomatic changes reported by CodeClimate #61

Merged
merged 1 commit into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
27 changes: 14 additions & 13 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -89,25 +89,26 @@ 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
}

// 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
Expand All @@ -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)
})
Expand Down