Browse Source

Merge pull request #223 from pelias/license

Add content for `attribution` static endpoint
pull/222/head
Diana Shkolnikov 9 years ago
parent
commit
f7f1ecc088
  1. 31
      controller/index.js
  2. 28
      controller/markdownToHtml.js
  3. 8
      middleware/geocodeJSON.js
  4. 0
      public/apiDoc.md
  5. 7
      public/attribution.md
  6. 9
      routes/v1.js
  7. 20
      test/unit/controller/index.js

31
controller/index.js

@ -1,31 +0,0 @@
var pkg = require('../package');
var markdown = require('markdown').markdown;
var fs = require('fs');
function setup(){
var styleString = '<style>html{font-family:monospace}</style>';
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;

28
controller/markdownToHtml.js

@ -0,0 +1,28 @@
var markdown = require('markdown').markdown;
var fs = require('fs');
function setup(peliasConfig, markdownFile){
var styleString = '<style>html{font-family:monospace}</style>';
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;

8
middleware/geocodeJSON.js

@ -25,16 +25,10 @@ function convertToGeocodeJSON(peliasConfig, req, next) {
// the GeocodeJSON spec that is implemented by this instance. // the GeocodeJSON spec that is implemented by this instance.
req.results.geojson.geocoding.version = '0.1'; 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, // 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. // and then multiple attributions, can be an object with one key by source.
// Can be a URI on the server, which outlines attribution details. // 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 // OPTIONAL. Default: null. The query that has been issued to trigger the
// search. // search.

0
DOCS.md → public/apiDoc.md

7
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

9
routes/v1.js

@ -1,3 +1,4 @@
var express = require('express');
var Router = require('express').Router; var Router = require('express').Router;
var reverseQuery = require('../query/reverse'); var reverseQuery = require('../query/reverse');
@ -11,7 +12,7 @@ var sanitisers = {
/** ----------------------- controllers ----------------------- **/ /** ----------------------- controllers ----------------------- **/
var controllers = { var controllers = {
index: require('../controller/index'), mdToHTML: require('../controller/markdownToHtml'),
place: require('../controller/place'), place: require('../controller/place'),
search: require('../controller/search') search: require('../controller/search')
}; };
@ -36,7 +37,10 @@ function addRoutes(app, peliasConfig) {
var routers = { var routers = {
index: createRouter([ index: createRouter([
controllers.index() controllers.mdToHTML(peliasConfig, './public/apiDoc.md')
]),
attribution: createRouter([
controllers.mdToHTML(peliasConfig, './public/attribution.md')
]), ]),
search: createRouter([ search: createRouter([
sanitisers.search.middleware, sanitisers.search.middleware,
@ -66,6 +70,7 @@ function addRoutes(app, peliasConfig) {
// api root // api root
app.get ( base, routers.index ); app.get ( base, routers.index );
app.get ( base + 'attribution', routers.attribution );
app.get ( base + 'place', routers.place ); app.get ( base + 'place', routers.place );
app.get ( base + 'autocomplete', routers.search ); app.get ( base + 'autocomplete', routers.search );
app.get ( base + 'search', routers.search ); app.get ( base + 'search', routers.search );

20
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 = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof setup, 'function', 'setup is a function'); 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(); t.end();
}); });
}; };
module.exports.tests.info_json = function(test, common) { module.exports.tests.info_json = function(test, common) {
test('returns server info in json', function(t) { test('returns server info in json', function(t) {
var controller = setup(); var controller = setup({}, './public/attribution.md');
var req = { var req = {
accepts: function (format) { accepts: function (format) {
t.equal(format, 'html', 'check for Accepts:html'); 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 ){ var res = { json: function( json ){
t.equal(typeof json, 'object', 'returns json'); t.equal(typeof json, 'object', 'returns json');
t.equal(typeof json.name, 'string', 'name'); t.assert(json.hasOwnProperty('markdown'), 'return object contains markdown property');
t.equal(typeof json.version, 'object', 'version'); t.assert(json.hasOwnProperty('html'), 'return object contains html property');
t.equal(typeof json.version.number, 'string', 'version number');
t.end(); t.end();
}}; }};
controller( req, res ); controller( req, res );
@ -33,21 +32,24 @@ module.exports.tests.info_json = function(test, common) {
module.exports.tests.info_html = function(test, common) { module.exports.tests.info_html = function(test, common) {
test('returns server info in html', function(t) { test('returns server info in html', function(t) {
var filePath = './foo.md';
var style = '<style>html{font-family:monospace}</style>'; var style = '<style>html{font-family:monospace}</style>';
var mockText = 'this text should show up in the html content'; var mockText = 'this text should show up in the html content';
var fsMock = { var fsMock = {
readFileSync: function (path, format) { 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'); t.equal(format, 'utf8', 'file format');
return mockText; return mockText;
} }
}; };
var proxyquire = require('proxyquire'); 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 = { var req = {
accepts: function () { accepts: function () {
return true; return true;

Loading…
Cancel
Save