Created
August 4, 2014 11:00
-
-
Save rudigiesler/c9b6c8c4ce3610d922f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/gmaps-app.js b/gmaps-app.js | |
index 17a52b3..284b1d0 100644 | |
--- a/gmaps-app.js | |
+++ b/gmaps-app.js | |
@@ -10,6 +10,8 @@ go.app = function() { | |
var LocationState = require('go-jsbox-location'); | |
var App = vumigo.App; | |
var EndState = vumigo.states.EndState; | |
+ var ChoiceState = vumigo.states.ChoiceState; | |
+ var Choice = vumigo.states.Choice; | |
var GoogleMaps = App.extend(function(self) { | |
App.call(self, 'states:start_loc'); | |
@@ -26,18 +28,74 @@ go.app = function() { | |
self.states.add('states:end_loc', function(name) { | |
return new LocationState(name, { | |
question: "Where do you want to go?", | |
- next: 'states:end', | |
+ next: 'states:send_dir', | |
store_fields: ["geometry.location"], | |
namespace: 'endlocation' | |
}); | |
}); | |
- self.states.add('states:end', function(name) { | |
+ self.states.add('states:send_dir', function(name) { | |
+ return new ChoiceState(name, { | |
+ question: 'Where should the directions be sent?', | |
+ | |
+ choices: [ | |
+ new Choice('myself', 'Myself'), | |
+ new Choice('other', 'Someone else') | |
+ ], | |
+ | |
+ next: function(choice) { | |
+ return { | |
+ myself:'states:end', | |
+ other:'states:custom_to_addr' | |
+ }[choice.value]; | |
+ } | |
+ }); | |
+ }); | |
+ | |
+ // This function normalizes cellphone number inputs | |
+ self.normalize_msisdn = function(number) { | |
+ // Remove invalid characters | |
+ number = number.replace(/( |[^0-9+])/g, ''); | |
+ // Handle ``00`` case | |
+ number = number.replace(/^0{2}/, '+'); | |
+ var country_code = self.im.config.country_code; | |
+ if(country_code){ | |
+ // Handle ``0`` case | |
+ number = number.replace(/^0/, ['+',country_code].join('')); | |
+ // Add ``+`` | |
+ if(number.match('^' + country_code)) { | |
+ number = ['+',number].join(''); | |
+ } | |
+ } | |
+ return (number.match(/^\+/)) ? number : null; | |
+ }; | |
+ | |
+ self.states.add('states:custom_to_addr', function(name) { | |
+ return new FreeText(name, { | |
+ question:'Please specify the number to recieve the directions:', | |
+ next: function(content){ | |
+ // normalize if the endpoint is cellphone | |
+ if(self.im.config.endpoint === 'sms') { | |
+ content = self.normalize_msisdn(content); | |
+ } | |
+ // go to the end state if the input is valid | |
+ return content ? { | |
+ name: 'states:end', | |
+ creator_opts: {reply:content} | |
+ } : { | |
+ name: 'states:custom_to_addr' | |
+ }; | |
+ } | |
+ }); | |
+ }); | |
+ | |
+ self.states.add('states:end', function(name, opts) { | |
return new EndState(name, { | |
- text: 'Thanks, cheers!', | |
+ text: 'Directions sent!', | |
next: 'states:start' | |
}); | |
}); | |
+ | |
}); | |
return { | |
diff --git a/lib/google-maps.js b/lib/google-maps.js | |
index 66dd55e..1c4aa18 100644 | |
--- a/lib/google-maps.js | |
+++ b/lib/google-maps.js | |
@@ -3,6 +3,8 @@ go.app = function() { | |
var LocationState = require('go-jsbox-location'); | |
var App = vumigo.App; | |
var EndState = vumigo.states.EndState; | |
+ var ChoiceState = vumigo.states.ChoiceState; | |
+ var Choice = vumigo.states.Choice; | |
var GoogleMaps = App.extend(function(self) { | |
App.call(self, 'states:start_loc'); | |
@@ -19,18 +21,74 @@ go.app = function() { | |
self.states.add('states:end_loc', function(name) { | |
return new LocationState(name, { | |
question: "Where do you want to go?", | |
- next: 'states:end', | |
+ next: 'states:send_dir', | |
store_fields: ["geometry.location"], | |
namespace: 'endlocation' | |
}); | |
}); | |
- self.states.add('states:end', function(name) { | |
+ self.states.add('states:send_dir', function(name) { | |
+ return new ChoiceState(name, { | |
+ question: 'Where should the directions be sent?', | |
+ | |
+ choices: [ | |
+ new Choice('myself', 'Myself'), | |
+ new Choice('other', 'Someone else') | |
+ ], | |
+ | |
+ next: function(choice) { | |
+ return { | |
+ myself:'states:end', | |
+ other:'states:custom_to_addr' | |
+ }[choice.value]; | |
+ } | |
+ }); | |
+ }); | |
+ | |
+ // This function normalizes cellphone number inputs | |
+ self.normalize_msisdn = function(number) { | |
+ // Remove invalid characters | |
+ number = number.replace(/( |[^0-9+])/g, ''); | |
+ // Handle ``00`` case | |
+ number = number.replace(/^0{2}/, '+'); | |
+ var country_code = self.im.config.country_code; | |
+ if(country_code){ | |
+ // Handle ``0`` case | |
+ number = number.replace(/^0/, ['+',country_code].join('')); | |
+ // Add ``+`` | |
+ if(number.match('^' + country_code)) { | |
+ number = ['+',number].join(''); | |
+ } | |
+ } | |
+ return (number.match(/^\+/)) ? number : null; | |
+ }; | |
+ | |
+ self.states.add('states:custom_to_addr', function(name) { | |
+ return new FreeText(name, { | |
+ question:'Please specify the number to recieve the directions:', | |
+ next: function(content){ | |
+ // normalize if the endpoint is cellphone | |
+ if(self.im.config.endpoint === 'sms') { | |
+ content = self.normalize_msisdn(content); | |
+ } | |
+ // go to the end state if the input is valid | |
+ return content ? { | |
+ name: 'states:end', | |
+ creator_opts: {reply:content} | |
+ } : { | |
+ name: 'states:custom_to_addr' | |
+ }; | |
+ } | |
+ }); | |
+ }); | |
+ | |
+ self.states.add('states:end', function(name, opts) { | |
return new EndState(name, { | |
- text: 'Thanks, cheers!', | |
+ text: 'Directions sent!', | |
next: 'states:start' | |
}); | |
}); | |
+ | |
}); | |
return { | |
diff --git a/test/test-google-maps.js b/test/test-google-maps.js | |
index a6a405b..64860b3 100644 | |
--- a/test/test-google-maps.js | |
+++ b/test/test-google-maps.js | |
@@ -61,7 +61,9 @@ describe("app", function() { | |
tester | |
.setup.config.app({ | |
- name: 'googlemaps' | |
+ name: 'googlemaps', | |
+ country_code: '27', | |
+ endpoint: 'sms' | |
}) | |
.setup(function(api) { | |
fixtures().forEach(api.http.fixtures.add); | |
@@ -127,5 +129,56 @@ describe("app", function() { | |
}); | |
}); | |
+ describe("When the function ``normalize_msisdn`` is run", function() { | |
+ it("Should remove invalid characters", function() { | |
+ return tester | |
+ .check(function() { | |
+ assert.equal(app.normalize_msisdn('+12ab5 7'), '+1257'); | |
+ }) | |
+ .run(); | |
+ }); | |
+ it("Should handle ``00`` case", function() { | |
+ return tester | |
+ .check(function(){ | |
+ assert.equal(app.normalize_msisdn('0027741234567'), | |
+ '+27741234567'); | |
+ }) | |
+ .run(); | |
+ }); | |
+ it("Should handle the `0` case", function() { | |
+ return tester | |
+ .check(function() { | |
+ assert.equal(app.normalize_msisdn('0741234567'), | |
+ '+27741234567'); | |
+ }) | |
+ .run(); | |
+ }); | |
+ it("Should handle the `0` case", function() { | |
+ return tester | |
+ .check(function() { | |
+ assert.equal(app.normalize_msisdn('0741234567'), | |
+ '+27741234567'); | |
+ }) | |
+ .run(); | |
+ }); | |
+ it("Should add the ``+`` in the case of country code", function() { | |
+ return tester | |
+ .check(function() { | |
+ assert.equal(app.normalize_msisdn('27741234567'), | |
+ '+27741234567'); | |
+ }) | |
+ .run(); | |
+ }); | |
+ it("should return null for incorrect numbers", function() { | |
+ return tester | |
+ .check(function() { | |
+ assert.equal(app.normalize_msisdn('1234'), null); | |
+ }) | |
+ .run(); | |
+ }); | |
+ }); | |
+ | |
+ | |
+ | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment