Skip to content
Browse files

break functionalities into modules

  • Loading branch information...
1 parent d3b7dc4 commit 073e189ad516f6af9ccc964ebb1bbce004656997 @yacc committed Oct 18, 2013
Showing with 259 additions and 7 deletions.
  1. +35 −2 README.md
  2. +87 −0 lib/carrier.js
  3. +24 −2 lib/main.js
  4. +50 −0 lib/sms_gateway.js
  5. +6 −2 package.json
  6. +31 −0 test/carrier.js
  7. +1 −1 test/main.js
  8. +25 −0 test/sms_gateway.js
View
37 README.md
@@ -1,6 +1,39 @@
node-sms
========
+[![Build Status](https://travis-ci.org/yacc/node-sms.png)](https://travis-ci.org/yacc/node-sms) Email to SMS gateway nodejs modules
-[![Build Status](https://travis-ci.org/yacc/node-sms.png)](https://travis-ci.org/yacc/node-sms)
+Want to send an SMS from your Node application? node-sms allows you to do just that.
+It allows you to send a text message in the form of an e-mail to a cell phone on any of the many supported carriers.
-Email to SMS gateway nodejs modules
+Status
+======
+In active development, not production ready yet.
+ETA for fully functional version end of Oct 2013.
+
+Supported Carrier (US & International)
+======================================
+Alltel, Ameritech, AT&T, Bell Atlantic, BellSouth Mobility, Beeline(UA), BlueSkyFrog, Boost Mobile, BPL Mobile, Cellular South, Claro (Brazil, Nicaragua), Comcast, Du, E-Plus, Etisalat, Fido, kajeet, Mobinil, Mobitel, Movistar, Metro PCS, O2, Orange, Powertel, PSC Wireless, Qwest, Rogers, Southern Link, Sprint, Suncom, T-Mobile (US/UK/Germany), Telefonica, Tracfone, Virgin Mobile, Verizon Wireless, Vodafone (UK, Egypt, Italy, Japan, Spain), and many more …
+
+Some International carriers require that their users subscribe to an Email to SMS feature before they are able to receive SMS messages this way. If one your users mentions that they are not receiving their messages, chances are it is due to this limitation. Some of these carriers are include, Mobitel, Etisalat, T-Mobile (Netherlands)
+
+
+Dependencies
+============
+node-sms relies on nodemailer for sending email.
+node-sms requires a smtpTransport to be defined using whatever transport nodemailer supports.
+
+Wishlist
+========
+* View Helper (list of all supported carrier to build a dropdown menu)
+* Support for other mailers
+
+
+Running Tests
+=============
+change to the root directory of the module, then ran the following command.
+
+ npm test
+
+Impiration
+==========
+this nodejs module was inspired by the "sms-fu" Ruy on Rails plugin https://github.com/yacc/sms-fu
View
87 lib/carrier.js
@@ -0,0 +1,87 @@
+var inflection = require('inflection');
+
+// Expose to the world
+module.exports.Carrier = Carrier;
+
+/**
+ * Hash of available countries with available carriers and their email gateways
+ * follows ISO 3166-1-alpha-2 code
+ * http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_names_and_code_elements.htm
+ */
+Carrier.countries = {
+ "US":{
+ "tmobile": {
+ "name": "T-Mobile",
+ "template": "{{number}}@tmomail.net"
+ },
+ "att":{
+ "name": "AT&T",
+ "template": "{{number}}@txt.att.net"
+ },
+ "verizon":{
+ "name": "Verizon Wireless",
+ "template": "{{number}}@vtext.com"
+ }
+ },
+ "GB":{
+
+ },
+ "FR":{
+
+ }
+};
+
+/**
+ * <p>Generates a Carrier from a country and carrier name</p>
+ *
+ * <p>All Carrier need to have a country and a carrier name property defined
+ *
+ * @constructor
+ * @param {String} country The country of the carrier
+ * @param {String} carrier The symbol for the (derived from the name of the carrier)
+ */
+function Carrier(country, known_as, callback){
+ var code = 0;
+ var msg = '';
+
+ this.country = (country || "").toString().trim().toUpperCase();
+ this.known_as = inflection.underscore( (known_as || "").toString().trim());
+
+ if (this.country in Carrier.countries) {
+ if (this.known_as in Carrier.countries[this.country]) {
+ var carrier = Carrier.countries[this.country][this.known_as];
+ if (carrier) {
+ this.name = carrier.name;
+ this.template = carrier.template;
+ msg = 'Country and carrier supported, yeah !';
+ }
+ } else {
+ code = 1;
+ msg += 'Carrier ' + known_as +' not supported'
+ }
+ } else {
+ code = 1;
+ msg += 'Country ' + country + ' not supported';
+ }
+
+ if(typeof callback == "function"){
+ callback(code?new Error(msg):null, {message: msg, emailTemplate: this.template});
+ }
+};
+
+
+/**
+ * <p>Generates an emails address to reach a cell phone by text</p>
+ *
+ * @param {String} cell phone number
+ */
+Carrier.prototype.email = function(number){
+ this.number = number;
+ //TODO: validate phone number
+ // if iz.phone(number) {
+
+ // }
+ this.email = this.template.replace('{{number}}',this.number);
+};
+
+
View
26 lib/main.js
@@ -1,3 +1,6 @@
+var Carrier = require("./carrier").Carrier;
+var SmsGateway = require("./sms_gateway").SmsGateway;
+
// args : [country_code,phone_number,carrier]
// callback : callback to execute
//
@@ -13,8 +16,27 @@ function sendsms(args,callback) {
if (typeof args[2] != "string")
return process.nextTick(function() {callback('Phone carrier has to be a string');});
- callback('message sent to +' + args[0] + ' ' + args[1] + ' through ' + args[2]);
-}
+ var country = args[0];
+ var phone = args[1];
+ var carrier = args[2];
+ // create reusable transport method (opens pool of SMTP connections)
+ var smtpTransport = ["SMTP",{
+ service: "Gmail",
+ auth: {
+ user: "xxxxx@xxxx.com",
+ pass: "ZuperZecret"
+ }
+ }];
+
+ new SmsGateway(country,carrier,smtpTransport,function(err,sms2email){
+ if (err) {
+ console.error(err.stack);
+ return;
+ } else {
+ sms2email.send(phone,'test message',callback);
+ }
+ });
+}
module.exports = sendsms;
View
50 lib/sms_gateway.js
@@ -0,0 +1,50 @@
+var nodemailer = require("nodemailer");
+var Carrier = require('../lib/carrier').Carrier;
+
+// Expose to the world
+module.exports.SmsGateway = SmsGateway;
+
+/**
+ * <p>Generates an SmsGateway with an email transport </p>
+ *
+ * @constructor
+ * @param {String} country The country of the carrier
+ * @param {String} carrier The carrier common name
+ */
+ function SmsGateway(country, carrier, transport, callback){
+ var code = 0;
+ var msg = 'Email Gateway ready!';
+
+ if (this.carrier = new Carrier(country,carrier,callback)) {
+ if (!(this.transport = nodemailer.createTransport(transport.type,transport.options))){
+ code = 1;
+ msg = 'Failed to initialize email transport layer';
+ };
+ };
+
+ if(typeof callback == "function"){
+ callback(code?new Error(msg):null, {message: msg, carrier: this.carrier});
+ }
+
+}
+
+/**
+ * <p>Sends a </p>
+ *
+ * @param {String} cell phone number
+ * @param {String} body of text message
+ * @param {function} callback function
+*/
+SmsGateway.prototype.send = function(to_number,body,callback){
+ this.to_email = this.carrier.email(to_number);
+ this.body = (body || "").toString().trim();
+
+ // setup e-mail data with unicode symbols
+ var mailOptions = {
+ from: this.from_address,
+ to: this.to_email,
+ text: this.boby,
+ html: '<b>' + this.body + '</b>'
+ }
+ this.transport.sendMail(mailOptions, callback);
+};
View
8 package.json
@@ -25,9 +25,13 @@
"bugs": {
"url": "https://github.com/yacc/node-sms/issues"
},
+ "dependencies":{
+ "nodemailer": "",
+ "inflection": ""
+ },
"devDependencies": {
"mocha": "",
"should": ""
- },
- "bin": { "sleep-sort": "./bin/sendsms" }
+ },
+ "bin": { "sendsms": "./bin/sendsms" }
}
View
31 test/carrier.js
@@ -0,0 +1,31 @@
+var should = require('should');
+var Carrier = require('../lib/carrier').Carrier;
+
+describe('Carrier', function() {
+
+ describe('with unknow country and/or carrier', function() {
+ it('calls the callback with an error message', function(done) {
+ var carrier = new Carrier('USofA', 'Tmobile', function(err,result) {
+ result.should.eql({ message: 'Country USofA not supported', emailTemplate: undefined });
+ done();
+ });
+ });
+ it('calls the callback with an error message', function(done) {
+ var carrier = new Carrier('US', 'TmobileSucks', function(err,result) {
+ result.should.eql({ message: 'Carrier TmobileSucks not supported', emailTemplate: undefined });
+ done();
+ });
+ });
+ });
+
+ describe('with valid arguments', function() {
+ it('generates a valid email', function(done) {
+ var carrier = new Carrier('US', 'Tmobile', function(err,result) {
+ result.should.eql({ message: 'Country and carrier supported, yeah !', emailTemplate: '{{number}}@tmomail.net' });
+ done();
+ });
+ });
+ });
+
+});
+
View
2 test/main.js
@@ -14,7 +14,7 @@ describe('sendsms', function() {
describe('with valid arguments', function() {
it('calls the callback with a success message', function(done) {
- var result = sendsms(['1','6503444444','TMobile'], function(result) {
+ var sms = sendsms(['US','6503444444','TMobile'], function(err,result) {
result.should.eql('message sent to +1 6503444444 through TMobile');
done();
});
View
25 test/sms_gateway.js
@@ -0,0 +1,25 @@
+var should = require('should');
+var SmsGateway = require('../lib/sms_gateway').SmsGateway;
+
+describe('SmsGateway', function() {
+
+ describe('with unknow country and/or SmsGateway', function() {
+ it('calls the callback with an error message', function(done) {
+ var smsGateway = new SmsGateway('USofA', 'Tmobile', ["SMTP",{service: "Gmail", auth: {user: "tt@tt.tt",pass: "passcode"}}],function(err,result) {
+ result.should.eql({"message": "Country USofA not supported","carrier": {"country": "USOFA","known_as": "tmobile"}});
+ done();
+ });
+ });
+ });
+
+ describe('with valid arguments', function() {
+ it('generates a valid email', function(done) {
+ var smsGateway = new SmsGateway('US', 'Tmobile', ["SMTP",{}],function(err,result) {
+ result.should.eql({"message": "Email Gateway ready!","carrier": {"country": "US","known_as": "tmobile","name": "T-Mobile","template": "{{number}}@tmomail.net"}});
+ done();
+ });
+ });
+ });
+
+});
+

0 comments on commit 073e189

Please sign in to comment.
Something went wrong with that request. Please try again.