sms24x7-client
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -5,3 +5,3 @@ #!/usr/bin/env nodejs | ||
'use strict'; | ||
'use strict'; | ||
@@ -11,3 +11,3 @@ | ||
var SmsSender, smsSender, Minimist, cmdArgs, command, printHelp; | ||
var SmsSender, smsSender, Minimist, cmdArgs, command, printHelp; | ||
@@ -17,4 +17,4 @@ | ||
SmsSender = require('../lib'); | ||
Minimist = require('minimist'); | ||
SmsSender = require('../lib'); | ||
Minimist = require('minimist'); | ||
@@ -35,3 +35,3 @@ | ||
' Unsupported command', | ||
]); | ||
]); | ||
} | ||
@@ -70,18 +70,18 @@ | ||
} | ||
}); | ||
}); | ||
switch (command) { | ||
case 'send': | ||
case 'send': | ||
smsSender | ||
.send(cmdArgs.phone, cmdArgs.message, cmdArgs.sender) | ||
.then(function(response) { | ||
console.log(response); | ||
console.log(JSON.stringify(response)); | ||
}) | ||
.catch(function(error) { | ||
console.log(error); | ||
}); | ||
console.log(JSON.stringify(response)); | ||
}); | ||
break; | ||
case 'help': | ||
case 'help': | ||
printHelp(false); | ||
@@ -88,0 +88,0 @@ break; |
168
lib/index.js
'use strict'; | ||
'use strict'; | ||
@@ -8,3 +8,3 @@ | ||
var _, Q, Request; | ||
var _, Q, Request; | ||
@@ -14,5 +14,5 @@ | ||
_ = require('lodash'); | ||
Q = require('q'); | ||
Request = require('request-promise'); | ||
_ = require('lodash'); | ||
Q = require('q'); | ||
Request = require('request-promise'); | ||
@@ -25,3 +25,3 @@ | ||
function SmsSender(options) { | ||
return this.init(options); | ||
return this.init(options); | ||
} | ||
@@ -33,6 +33,6 @@ | ||
._setDefaultOptions() | ||
._setOptions(options); | ||
._setOptions(options); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
@@ -43,8 +43,8 @@ | ||
deferred = Q.defer(), | ||
phones = _.isArray(phone) | ||
? phone | ||
phones = _.isArray(phone) | ||
? phone | ||
: [phone], | ||
jsonPhones = JSON.stringify(phones); | ||
jsonPhones = JSON.stringify(phones); | ||
sender = sender || options.defaultSender; | ||
sender = sender || options.defaultSender; | ||
@@ -79,3 +79,3 @@ if (!message) { | ||
deferred.resolve({ | ||
status: 'ok', | ||
status: 'ok', | ||
response: JSON.parse(response.body) | ||
@@ -92,3 +92,3 @@ }); | ||
} | ||
}); | ||
}); | ||
} catch (e) { | ||
@@ -101,14 +101,14 @@ deferred.reject({ | ||
return deferred.promise; | ||
}; | ||
return deferred.promise; | ||
}; | ||
SmsSender.prototype.getOptions = function() { | ||
return this._options; | ||
}; | ||
return this._options; | ||
}; | ||
SmsSender.prototype._callRemoteMethod = function(method, params, httpMethod) { | ||
var options = this.getOptions(), | ||
requestOptions; | ||
var options = this.getOptions(), | ||
requestOptions; | ||
@@ -119,3 +119,3 @@ if (!options.auth) { | ||
details: 'No auth data provided, please set up email and password.' | ||
}; | ||
}; | ||
} | ||
@@ -127,4 +127,4 @@ | ||
details: 'Email is not set, please set up auth options first.' | ||
}; | ||
} | ||
}; | ||
} | ||
@@ -135,19 +135,19 @@ if (!options.auth.password) { | ||
details: 'Password is not set, please set up auth options first.' | ||
}; | ||
}; | ||
} | ||
params = _.merge(params, { | ||
method: method, | ||
method: method, | ||
email: options.auth.email, | ||
password: options.auth.password | ||
}); | ||
}); | ||
requestOptions = this._getRequestOptions(params); | ||
requestOptions = this._getRequestOptions(params); | ||
return Request(requestOptions); | ||
}; | ||
return Request(requestOptions); | ||
}; | ||
SmsSender.prototype._getRequestOptions = function(params, httpMethod) { | ||
var options = this.getOptions(), | ||
var options = this.getOptions(), | ||
apiUrl = this._getApiBaseUrl(), | ||
@@ -158,13 +158,13 @@ requestOptions, | ||
apiVersion, | ||
dataType; | ||
dataType; | ||
httpMethod = (httpMethod || options.connection.defaultHttpMethod).toUpperCase(); | ||
fullResponse = options.connection.forceFullResponse; | ||
apiVersion = options.connection.apiVersion; | ||
dataType = options.connection.dataType; | ||
fullResponse = options.connection.forceFullResponse; | ||
apiVersion = options.connection.apiVersion; | ||
dataType = options.connection.dataType; | ||
params = _.merge(params || {}, { | ||
api_v: apiVersion, | ||
format: dataType | ||
}); | ||
}); | ||
@@ -174,16 +174,16 @@ requestOptions = { | ||
resolveWithFullResponse: fullResponse | ||
}; | ||
}; | ||
switch (httpMethod.toUpperCase()) { | ||
case 'GET': | ||
case 'GET': | ||
fullUrl = [ | ||
apiUrl, | ||
this._getQuery(params) | ||
].join('?'); | ||
requestOptions.uri = fullUrl; | ||
break; | ||
this._getQuery(params) | ||
].join('?'); | ||
requestOptions.uri = fullUrl; | ||
break; | ||
case 'POST': | ||
requestOptions.uri = apiUrl; | ||
requestOptions.form = params; | ||
case 'POST': | ||
requestOptions.uri = apiUrl; | ||
requestOptions.form = params; | ||
break; | ||
@@ -198,3 +198,3 @@ | ||
return requestOptions; | ||
return requestOptions; | ||
}; | ||
@@ -204,12 +204,12 @@ | ||
SmsSender.prototype._getApiBaseUrl = function() { | ||
var protocol = this._getApiProtocol(), | ||
host = this._getApiHost(); | ||
var protocol = this._getApiProtocol(), | ||
host = this._getApiHost(); | ||
return [ | ||
protocol, | ||
'://', | ||
'://', | ||
host, | ||
'/' | ||
].join(''); | ||
}; | ||
].join(''); | ||
}; | ||
@@ -219,7 +219,7 @@ | ||
var self = this, | ||
newPrefix; | ||
newPrefix; | ||
return _ | ||
.reduce(params, function(memo, value, key) { | ||
var queryPart; | ||
var queryPart; | ||
@@ -229,3 +229,3 @@ if (_.isArray(value) || _.isPlainObject(value)) { | ||
newPrefix = [ | ||
prefix, | ||
prefix, | ||
'[', | ||
@@ -235,5 +235,5 @@ (key && !_.isNumber(key)) ? key : '', | ||
].join(''); | ||
queryPart = self._getQuery(value, newPrefix); | ||
queryPart = self._getQuery(value, newPrefix); | ||
} else { | ||
queryPart = self._getQuery(value, key); | ||
queryPart = self._getQuery(value, key); | ||
} | ||
@@ -247,43 +247,43 @@ } else { | ||
']=', | ||
value | ||
].join(''); | ||
encodeURIComponent(value) | ||
].join(''); | ||
} else { | ||
queryPart = [ | ||
key, | ||
value | ||
].join('='); | ||
encodeURIComponent(value) | ||
].join('='); | ||
} | ||
} | ||
memo.push(queryPart); | ||
memo.push(queryPart); | ||
return memo; | ||
return memo; | ||
}, []) | ||
.join('&'); | ||
}; | ||
.join('&'); | ||
}; | ||
SmsSender.prototype._getApiHost = function() { | ||
var options = this.getOptions(); | ||
var options = this.getOptions(); | ||
return options.connection.host; | ||
}; | ||
return options.connection.host; | ||
}; | ||
SmsSender.prototype._getApiProtocol = function() { | ||
var options = this.getOptions(); | ||
var options = this.getOptions(); | ||
return options.connection.forceHttps | ||
? 'https' | ||
: 'http'; | ||
}; | ||
return options.connection.forceHttps | ||
? 'https' | ||
: 'http'; | ||
}; | ||
SmsSender.prototype._setOptions = function(options) { | ||
var currentOptions = _.merge(this._getDefaultOptions(), this._options); | ||
var currentOptions = _.merge(this._getDefaultOptions(), this._options); | ||
this._options = _.merge(currentOptions, options); | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
@@ -295,3 +295,3 @@ | ||
connection: { | ||
host: 'api.sms24x7.ru', | ||
host: 'api.sms24x7.ru', | ||
forceHttps: false, | ||
@@ -308,15 +308,15 @@ defaultHttpMethod: 'GET', | ||
} | ||
}; | ||
}; | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
SmsSender.prototype._getDefaultOptions = function() { | ||
return this._defaultOptions; | ||
}; | ||
return this._defaultOptions; | ||
}; | ||
return SmsSender; | ||
return SmsSender; | ||
})(); | ||
})(); |
{ | ||
"name": "sms24x7-client", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Send text messages via SMS using sms24x7.ru", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/devlato/sms24x7", |
11925