Comparing version 1.3.0 to 1.4.0
@@ -64,3 +64,3 @@ var _ = require('underscore'); | ||
//make request | ||
client.request(requestArgs, args.callback); | ||
return client.request(requestArgs, args.callback); | ||
}; | ||
@@ -67,0 +67,0 @@ }; |
@@ -10,3 +10,4 @@ /** | ||
//Dependencies | ||
var querystring = require('querystring'), | ||
var Q = require('q'), | ||
querystring = require('querystring'), | ||
request = require('request'), | ||
@@ -59,3 +60,3 @@ moduleinfo = require('../package.json'), | ||
//SMS shorthand | ||
//Messaging shorthand | ||
this.sendSms = this.accounts.sms.messages.post; | ||
@@ -65,6 +66,11 @@ this.sendMms = this.accounts.messages.post; | ||
this.listSms = this.accounts.sms.messages.get; | ||
this.listMessages = this.accounts.messages.get; | ||
this.getSms = function(messageSid, callback) { | ||
this.accounts.sms.messages(messageSid).get(callback); | ||
}; | ||
this.getMessage = function(messageSid, callback) { | ||
this.accounts.messages(messageSid).get(callback); | ||
}; | ||
//Calls shorthand | ||
@@ -100,3 +106,4 @@ this.makeCall = this.accounts.calls.post; | ||
RestClient.prototype.request = function (options, callback) { | ||
var client = this; | ||
var client = this, | ||
deferred = Q.defer(); | ||
@@ -122,66 +129,73 @@ //Prepare request options | ||
request(options, function (err, response, body) { | ||
if (callback) { | ||
var data; | ||
try { | ||
data = err || !body ? {status: 500, message: 'Empty body'} : JSON.parse(body); | ||
} catch (e) { | ||
data = { status: 500, message: (e.message || 'Invalid JSON body') }; | ||
} | ||
var data; | ||
try { | ||
data = err || !body ? {status: 500, message: 'Empty body'} : JSON.parse(body); | ||
} catch (e) { | ||
data = { status: 500, message: (e.message || 'Invalid JSON body') }; | ||
} | ||
//request doesn't think 4xx is an error - we want an error for any non-2xx status codes | ||
var error = null; | ||
if (err || (response && (response.statusCode < 200 || response.statusCode > 206))) { | ||
error = {}; | ||
// response is null if server is unreachable | ||
if (response) { | ||
error.status = response.statusCode; | ||
error.message = data ? data.message : 'Unable to complete HTTP request'; | ||
error.code = data && data.code; | ||
error.moreInfo = data && data.more_info; | ||
} else { | ||
error.status = err.code; | ||
error.message = 'Unable to reach host: "'+client.host+'"'; | ||
} | ||
//request doesn't think 4xx is an error - we want an error for any non-2xx status codes | ||
var error = null; | ||
if (err || (response && (response.statusCode < 200 || response.statusCode > 206))) { | ||
error = {}; | ||
// response is null if server is unreachable | ||
if (response) { | ||
error.status = response.statusCode; | ||
error.message = data ? data.message : 'Unable to complete HTTP request'; | ||
error.code = data && data.code; | ||
error.moreInfo = data && data.more_info; | ||
} else { | ||
error.status = err.code; | ||
error.message = 'Unable to reach host: "'+client.host+'"'; | ||
} | ||
} | ||
//process data and make available in a more JavaScripty format | ||
function processKeys(source) { | ||
if (_.isObject(source)) { | ||
Object.keys(source).forEach(function(key) { | ||
//process data and make available in a more JavaScripty format | ||
function processKeys(source) { | ||
if (_.isObject(source)) { | ||
Object.keys(source).forEach(function(key) { | ||
//Supplement underscore values with camel-case | ||
if (key.indexOf('_') > 0) { | ||
var cc = key.replace(/_([a-z])/g, function (g) { | ||
return g[1].toUpperCase() | ||
}); | ||
source[cc] = source[key]; | ||
} | ||
//Supplement underscore values with camel-case | ||
if (key.indexOf('_') > 0) { | ||
var cc = key.replace(/_([a-z])/g, function (g) { | ||
return g[1].toUpperCase() | ||
}); | ||
source[cc] = source[key]; | ||
} | ||
//process any nested arrays... | ||
if (Array.isArray(source[key])) { | ||
source[key].forEach(processKeys); | ||
} | ||
else if (_.isObject(source[key])) { | ||
processKeys(source[key]); | ||
} | ||
}); | ||
//process any nested arrays... | ||
if (Array.isArray(source[key])) { | ||
source[key].forEach(processKeys); | ||
} | ||
else if (_.isObject(source[key])) { | ||
processKeys(source[key]); | ||
} | ||
}); | ||
//Look for and convert date strings for specific keys | ||
['startDate', 'endDate', 'dateCreated', 'dateUpdated', 'startTime', 'endTime'].forEach(function(dateKey) { | ||
if (source[dateKey]) { | ||
source[dateKey] = new Date(source[dateKey]); | ||
} | ||
}); | ||
} | ||
//Look for and convert date strings for specific keys | ||
['startDate', 'endDate', 'dateCreated', 'dateUpdated', 'startTime', 'endTime'].forEach(function(dateKey) { | ||
if (source[dateKey]) { | ||
source[dateKey] = new Date(source[dateKey]); | ||
} | ||
}); | ||
} | ||
processKeys(data); | ||
} | ||
processKeys(data); | ||
//hang response off the JSON-serialized data | ||
data.nodeClientResponse = response; | ||
//hang response off the JSON-serialized data | ||
data.nodeClientResponse = response; | ||
callback.call(client, error, data, response); | ||
// Resolve promise | ||
if (error) { | ||
deferred.reject(error); | ||
} else { | ||
deferred.resolve(data); | ||
} | ||
}); | ||
// Return promise, but also support original node callback style | ||
return deferred.promise.nodeify(callback); | ||
}; | ||
module.exports = RestClient; |
{ | ||
"name": "twilio", | ||
"description": "A Twilio helper library", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"author": "Kevin Whinnery <kevin.whinnery@gmail.com>", | ||
@@ -23,3 +23,4 @@ "contributors": [ | ||
"underscore": "1.x", | ||
"jwt-simple": "0.1.x" | ||
"jwt-simple": "0.1.x", | ||
"q": "0.9.7" | ||
}, | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
# twilio-node | ||
[![Build Status](https://travis-ci.org/twilio/twilio-node.png?branch=master)](https://travis-ci.org/twilio/twilio-node) | ||
A node.js Twilio helper library. For usage infomation and API docs, head out here: | ||
[http://twilio.github.com/twilio-node/](http://twilio.github.com/twilio-node/) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49771
28
968
8
4