Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #3

Merged
merged 2 commits into from
Aug 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Below are the implemented endpoints.
- [x] /neareststation.asp
- [x] /stationresults.asp
- [x] /trafficmeans.asp
- [ ] /journeypath.asp
- [x] /journeypath.asp

Documentation for this package can be found in the Wiki.

Expand Down
38 changes: 37 additions & 1 deletion lib/endpoints/journeypath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,41 @@ var xmlParser = require('xml2js');
var config = require('../config');

module.exports = {
// TODO implement this endpoint wrapper
/*
* Returns coordinates for journeypath on a map.
*
* @param cf string : JourneyKey retrieved in response from resultspage method
* param id string : SequenceNo of journey retrieved in response from resultspage method
* @param cb function : callback function
*
* @returns SOMETHING COOL
*/
getJourneyPath: function(opts, cb) {
if (opts.journeyKey && opts.sequenceNo) {
var reqString = config.baseURL + '/journeypath.asp?cf=' + encodeURIComponent(opts.journeyKey) + '&id=' + encodeURIComponent(opts.sequenceNo);

request(reqString, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (config.returnXML) {
/* istanbul ignore next */
return cb(body, null);
} else {
xmlParser.parseString(body, function (err, result) {
if (!err && result) {
return cb(result['soap:Envelope']['soap:Body'][0]['GetJourneyPathResponse'][0]['GetJourneyPathResult'], null);
} else {
/* istanbul ignore next */
return cb(null, error);
}
});
}
} else {
/* istanbul ignore next */
return cb(null, error);
}
});
} else {
return cb(null, 'Parameters journeyKey and sequenceNo must be specified.');
}
}
}
4 changes: 3 additions & 1 deletion lib/endpoints/neareststation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ module.exports = {
request(reqString, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (config.returnXML) {
/* istanbul ignore next */
return cb(body, null);
} else {
xmlParser.parseString(body, function (err, result) {
if (!err && result) {
return cb(result['soap:Envelope']['soap:Body'][0]['GetNearestStopAreaResponse'][0]['GetNearestStopAreaResult'][0]['NearestStopAreas'][0]['NearestStopArea'], null);
} else {
/* istanbul ignore next */
return cb(null, err);
}
});
}

} else {
/* istanbul ignore next */
return cb(null, error);
}
});
Expand All @@ -46,4 +49,3 @@ module.exports = {
}
}
}

5 changes: 4 additions & 1 deletion lib/endpoints/querypage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ module.exports = {
* @returns an array of from- and to-stops
*/
findStartAndStop: function(opts, cb) {
if (opts.from && opts.to && typeof cb === 'function') {
if (opts.from && opts.to) {
var reqString = config.baseURL + '/querypage.asp?inpPointfr=' + encodeURIComponent(opts.from)
+ '&inpPointTo=' + encodeURIComponent(opts.to);

request(reqString, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (config.returnXML) {
/* istanbul ignore next */
return cb(body, null)
} else {
xmlParser.parseString(body, function (err, result) {
Expand All @@ -30,12 +31,14 @@ module.exports = {
resultObj.to = result['soap:Envelope']['soap:Body'][0]['GetStartEndPointResponse'][0]['GetStartEndPointResult'][0]['EndPoints'][0]['Point'];
return cb(resultObj, null);
} else {
/* istanbul ignore next */
return cb(null, error);
}
});
}

} else {
/* istanbul ignore next */
return cb(null, error);
}
});
Expand Down
5 changes: 4 additions & 1 deletion lib/endpoints/querystation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ module.exports = {
* @returns an array of stops
*/
findStop: function(opts, cb) {
if (opts.name && typeof cb === 'function') {
if (opts.name) {
var reqString = config.baseURL + '/querystation.asp?inpPointfr=' + encodeURIComponent(opts.name);

request(reqString, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (config.returnXML) {
/* istanbul ignore next */
return cb(body, null);
} else {
xmlParser.parseString(body, function (err, result) {
if (!err && result) {
return cb(result['soap:Envelope']['soap:Body'][0]['GetStartEndPointResponse'][0]['GetStartEndPointResult'][0]['StartPoints'][0]['Point'], null);
} else {
/* istanbul ignore next */
return cb(null, error);
}
});
}
} else {
/* istanbul ignore next */
return cb(null, error);
}
});
Expand Down
10 changes: 8 additions & 2 deletions lib/endpoints/resultspage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
/*
* Finds all journeys between two points.
*
* @param next string : next/previous set of journeys
* @param selPointFr obj : object containing info of departure point name|id|type
* @param selPointTo obj : object containing info of arrival point name|id|type
* @param limit int : number of journeys (optional)
Expand All @@ -14,10 +15,10 @@ module.exports = {
* @param transportMode int : sum of trafficmeans IDs (optional)
* @param cb function : callback function
*
* @returns an array of journyes
* @returns an array of journies
*/
getJourneys: function(opts, cb) {
if (opts.from && opts.to && opts.action && typeof cb === 'function') {
if (opts.from && opts.to && opts.action) {
// validate opts.from
if (!opts.from.name || !opts.from.id || (opts.from.type != 0 && opts.from.type != 1)) {
return cb(null, 'Invalid opts.from object. Should have properties name, id and type.');
Expand Down Expand Up @@ -57,20 +58,25 @@ module.exports = {
request(reqString, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (config.returnXML) {
/* istanbul ignore next */
return cb(body, null);
} else {
xmlParser.parseString(body, function (err, result) {
if (!err && result) {
return cb(result['soap:Envelope']['soap:Body'][0]['GetJourneyResponse'][0]['GetJourneyResult'][0]['Journeys'][0]['Journey'], null);
} else {
/* istanbul ignore next */
return cb(null, error);
}
});
}
} else {
/* istanbul ignore next */
return cb(null, error);
}
});
} else {
return cb(null, 'Invalid opts object. Should have objects from and to and action.');
}
}
}
3 changes: 3 additions & 0 deletions lib/endpoints/stationresults.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@ module.exports = {
request(reqString, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (config.returnXML) {
/* istanbul ignore next */
return cb(body, null);
} else {
xmlParser.parseString(body, function (err, result) {
if (!err && result) {
return cb(result['soap:Envelope']['soap:Body'][0]['GetDepartureArrivalResponse'][0]['GetDepartureArrivalResult'][0]['Lines'][0].Line, null);
} else {
/* istanbul ignore next */
return cb(null, err);
}
});
}

} else {
/* istanbul ignore next */
return cb(null, error);
}
});
Expand Down
5 changes: 4 additions & 1 deletion lib/endpoints/trafficmeans.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {

/*
* Finds current traffic means.
*
*
* @returns an array of traffic means
*/
getTrafficMeans: function(cb) {
Expand All @@ -15,18 +15,21 @@ module.exports = {
request(reqString, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (config.returnXML) {
/* istanbul ignore next */
return cb(body, null)
} else {
xmlParser.parseString(body, function (err, result) {
if (!err && result) {
return cb(result['soap:Envelope']['soap:Body'][0]['GetMeansOfTransportResponse'][0]['GetMeansOfTransportResult'][0]['TransportModes'], null);
} else {
/* istanbul ignore next */
return cb(null, error);
}
});
}

} else {
/* istanbul ignore next */
return cb(null, error);
}
});
Expand Down
4 changes: 4 additions & 0 deletions lib/node_skanetrafiken.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var neareststation = require('./endpoints/neareststation');
var stationresults = require('./endpoints/stationresults');
var trafficmeans = require('./endpoints/trafficmeans');
var resultspage = require('./endpoints/resultspage');
var journeypath = require('./endpoints/journeypath');

function validate(func, opts, cb, emptyOptsOk) {
if (opts && cb && typeof cb === 'function' && !emptyOptsOk) {
Expand Down Expand Up @@ -51,5 +52,8 @@ module.exports = {
},
getJourneys: function(opts, cb) {
return validate(resultspage.getJourneys, opts, cb, false);
},
getJourneyPath: function(opts, cb) {
return validate(journeypath.getJourneyPath, opts, cb, false);
}
};
21 changes: 20 additions & 1 deletion test/endpoints/journeypath.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@ var chai = require('chai');
var expect = chai.expect;
var nodeSkanetrafiken = require('../../lib/node_skanetrafiken');

// TODO implement tests
/* getJourneyPath- dummy tests for now */
describe('Get journeypath with correct parameters', function() {
it('Should successfully (if the API will start working) return journeypath matching the query', function(done) {
nodeSkanetrafiken.getJourneyPath({ journeyKey: '13632115750062105252600390800220033', sequenceNo: '0' }, function(results, err) {
expect(results).to.not.be.null;
expect(err).to.be.null;
done();
});
});
});

describe('Get journeypath without correct parameters', function() {
it('Should fail to return journeypath', function(done) {
nodeSkanetrafiken.getJourneyPath({ sequenceNo: 0 }, function(results, err) {
expect(results).to.be.null;
expect(err).to.equal('Parameters journeyKey and sequenceNo must be specified.');
done();
});
});
});
9 changes: 9 additions & 0 deletions test/endpoints/neareststation.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ describe('Find nearest stops within specified radius', function() {
});
});

describe('Find nearest stops without specifying x and y', function() {
it('Should fail to return nearest stops', function(done) {
nodeSkanetrafiken.findNearestStops({ radius: 500 }, function(results, err) {
expect(results).to.be.null;
expect(err).to.equal('Parameters x and y must be specified.');
done();
});
});
});
10 changes: 10 additions & 0 deletions test/endpoints/resultspage.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,13 @@ describe('Find journeys in specified transport mode', function() {
});
});
});

describe('Find journeys with missing parameters', function() {
it('Should fail to return journeys', function(done) {
nodeSkanetrafiken.getJourneys({ from: {} }, function(results, err) {
expect(results).to.be.null;
expect(err).to.equal('Invalid opts object. Should have objects from and to and action.');
done();
});
});
});