Skip to content

Commit

Permalink
Implemented /resultspage endpoint along with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
axelniklasson committed Aug 3, 2017
1 parent eb1dc71 commit 643ec62
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
yarn.lock
demo.js
*.swp
test.js
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ Node.js wrapper for Skånetrafiken's Open API. I recently had a night off and th
Skånetrafikens official API documentation can be found [here](http://www.labs.skanetrafiken.se/api.asp) and this wrapper is written for the latest version (v2.2).

## Implemented endpoints
Below are a small set of featured intended to be implemented.
Below are the implemented endpoints.
- [x] /querypage.asp
- [ ] /resultspage.asp
- [x] /resultspage.asp
- [x] /querystation.asp
- [x] /neareststation.asp
- [x] /stationresults.asp
- [x] /trafficmeans.asp
- [ ] /journeypath.asp

Documentation for this package can be found in the Wiki.

## Limitations
### XML to JSON conversion
I am currently using a package called [xml2js](https://www.npmjs.com/package/xml2js) for the conversion between XML and JSON and no other formatting of the data will be provided. It is to be used as-is. In the future, returning data in XML should also be supported.
I am currently using a package called [xml2js](https://www.npmjs.com/package/xml2js) for the conversion between XML and JSON and no other formatting of the data will be provided. It is to be used as-is. In the future, returning data in XML Should also be supported.

## Installation
This package is published to [npm](https://www.npmjs.com/package/node-skanetrafiken) and can easily be installed using
Expand All @@ -30,7 +32,7 @@ npm install node-skanetrafiken
```

## Usage
Below shows a simple example of finding a stop by name. Docs for all endpoints will be added.
Below shows a simple example of finding a stop by name. Docs for all endpoints can be found in the Wiki.
```
// demo.js
Expand All @@ -56,7 +58,7 @@ npm test
```

### Contributing
Pull Requests are always welcome. All PRs should contain appropriate tests and submitted to the ``development`` branch. PRs that break the build in Travis will not be accepted, for obvious reasons.
Pull Requests are always welcome. All PRs Should contain appropriate tests and submitted to the ``development`` branch. PRs that break the build in Travis will not be accepted, for obvious reasons.

## License
This package is MIT licensed.
21 changes: 21 additions & 0 deletions lib/endpoints/resultspage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module.exports = {
*
* @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)
* @param date yymmdd : date of journeys yymmdd (optional)
* @param time hhmm : date of journeys hhmm (optional)
* @param transportMode int : sum of trafficmeans IDs (optional)
* @param cb function : callback function
*
* @returns an array of journyes
Expand All @@ -33,6 +37,23 @@ module.exports = {
reqString += '&selPointFr=' + encodeURIComponent(opts.from.name + '|' + opts.from.id + '|' + opts.from.type);
reqString += '&selPointTo=' + encodeURIComponent(opts.to.name + '|' + opts.to.id + '|' + opts.to.type);

// optional parameters
if ((opts.date && !opts.time) || (!opts.date && opts.time) ) {
return cb(null, 'Parameters date and time must both be present.');
}

if (opts.time && opts.date) {
reqString += '&inpTime=' + encodeURIComponent(opts.inpTime) + '&inpDate=' + encodeURIComponent(opts.date);
}

if (opts.limit) {
reqString += '&NoOf=' + encodeURIComponent(opts.limit);
}

if (opts.transportMode) {
reqString += '&transportMode=' + encodeURIComponent(opts.transportMode);
}

request(reqString, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (config.returnXML) {
Expand Down
16 changes: 12 additions & 4 deletions lib/endpoints/stationresults.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var request = require('request');
var xmlParser = require('xml2js');
var config = require('../config');

module.exports = {
/*
* Finds all departures from a specified stop
*
* @param stopID int : id of the stop
* @param inpDate date : date for departure yymmdd (optional)
* @param inpTime time : time for departure hhmm (optional)
* @param selDirection int : direction - 0 for departures, 1 for arrivals (optional)
* @param date date : date for departure yymmdd (optional)
* @param time time : time for departure hhmm (optional)
* @param arrivals bool : true if arrivals are wanted (optional)
* @param cb function : callback function
*
* @returns an array of departures from the specified stop
Expand All @@ -18,10 +18,18 @@ module.exports = {
if (opts.stopID) {
var reqString = config.baseURL + '/stationresults.asp?selPointFrKey=' + encodeURIComponent(opts.stopID);

if ((opts.date && !opts.time) || (!opts.date && opts.time) ) {
return cb(null, 'Parameters date and time must both be present.');
}

if (opts.date && opts.time) {
reqString += '&inpDate=' + encodeURIComponent(opts.date) + '&inpTime=' + encodeURIComponent(opts.time);
}

if (opts.arrivals) {
reqString += '&selDirection=1';
}

request(reqString, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (config.returnXML) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"scripts": {
"test": "./node_modules/mocha/bin/mocha test",
"local_coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha",
"coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var nodeSkanetrafikenConfig = require('../lib/config');

/* baseURL */
describe('Get baseURL for API', function() {
it('should return correct baseURL', function() {
it('Should return correct baseURL', function() {
expect(nodeSkanetrafikenConfig).to.not.be.empty;
expect(nodeSkanetrafikenConfig.baseURL).to.equal('http://www.labs.skanetrafiken.se/v2.2');
});
Expand Down
6 changes: 3 additions & 3 deletions test/endpoints/neareststation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var nodeSkanetrafiken = require('../../lib/node_skanetrafiken');

/* findNearestStops */
describe('Find nearest stops by names with correct parameters', function() {
it('should successfully return nearest stops', function(done) {
it('Should successfully return nearest stops', function(done) {
nodeSkanetrafiken.findNearestStops({ x: 6167930, y: 1323215, radius: 500 }, function(results, err) {
expect(results).to.not.be.empty;
expect(err).to.be.null;
Expand All @@ -14,7 +14,7 @@ describe('Find nearest stops by names with correct parameters', function() {
});

describe('Find nearest stops by names without radius', function() {
it('should successfully return nearest stops', function(done) {
it('Should successfully return nearest stops', function(done) {
nodeSkanetrafiken.findNearestStops({ x: 6167930, y: 1323215 }, function(results, err) {
expect(results).to.not.be.empty;
expect(err).to.be.null;
Expand All @@ -24,7 +24,7 @@ describe('Find nearest stops by names without radius', function() {
});

describe('Find nearest stops within specified radius', function() {
it('should successfully return nearest stops, all within specified radius', function(done) {
it('Should successfully return nearest stops, all within specified radius', function(done) {
nodeSkanetrafiken.findNearestStops({ x: 6167930, y: 1323215, radius: 500 }, function(results, err) {
expect(results).to.not.be.empty;
expect(err).to.be.null;
Expand Down
8 changes: 4 additions & 4 deletions test/endpoints/querypage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var nodeSkanetrafiken = require('../../lib/node_skanetrafiken');

/* findStartAndStop */
describe('Find starts and stops by names with correct parameters', function() {
it('should successfully return starts and stops matching the query', function(done) {
it('Should successfully return starts and stops matching the query', function(done) {
nodeSkanetrafiken.findStartAndStop({ from: 'Kristianstad', to: 'Lund' }, function(results, err) {
expect(results).to.not.be.empty;
expect(err).to.be.null;
Expand All @@ -14,7 +14,7 @@ describe('Find starts and stops by names with correct parameters', function() {
});

describe('Find starts and stops by name without to parameter', function() {
it('should return error message, since parameters were wrong', function(done) {
it('Should return error message, since parameters were wrong', function(done) {
nodeSkanetrafiken.findStartAndStop({ from: 'Kristianstad' }, function(results, err) {
expect(results).to.be.null;
expect(err).to.equal('Parameters from and to must be specified.');
Expand All @@ -24,7 +24,7 @@ describe('Find starts and stops by name without to parameter', function() {
});

describe('Find starts and stops by name without from parameter', function() {
it('should return error message, since parameters were wrong', function(done) {
it('Should return error message, since parameters were wrong', function(done) {
nodeSkanetrafiken.findStartAndStop({ to: 'Lund' }, function(results, err) {
expect(results).to.be.null;
expect(err).to.equal('Parameters from and to must be specified.');
Expand All @@ -34,7 +34,7 @@ describe('Find starts and stops by name without from parameter', function() {
});

describe('Find starts and stops by name without set parameters', function() {
it('should return error message, since parameters were wrong', function(done) {
it('Should return error message, since parameters were wrong', function(done) {
nodeSkanetrafiken.findStartAndStop({ foo: 'bar', baz: 'qux' }, function(results, err) {
expect(results).to.be.null;
expect(err).to.equal('Parameters from and to must be specified.');
Expand Down
4 changes: 2 additions & 2 deletions test/endpoints/querystation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var nodeSkanetrafiken = require('../../lib/node_skanetrafiken');

/* findStop */
describe('Find stop by name with correct parameters', function() {
it('should successfully return stops matching the query', function(done) {
it('Should successfully return stops matching the query', function(done) {
nodeSkanetrafiken.findStop({ name: 'Kristianstad' }, function(results, err) {
expect(results).to.not.be.empty;
expect(err).to.be.null;
Expand All @@ -14,7 +14,7 @@ describe('Find stop by name with correct parameters', function() {
});

describe('Find stop by name with wrong parameters', function() {
it('should return error message, since query was empty', function(done) {
it('Should return error message, since query was empty', function(done) {
nodeSkanetrafiken.findStop({ foo: 'bar' }, function(results, err) {
expect(results).to.be.null;
expect(err).to.equal('Parameter name must be specified.');
Expand Down
71 changes: 67 additions & 4 deletions test/endpoints/resultspage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var nodeSkanetrafiken = require('../../lib/node_skanetrafiken');

/* getJourneys */
describe('Find journeys with correct parameters', function() {
it('should successfully return journeys matching the query', function(done) {
it('Should successfully return journeys matching the query', function(done) {
nodeSkanetrafiken.getJourneys({
from: { name: 'Malmö', id: 80000, type: 0 },
to: { name: 'Landskrona', id: 82000, type: 0 },
Expand All @@ -18,7 +18,7 @@ describe('Find journeys with correct parameters', function() {
});

describe('Find journeys without correct from parameter', function() {
it('should successfully return journeys matching the query', function(done) {
it('Should successfully return journeys matching the query', function(done) {
nodeSkanetrafiken.getJourneys({
from: { name: 'Malmö', type: 0 },
to: { name: 'Landskrona', id: 82000, type: 0 },
Expand All @@ -33,7 +33,7 @@ describe('Find journeys without correct from parameter', function() {
});

describe('Find journeys without correct to parameter', function() {
it('should fail to return journeys matching the query', function(done) {
it('Should fail to return journeys matching the query', function(done) {
nodeSkanetrafiken.getJourneys({
from: { name: 'Malmö', id: 80000, type: 0 },
to: { name: 'Landskrona', type: 0 },
Expand All @@ -48,7 +48,7 @@ describe('Find journeys without correct to parameter', function() {
});

describe('Find journeys without correct action parameter', function() {
it('should fail tp return journeys matching the query', function(done) {
it('Should fail to return journeys matching the query', function(done) {
nodeSkanetrafiken.getJourneys({
from: { name: 'Malmö', id: 80000, type: 0 },
to: { name: 'Landskrona', id: 82000, type: 0 },
Expand All @@ -61,3 +61,66 @@ describe('Find journeys without correct action parameter', function() {
});
});
});

describe('Find journeys without date but not time', function() {
it('Should fail because both date and time need to be present if one is', function(done) {
nodeSkanetrafiken.getJourneys({
from: { name: 'Malmö', id: 80000, type: 0 },
to: { name: 'Landskrona', id: 82000, type: 0 },
action: 'search',
date: '170727'
}, function(results, err) {
expect(results).to.be.null;
expect(err).to.not.be.null;
expect(err).to.equal('Parameters date and time must both be present.');
done();
});
});
});

describe('Find journeys with date and time', function() {
it('Should successfully return journeys matching date and time', function(done) {
nodeSkanetrafiken.getJourneys({
from: { name: 'Malmö', id: 80000, type: 0 },
to: { name: 'Landskrona', id: 82000, type: 0 },
action: 'search',
date: '170727',
time: '1337'
}, function(results, err) {
expect(results).to.not.be.null;
expect(err).to.be.null;
done();
});
});
});

describe('Find journeys and limit result', function() {
it('Should successfully return [limit] amount of journeys', function(done) {
nodeSkanetrafiken.getJourneys({
from: { name: 'Malmö', id: 80000, type: 0 },
to: { name: 'Landskrona', id: 82000, type: 0 },
action: 'search',
limit: '2'
}, function(results, err) {
expect(results).to.not.be.null;
expect(results).to.have.lengthOf(2);
expect(err).to.be.null;
done();
});
});
});

describe('Find journeys in specified transport mode', function() {
it('Should successfully return journeys', function(done) {
nodeSkanetrafiken.getJourneys({
from: { name: 'Malmö', id: 80000, type: 0 },
to: { name: 'Landskrona', id: 82000, type: 0 },
action: 'search',
transportMode: 16
}, function(results, err) {
expect(results).to.not.be.null;
expect(err).to.be.null;
done();
});
});
});
45 changes: 43 additions & 2 deletions test/endpoints/stationresults.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var nodeSkanetrafiken = require('../../lib/node_skanetrafiken');

/* getDepartures */
describe('Find departures from a given stop.', function() {
it('should successfully return upcoming departures, all from specified stop', function(done) {
it('Should successfully return upcoming departures, all from specified stop', function(done) {
nodeSkanetrafiken.getDepartures({ stopID: 90042 }, function(results, err) {
expect(results).to.not.be.null;
expect(err).to.be.null;
Expand All @@ -14,7 +14,7 @@ describe('Find departures from a given stop.', function() {
});

describe('Find departures from a given stop, without specifying stopID.', function() {
it('should fail, since stopID is a mandatory parameter', function(done) {
it('Should fail, since stopID is a mandatory parameter', function(done) {
nodeSkanetrafiken.getDepartures({ foo: 'bar' }, function(results, err) {
expect(results).to.be.null;
expect(err).to.not.be.null;
Expand All @@ -24,3 +24,44 @@ describe('Find departures from a given stop, without specifying stopID.', functi
});
});

describe('Find departures from a given stop, when specifying date and time.', function() {
it('Should return departures from a given stop, matching date and time', function(done) {
nodeSkanetrafiken.getDepartures({ stopID: 90042, date: '170802', time: '1337' }, function(results, err) {
expect(results).to.not.be.null;
expect(err).to.be.null;
done();
});
});
});

describe('Find departures from a given stop, when specifying date but not time.', function() {
it('Should fail, since date and time both need to be specified if one is', function(done) {
nodeSkanetrafiken.getDepartures({ stopID: 90042, date: '170802' }, function(results, err) {
expect(results).to.be.null;
expect(err).to.not.be.null;
expect(err).to.equal('Parameters date and time must both be present.');
done();
});
});
});

describe('Find departures from a given stop, when specifying time but not date.', function() {
it('Should fail, since date and time both need to be specified if one is', function(done) {
nodeSkanetrafiken.getDepartures({ stopID: 90042, time: '1337' }, function(results, err) {
expect(results).to.be.null;
expect(err).to.not.be.null;
expect(err).to.equal('Parameters date and time must both be present.');
done();
});
});
});

describe('Find arrivals from a given stop', function() {
it('Should return arrivals from given stop', function(done) {
nodeSkanetrafiken.getDepartures({ stopID: 90042, arrivals: true }, function(results, err) {
expect(results).to.not.be.null;
expect(err).to.be.null;
done();
});
});
});
2 changes: 1 addition & 1 deletion test/endpoints/trafficmeans.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var nodeSkanetrafiken = require('../../lib/node_skanetrafiken');

/* getTrafficMeans */
describe('Get traffic means without options', function() {
it('should successfully return traffic means', function(done) {
it('Should successfully return traffic means', function(done) {
nodeSkanetrafiken.getTrafficMeans(function(results, err) {
expect(results).to.not.be.empty;
expect(err).to.be.null;
Expand Down
Loading

0 comments on commit 643ec62

Please sign in to comment.