From f7044d2435b5baa343176661a6cdd8238aff152c Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Mon, 7 Sep 2015 22:41:20 -0400 Subject: [PATCH 1/2] Add content for `attribution` static endpoint Fixes #212 --- controller/index.js | 31 ------------------------------- controller/markdownToHtml.js | 28 ++++++++++++++++++++++++++++ middleware/geocodeJSON.js | 8 +------- DOCS.md => public/apiDoc.md | 0 public/attribution.md | 7 +++++++ routes/v1.js | 9 +++++++-- test/unit/controller/index.js | 20 +++++++++++--------- 7 files changed, 54 insertions(+), 49 deletions(-) delete mode 100644 controller/index.js create mode 100644 controller/markdownToHtml.js rename DOCS.md => public/apiDoc.md (100%) create mode 100644 public/attribution.md diff --git a/controller/index.js b/controller/index.js deleted file mode 100644 index d900793f..00000000 --- a/controller/index.js +++ /dev/null @@ -1,31 +0,0 @@ - -var pkg = require('../package'); -var markdown = require('markdown').markdown; -var fs = require('fs'); - -function setup(){ - - var styleString = ''; - var text = '# Pelias API\n'; - text += '### Version: ['+ pkg.version+ '](https://github.com/pelias/api/releases)\n'; - text += fs.readFileSync( './DOCS.md', 'utf8'); - var indexHtml = styleString + markdown.toHTML(text); - - function controller( req, res, next ) { - if (req.accepts('html')) { - res.send(indexHtml); - return; - } - // default behaviour - res.json({ - name: pkg.name, - version: { - number: pkg.version - } - }); - } - - return controller; -} - -module.exports = setup; diff --git a/controller/markdownToHtml.js b/controller/markdownToHtml.js new file mode 100644 index 00000000..656afe01 --- /dev/null +++ b/controller/markdownToHtml.js @@ -0,0 +1,28 @@ + +var markdown = require('markdown').markdown; +var fs = require('fs'); + +function setup(peliasConfig, markdownFile){ + + var styleString = ''; + var text = '# Pelias API\n'; + text += '### Version: [' + peliasConfig.version + '](https://github.com/pelias/api/releases)\n'; + text += fs.readFileSync( markdownFile, 'utf8'); + var html = styleString + markdown.toHTML(text); + + function controller( req, res ) { + if (req.accepts('html')) { + res.send(html); + return; + } + // default behaviour + res.json({ + markdown: text, + html: html + }); + } + + return controller; +} + +module.exports = setup; diff --git a/middleware/geocodeJSON.js b/middleware/geocodeJSON.js index da6cfdf6..d2f58b46 100644 --- a/middleware/geocodeJSON.js +++ b/middleware/geocodeJSON.js @@ -25,16 +25,10 @@ function convertToGeocodeJSON(peliasConfig, req, next) { // the GeocodeJSON spec that is implemented by this instance. req.results.geojson.geocoding.version = '0.1'; - // OPTIONAL. Default: null. The licence of the data. In case of multiple sources, - // and then multiple licences, can be an object with one key by source. - // Can be a freeform text property describing the licensing details. - // Can be a URI on the server, which outlines licensing details. - req.results.geojson.geocoding.license = peliasConfig.host + '/license'; - // OPTIONAL. Default: null. The attribution of the data. In case of multiple sources, // and then multiple attributions, can be an object with one key by source. // Can be a URI on the server, which outlines attribution details. - req.results.geojson.geocoding.attribution = peliasConfig.host + '/attribution'; + req.results.geojson.geocoding.attribution = peliasConfig.host + 'attribution'; // OPTIONAL. Default: null. The query that has been issued to trigger the // search. diff --git a/DOCS.md b/public/apiDoc.md similarity index 100% rename from DOCS.md rename to public/apiDoc.md diff --git a/public/attribution.md b/public/attribution.md new file mode 100644 index 00000000..db9c392e --- /dev/null +++ b/public/attribution.md @@ -0,0 +1,7 @@ +## Attribution +* Geocoding by [Pelias](https://mapzen.com/pelias) from [Mapzen](https://mapzen.com) +* Data from + * [OpenStreetMap](http://www.openstreetmap.org/copyright) © OpenStreetMap contributors under [ODbL](http://opendatacommons.org/licenses/odbl/) + * [Quattroshapes](https://github.com/foursquare/quattroshapes/blob/master/LICENSE.md) under [CC-BY-2.0](https://creativecommons.org/licenses/by/2.0/) + * [GeoNames](http://www.geonames.org/) under [CC-BY-3.0](https://creativecommons.org/licenses/by/2.0/) + * and other sources diff --git a/routes/v1.js b/routes/v1.js index cb3b53e2..37512008 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -1,3 +1,4 @@ +var express = require('express'); var Router = require('express').Router; var reverseQuery = require('../query/reverse'); @@ -11,7 +12,7 @@ var sanitisers = { /** ----------------------- controllers ----------------------- **/ var controllers = { - index: require('../controller/index'), + mdToHTML: require('../controller/markdownToHtml'), place: require('../controller/place'), search: require('../controller/search') }; @@ -36,7 +37,10 @@ function addRoutes(app, peliasConfig) { var routers = { index: createRouter([ - controllers.index() + controllers.mdToHTML(peliasConfig, './public/apiDoc.md') + ]), + attribution: createRouter([ + controllers.mdToHTML(peliasConfig, './public/attribution.md') ]), search: createRouter([ sanitisers.search.middleware, @@ -66,6 +70,7 @@ function addRoutes(app, peliasConfig) { // api root app.get ( base, routers.index ); + app.get ( base + 'attribution', routers.attribution ); app.get ( base + 'place', routers.place ); app.get ( base + 'autocomplete', routers.search ); app.get ( base + 'search', routers.search ); diff --git a/test/unit/controller/index.js b/test/unit/controller/index.js index f59dacd4..c2f10aba 100644 --- a/test/unit/controller/index.js +++ b/test/unit/controller/index.js @@ -1,19 +1,19 @@ -var setup = require('../../../controller/index'); +var setup = require('../../../controller/markdownToHtml'); module.exports.tests = {}; module.exports.tests.interface = function(test, common) { test('valid interface', function(t) { t.equal(typeof setup, 'function', 'setup is a function'); - t.equal(typeof setup(), 'function', 'setup returns a controller'); + t.equal(typeof setup({}, './public/apiDoc.md'), 'function', 'setup returns a controller'); t.end(); }); }; module.exports.tests.info_json = function(test, common) { test('returns server info in json', function(t) { - var controller = setup(); + var controller = setup({}, './public/attribution.md'); var req = { accepts: function (format) { t.equal(format, 'html', 'check for Accepts:html'); @@ -22,9 +22,8 @@ module.exports.tests.info_json = function(test, common) { }; var res = { json: function( json ){ t.equal(typeof json, 'object', 'returns json'); - t.equal(typeof json.name, 'string', 'name'); - t.equal(typeof json.version, 'object', 'version'); - t.equal(typeof json.version.number, 'string', 'version number'); + t.assert(json.hasOwnProperty('markdown'), 'return object contains markdown property'); + t.assert(json.hasOwnProperty('html'), 'return object contains html property'); t.end(); }}; controller( req, res ); @@ -33,21 +32,24 @@ module.exports.tests.info_json = function(test, common) { module.exports.tests.info_html = function(test, common) { test('returns server info in html', function(t) { + var filePath = './foo.md'; var style = ''; var mockText = 'this text should show up in the html content'; var fsMock = { readFileSync: function (path, format) { - t.equal(path, './DOCS.md', 'open DOCS.md file'); + t.equal(path, filePath, 'open specified file'); t.equal(format, 'utf8', 'file format'); return mockText; } }; var proxyquire = require('proxyquire'); - var setup = proxyquire('../../../controller/index', { 'fs': fsMock }); + var setup = proxyquire('../../../controller/markdownToHTML', { 'fs': fsMock }); - var controller = setup(); + var config = { version: '1.1.1' }; + + var controller = setup(config, filePath); var req = { accepts: function () { return true; From 2eccfc5c9a3a474b3df6bef3543194901f15e43b Mon Sep 17 00:00:00 2001 From: Diana Shkolnikov Date: Mon, 7 Sep 2015 22:53:16 -0400 Subject: [PATCH 2/2] Fix unit test --- test/unit/controller/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/controller/index.js b/test/unit/controller/index.js index c2f10aba..cffe1030 100644 --- a/test/unit/controller/index.js +++ b/test/unit/controller/index.js @@ -45,7 +45,7 @@ module.exports.tests.info_html = function(test, common) { }; var proxyquire = require('proxyquire'); - var setup = proxyquire('../../../controller/markdownToHTML', { 'fs': fsMock }); + var setup = proxyquire('../../../controller/markdownToHtml', { 'fs': fsMock }); var config = { version: '1.1.1' };