From 901c56b77e46e31b0458778eee7e705a8725f212 Mon Sep 17 00:00:00 2001 From: Stanley Zheng Date: Wed, 3 Dec 2014 16:59:47 -0500 Subject: [PATCH 01/12] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ecb554e..98c7304 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ npm install node app.js ``` - + HRT BUS API From 4e8ca687ba3d49d4ea928704a9c2c95bd80878fb Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Tue, 16 Dec 2014 16:41:45 -0500 Subject: [PATCH 02/12] adding mocha --- package.json | 5 ++++- test/test.js | 0 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 test/test.js diff --git a/package.json b/package.json index 6e9627c..405a69b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "hrt-bus-text-my-bus ===================", "main": "app.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha test" }, "repository": { "type": "git", @@ -27,5 +27,8 @@ "request": "^2.45.0", "twilio": "^1.7.0", "where": "^0.1.2" + }, + "devDependencies": { + "mocha": "^2.0.1" } } diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..e69de29 From 0db2a8c8e99300481ea0a952891d1da40608cc54 Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Tue, 16 Dec 2014 16:42:00 -0500 Subject: [PATCH 03/12] my editor hates trailing whitespace --- app.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/app.js b/app.js index 08fce3f..917ab6d 100644 --- a/app.js +++ b/app.js @@ -39,15 +39,15 @@ // Asking for help. (/help/i.test(text) ? - getResponse('Hi, thanks for texting your HRT bus! Please text an ' - + 'address or a stop number to find the next time your bus will' + getResponse('Hi, thanks for texting your HRT bus! Please text an ' + + 'address or a stop number to find the next time your bus will' + ' come your way.') : // Check for an address. hasAddress(text) ? getStop(text).then(getTimes).then(getResponse) : // Checks for a bus stop number, like http://hrtb.us/#stops/0263 - hasStop(text) ? - getTimes(text).then(getResponse) : + hasStop(text) ? + getTimes(text).then(getResponse) : // Otherwise, give a help(ful) message. getResponse('Hi! You have texted the Text Your Bus application. I ' + 'do not quite understand your request. Please text "help" ' @@ -95,7 +95,7 @@ if (bounded.length === 0) { return resolve('0'); } else { - r.get('http://api.hrtb.us/api/stops/near/' + r.get('http://api.hrtb.us/api/stops/near/' + point.lat + '/' + point.lon, function (error, response, body) { return resolve(closestStop(JSON.parse(body) @@ -111,7 +111,7 @@ * Seven Cities area. * * @param {Array} points - The points returned from Nominatim. - * + * * @return {Array} The points local to the Seven Cities. **/ var bound = function (points) { @@ -147,11 +147,11 @@ * @return {Number} The distance from x to y. **/ var distance = function (x, y) { - return Math.sqrt(Math.pow(x.location[0] - y.location[0], 2) + return Math.sqrt(Math.pow(x.location[0] - y.location[0], 2) + Math.pow(x.location[1] - y.location[1], 2)); }; - /** + /** * Makes API request to the HRTB.US API and transform the json results. * * @param {String} stop - The stop to get times for. @@ -163,7 +163,7 @@ return new bPromise(function (resolve, reject) { r.get("http://api.hrtb.us/api/stop_times/" + stop, function (err, response, body) { - /* If we cannot parse the times, provide a helpful error + /* If we cannot parse the times, provide a helpful error * message. */ return resolve(body === '[]' || JSON.parse(body) === [] ? 'Hmm, I cannot tell if that stop does not exist or if ' @@ -172,7 +172,7 @@ + 'stop you mentioned any time soon.' : showTimes(JSON.parse(body))); }); - }); + }); }; /** @@ -189,15 +189,15 @@ // EVMS/NORFOLK will be here in about 10, the next one in 15. // NEWTOWN ROAD will be here in about 5 minutes and the next. return _.reduce(times, function (response, time) { - return { + return { '0': 'Light rail' , '3': 'Bus' - , '4': 'Ferry' - }[time.drop_off_type] + ' ' - + time.routeShortName + ' to ' + time.destination - + ' will arrive in ' + , '4': 'Ferry' + }[time.drop_off_type] + ' ' + + time.routeShortName + ' to ' + time.destination + + ' will arrive in ' + moment.utc(time.arrival_time).diff(moment.utc() - , 'minutes') + , 'minutes') + ' minutes.'; }, ''); }).join('\n'); @@ -214,7 +214,7 @@ return new bPromise(function (resolve, reject) { console.log(message); resolve('\n' - + '\n' + message + + '\n' + message + '\n'); }); }; From 6e2bd5750204bee4feecb6f32e45cfb5f8ed924b Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Wed, 17 Dec 2014 09:07:35 -0500 Subject: [PATCH 04/12] adding additional tests to be written --- test/test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test.js b/test/test.js index e69de29..71df5df 100644 --- a/test/test.js +++ b/test/test.js @@ -0,0 +1,16 @@ +var assert = require("assert"); +var request = require("request"); + +/** +* Test +* +* @param {Object} req - The request value. +**/ + +// #TODO Tests for +// Bounding Boxes +// Asking for help +// Checking for input a known address +// Checking for input a unknown/invalid address +// Check for known stop number +// Check for unknown/invalid stop number From 7f69e20b568d73f6f318eeb28d79be2c8a954f4c Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Wed, 17 Dec 2014 16:35:55 -0500 Subject: [PATCH 05/12] return app object and missed semi colon --- app.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 917ab6d..42f0a80 100644 --- a/app.js +++ b/app.js @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -(function () { +module.exports = (function () { var r = require("request"), express = require("express"), app = express(), @@ -76,7 +76,7 @@ * @return {Boolean} Does the body have an address? **/ var hasAddress = function (text) { - return /^\s*(\d*)\s*(([a-z]+[.]?|\d*(1st|[23]n?d|[4-9]th))\s*)+(([a-z]+[.]?)\s*,?)?\s*(([a-z]*\s*)*,?)?\s*(V(irgini)?a)?\s*$/i.test(text) + return /^\s*(\d*)\s*(([a-z]+[.]?|\d*(1st|[23]n?d|[4-9]th))\s*)+(([a-z]+[.]?)\s*,?)?\s*(([a-z]*\s*)*,?)?\s*(V(irgini)?a)?\s*$/i.test(text); }; /** @@ -234,4 +234,5 @@ app.listen(app.get('port'), function() { console.log('Express server listening on port ' + app.get('port')); }); + return app; }()); From 213cdb6f99b7c8fc793270451846030a8c7e121c Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Wed, 17 Dec 2014 16:39:37 -0500 Subject: [PATCH 06/12] adding tests via supertest pattern, can make some general assertions on what to expect --- package.json | 3 +-- test/test.js | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 405a69b..d575716 100644 --- a/package.json +++ b/package.json @@ -17,14 +17,13 @@ }, "homepage": "https://github.com/stanzheng/hrt-bus-text-my-bus", "dependencies": { - "bluebird": "^2.3.10", "body-parser": "^1.9.0", "express": "^4.9.7", "lodash": "^2.4.1", "moment": "^2.8.3", "path": "^0.4.9", "querystring": "^0.2.0", - "request": "^2.45.0", + "supertest": "^0.15.0", "twilio": "^1.7.0", "where": "^0.1.2" }, diff --git a/test/test.js b/test/test.js index 71df5df..0916e2a 100644 --- a/test/test.js +++ b/test/test.js @@ -1,11 +1,31 @@ var assert = require("assert"); -var request = require("request"); +var express = require("express"); +var r = require("supertest"); /** * Test * * @param {Object} req - The request value. **/ +process.env.PORT = 4000; +var app = require("../app.js"); + + +describe('General incoming request', function(){ + it('respond with xml', function(done){ + r(app) + .post('/msg') + .send({Body:"8004"}) + .set('Accept', 'application/xml') + .expect(200, done); + }); +}); + +function clientError(e) { + return e.code >= 400 && e.code < 500; +} + + // #TODO Tests for // Bounding Boxes From 319c8be638639378910cff315d7d470fee33e06e Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Wed, 17 Dec 2014 16:43:21 -0500 Subject: [PATCH 07/12] adding travis ci file --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..18ae2d8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - "0.11" + - "0.10" From 8dbd9223b81ec4a864096fbc378a94f9e03d4509 Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Wed, 17 Dec 2014 17:02:35 -0500 Subject: [PATCH 08/12] forgot was using request and bluebird within the application doh .. --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index d575716..dfbd20f 100644 --- a/package.json +++ b/package.json @@ -17,12 +17,14 @@ }, "homepage": "https://github.com/stanzheng/hrt-bus-text-my-bus", "dependencies": { + "bluebird": "^2.3.11", "body-parser": "^1.9.0", "express": "^4.9.7", "lodash": "^2.4.1", "moment": "^2.8.3", "path": "^0.4.9", "querystring": "^0.2.0", + "request": "^2.51.0", "supertest": "^0.15.0", "twilio": "^1.7.0", "where": "^0.1.2" From 144b22bf2d485183cfd8e76e7aaa8e8eb3790d7d Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Wed, 17 Dec 2014 17:04:50 -0500 Subject: [PATCH 09/12] trick to make travis ci super fast --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 18ae2d8..ef497d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: node_js +sudo: false node_js: - "0.11" - "0.10" From fbfcff5db3827756c97ecbaf39a9f324d142f534 Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Wed, 17 Dec 2014 17:12:06 -0500 Subject: [PATCH 10/12] adding special img flag --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 98c7304..eddf049 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ hrt-bus-text-my-bus =================== +[![Build Status](https://travis-ci.org/Code4HR/hrt-bus-text-my-bus.svg?branch=master)](https://travis-ci.org/Code4HR/hrt-bus-text-my-bus) + STATUS: pre-alpha need more testing phone number: 757-913-5000 From d40c0ecfdeba5c714d225cc30088f2eeac88f379 Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Wed, 17 Dec 2014 18:00:40 -0500 Subject: [PATCH 11/12] call done itself --- package.json | 2 +- test/test.js | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index dfbd20f..9a7e6b0 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "url": "https://github.com/stanzheng/hrt-bus-text-my-bus.git" }, "author": "", - "license": "ISC", + "license": "APACHE", "bugs": { "url": "https://github.com/stanzheng/hrt-bus-text-my-bus/issues" }, diff --git a/test/test.js b/test/test.js index 0916e2a..213cfe9 100644 --- a/test/test.js +++ b/test/test.js @@ -12,15 +12,20 @@ var app = require("../app.js"); describe('General incoming request', function(){ - it('respond with xml', function(done){ + it('responds with xml', function(done){ r(app) .post('/msg') .send({Body:"8004"}) .set('Accept', 'application/xml') - .expect(200, done); + .expect(200) + .end(function(err, res){ + if (err) throw err; + done(); + }); }); }); + function clientError(e) { return e.code >= 400 && e.code < 500; } From 28ddce5801f5bc2543aad06a53943bdbe2bf9963 Mon Sep 17 00:00:00 2001 From: stanley zheng Date: Wed, 17 Dec 2014 20:09:48 -0500 Subject: [PATCH 12/12] adding test for light rail stop --- package.json | 4 +++- test/test.js | 32 +++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 9a7e6b0..07103c4 100644 --- a/package.json +++ b/package.json @@ -25,9 +25,11 @@ "path": "^0.4.9", "querystring": "^0.2.0", "request": "^2.51.0", + "should": "^4.4.1", "supertest": "^0.15.0", "twilio": "^1.7.0", - "where": "^0.1.2" + "where": "^0.1.2", + "xml2js": "^0.4.4" }, "devDependencies": { "mocha": "^2.0.1" diff --git a/test/test.js b/test/test.js index 213cfe9..5de4599 100644 --- a/test/test.js +++ b/test/test.js @@ -1,7 +1,8 @@ var assert = require("assert"); var express = require("express"); var r = require("supertest"); - +var parseString = require('xml2js').parseString; +var should = require('should'); /** * Test * @@ -12,6 +13,7 @@ var app = require("../app.js"); describe('General incoming request', function(){ + it('responds with xml', function(done){ r(app) .post('/msg') @@ -23,13 +25,29 @@ describe('General incoming request', function(){ done(); }); }); -}); - - -function clientError(e) { - return e.code >= 400 && e.code < 500; -} + it('light rail stops are valid, testing 8004', function(done){ + r(app) + .post('/msg') + .send({Body:"8004"}) + .set('Accept', 'application/xml') + .expect(200) + .end(function(err, res){ + if (err) throw err; + should.not.exist(err); + + // cleans utf8 parsing for xml in form + // see http://www.multiasking.com/blog/xml2js-sax-js-non-whitespace-before-first-tag/ + var xml = res.text.replace("\ufeff", ""); + parseString(xml, function (err, ouput) { + var parse = ouput.Response.Message[0]; + parse.should.startWith('Light rail '); + //parse.should.have("Light rail"); + }); + done(); + }); + }); +}); // #TODO Tests for