Skip to content

Commit

Permalink
Merge pull request #4 from akoenig/feature/http-load
Browse files Browse the repository at this point in the history
Functionality for parsing external files
  • Loading branch information
elliotstokes committed Aug 12, 2014
2 parents 29cc582 + a03b8cd commit 250d6c1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ gpxParse.parseGpx("<gpx></gpx>", function(error, data) {
//do stuff
});

// or an external file via HTTP(S)
gpxParse.parseRemoteGpxFile("http://host.tld/my.gpx", function(error, data) {
//do stuff
});

```

#Tests
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = process.env.GPXPARSE_COV ? require('./lib-cov/gpx-parse') : require('./lib/gpx-parse');
module.exports = require('./lib/gpx-parse');
38 changes: 37 additions & 1 deletion lib/gpx-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* @module gpx-parse
**/

var fs = require("fs"),
var assert = require("assert"),
fs = require("fs"),
url = require("url"),
http = require("http"),
https = require("https"),
GpxResult = require('./gpxResult'),
GpxExtent = require('./gpxExtent'),
GpxWaypoint = require('./gpxWaypoint'),
Expand Down Expand Up @@ -176,6 +180,38 @@ exports.parseGpxFromFile = function(gpxFile, callback) {
});
};

/**
* Fetch a remote GPX file via HTTP(S) and parse it.
*
* @param {string} uri The URI of the remote gpx file.
* @param {function} callback Invoked as `callback(err, data)`
*
*/
exports.parseRemoteGpxFile = function parseRemoteGpxFile (uri, callback) {
assert.equal(typeof uri, 'string', 'uri should be a string');
assert.equal(typeof callback, 'function', 'callback should be a function');

function onError (err) {
return callback(new Error('failed to fetch gpx file: ' + err.toString()));
}

function onResponse (res) {
var body = '';

res.on('data', function (data) {
body = body + data;
});

res.once('end', function () {
exports.parseGpx(body, callback);
});
}

(('https:' === url.parse(uri).protocol) ? https : http)
.get(uri, onResponse)
.once('error', onError);
};

//expose objects
exports.GpxResult = GpxResult;
exports.GpxExtent = GpxExtent;
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
"geo"
],
"dependencies": {
"xml2js": "*"
"xml2js": "^0.4.4"
},
"repository": {
"type": "git",
"url": "https://github.com/elliotstokes/gpx-parse.git"
},
"devDependencies": {
"nodeunit": "*",
"sandboxed-module": "*",
"jscoverage": "*",
"coveralls": "*",
"grunt": "^0.4.5",
"grunt-jsdoc": "^0.5.4"
"grunt-jsdoc": "^0.5.4",
"jscoverage": "*",
"nodeunit": "*",
"sandboxed-module": "*",
"server-destroy": "^1.0.0",
"st": "^0.5.1"
},
"scripts": {
"test": "node_modules/.bin/nodeunit tests/",
Expand Down
38 changes: 38 additions & 0 deletions tests/gpx-http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var st = require('st')
var http = require('http')

var enableDestroy = require('server-destroy');

var gpxParse = require("../");

var server;

module.exports = {

setUp: function(callback) {
server = http.createServer(
st(__dirname + '/integration')
).listen(9090, function () {
enableDestroy(server);

callback();
});
},
tearDown: function(callback) {
// clean up
server.destroy();

callback();
},

"Test that remote gpx file can be loaded": function(test) {
gpxParse.parseRemoteGpxFile('http://localhost:9090/data/route-1.1.gpx', function (err, data) {
test.ok(data.metadata);
test.ok(data.waypoints);
test.ok(data.routes);
test.ok(data.tracks);

test.done();
});
}
};

0 comments on commit 250d6c1

Please sign in to comment.