From 88e4811a2167990983ba1247e55a41d1d001ed3d Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 30 Apr 2016 01:20:04 +0200 Subject: [PATCH] clean slate --- .editorconfig | 7 +- .gitignore | 4 - .npmignore | 12 --- README.md | 82 ++-------------- bin/vbb-rest | 53 ---------- bin/vbb-restd | 94 ------------------ docs/autocomplete.md | 48 --------- docs/departures.md | 86 ---------------- docs/index.md | 8 -- docs/locations.md | 73 -------------- docs/routes.md | 124 ------------------------ index.js | 5 + package.json | 71 +++----------- self-signed-certificate.zsh | 10 -- src/Server.coffee | 105 -------------------- src/services/hafas/index.coffee | 4 - src/services/hafas/locations.coffee | 28 ------ src/services/hafas/routes.coffee | 58 ----------- src/services/index.coffee | 4 - src/services/static/autocomplete.coffee | 24 ----- src/services/static/by-id.coffee | 19 ---- src/services/static/departures.coffee | 39 -------- src/services/static/filter.coffee | 21 ---- src/services/static/index.coffee | 6 -- urls.http | 14 --- vbb-rest.sublime-project | 37 ------- 26 files changed, 25 insertions(+), 1011 deletions(-) delete mode 100644 .npmignore delete mode 100755 bin/vbb-rest delete mode 100755 bin/vbb-restd delete mode 100644 docs/autocomplete.md delete mode 100644 docs/departures.md delete mode 100644 docs/index.md delete mode 100644 docs/locations.md delete mode 100644 docs/routes.md create mode 100644 index.js delete mode 100755 self-signed-certificate.zsh delete mode 100644 src/Server.coffee delete mode 100644 src/services/hafas/index.coffee delete mode 100644 src/services/hafas/locations.coffee delete mode 100644 src/services/hafas/routes.coffee delete mode 100644 src/services/index.coffee delete mode 100644 src/services/static/autocomplete.coffee delete mode 100644 src/services/static/by-id.coffee delete mode 100644 src/services/static/departures.coffee delete mode 100644 src/services/static/filter.coffee delete mode 100644 src/services/static/index.coffee delete mode 100644 urls.http delete mode 100644 vbb-rest.sublime-project diff --git a/.editorconfig b/.editorconfig index 72dd046..965bc87 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,16 +1,13 @@ -# This file is for unifying the coding style for different editors and IDEs # editorconfig.org - root = true - [*] end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -# Use tabs in JavaScript, CoffeeScript and JSON. -[**.{coffee,js,json}] +# Use tabs in JavaScript. +[**.{js}] indent_style = tab indent_size = 4 diff --git a/.gitignore b/.gitignore index 1af4c77..d6fb75e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,3 @@ Thumbs.db .nvm-version node_modules npm-debug.log - -vbb-rest.sublime-workspace -server.crt -server.key diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 0efcc40..0000000 --- a/.npmignore +++ /dev/null @@ -1,12 +0,0 @@ -.DS_Store -Thumbs.db - -.nvm-version -node_modules -npm-debug.log - -docs/ -.editorconfig -.gitignore -vbb-rest.sublime-project -vbb-rest.sublime-workspace diff --git a/README.md b/README.md index 7e6fbe5..a26177e 100644 --- a/README.md +++ b/README.md @@ -1,90 +1,20 @@ -# vbb-rest +# *vbb-rest* -*vbb-rest* is an HTTP server that proxies all requests to the **Berlin & Brandenburg public transport (VBB) API**. Using the [`vbb` API client library](https://github.com/derhuerst/vbb), it wraps the verbose HAFAS interface in a straightforward REST API. - -*vbb-rest* is written in CoffeeScript and embraces [prototypal programming](http://davidwalsh.name/javascript-objects-deconstruction#simpler-object-object), making it easily extendable. It is [MIT-licensed](LICENSE). +***vbb-rest* is a public transport REST API**, a clean alternative to the [VBB HAFAS API](https://github.com/derhuerst/vbb-hafas). [![npm version](https://img.shields.io/npm/v/vbb-rest.svg)](https://www.npmjs.com/package/vbb-rest) [![dependency status](https://img.shields.io/david/derhuerst/vbb-rest.svg)](https://david-dm.org/derhuerst/vbb-rest) +![ISC-licensed](https://img.shields.io/github/license/derhuerst/vbb-rest.svg) +## Installing -## Installing (globally) - -```shell -npm install -g vbb-rest -``` - -You can now start the *vbb-rest* server wherever you want. - +todo ## Usage -The server will forward the [VBB API token](http://www.vbb.de/de/article/webservices/schnittstellen-fuer-webentwickler/5070.html#testserver) from the HTTP `X-API-Key` field, so get one for testing. - -```shell -vbb-rest -c -k # run the server in "live mode" -``` - -or - -```shell -vbb-restd start -c -k # start/stop the server in the background -``` - - -### `vbb-rest` - -``` -Usage: -vbb-rest -c -k [-p ] - -Arguments: - -c, --cert The SSL certificate. - -k, --key The SSL key. - -Options: - -p, --port Where the server will listen. Default: 8000 -``` - - -### `vbb-restd` - -``` -Usage: -vbb-restd start -c -k [-p ] -vbb-restd stop - -Arguments: - -c, --cert The SSL certificate. - -k, --key The SSL key. - id The server process id. - -Options: - -p, --port Where the server will listen. Default: 8000 -``` - -When you `start` a server, it will print its process id. - -```shell -vbb-restd start -c -k -info: The server has been started. -``` - -You can use the `id` later to `stop` the server. - -```shell -$ vbb-restd stop . -info: The server has been stopped. -``` - - - -## Documentation - -- [*vbb-rest* HTTP documentation](docs/index.md) - +todo ## Contributing diff --git a/bin/vbb-rest b/bin/vbb-rest deleted file mode 100755 index 7ef981f..0000000 --- a/bin/vbb-rest +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env coffee - - - -minimist = require 'minimist' -fs = require 'fs' -argv = minimist process.argv.slice 2 - - - -help = [ - 'Usage:' - 'vbb-rest -c -k [-p ]' - '' - 'Arguments:' - ' -c, --cert The SSL certificate.' - ' -k, --key The SSL key.' - '' - 'Options:' - ' -p, --port Where the server will listen. Default: 8000' -].join '\n' - -if argv.h is true or argv.help is true - process.stdout.write help - process.exit 0 - - - -cert = null -if argv.c? then cert = argv.c -else if argv.cert? then cert = argv.cert -else throw new Error 'Missing `--cert` argument.' -cert = fs.readFileSync cert - -key = null -if argv.k? then key = argv.k -else if argv.key? then key = argv.key -else throw new Error 'Missing `--key` argument.' -key = fs.readFileSync key - -port = 8000 -if argv.p? then port = argv.p -if argv.port? then port = argv.port - - - -Server = require '../src/Server' -winston = require 'winston' # todo: consider using `bunyan` - -server = Object.create Server -server.init cert, key, port, winston -server.listen () -> - winston.info "Server listening on #{port}." diff --git a/bin/vbb-restd b/bin/vbb-restd deleted file mode 100755 index 50bf16b..0000000 --- a/bin/vbb-restd +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env coffee - - - -minimist = require 'minimist' -argv = minimist process.argv.slice 2 - - - -help = [ - 'Usage:' - 'vbb-restd start -c -k [-p ]' - 'vbb-restd stop ' - '' - 'Arguments:' - ' -c, --cert The SSL certificate.' - ' -k, --key The SSL key.' - ' id The server process id.' - '' - 'Options:' - ' -p, --port Where the server will listen. Default: 8000' -].join '\n' - -if argv.h is true or argv.help is true or argv._.length is 0 - process.stdout.write help - process.exit 0 -else if argv._.length is 1 - process.stdout.write help - process.exit 1 - - - -if argv._[0] is 'start' - fs = require 'fs' - pm2 = require 'pm2' - path = require 'path' - shortid = require 'shortid' - winston = require 'winston' # todo: consider using `bunyan` - - cert = null - if argv.c? then cert = argv.c - else if argv.cert? then cert = argv.cert - else throw new Error 'Missing `--cert` argument.' - cert = fs.readFileSync cert - - key = null - if argv.k? then key = argv.k - else if argv.key? then key = argv.key - else throw new Error 'Missing `--key` argument.' - key = fs.readFileSync key - - port = 8000 - if argv.p? then port = argv.p - if argv.port? then port = argv.port - - id = shortid.generate() - - options = - exec_mode: 'fork_mode' - exec_interpreter: 'coffee' - script: path.join __dirname, 'vbb-rest' - name: "vbb-rest-#{id}" - args: [ - '-c', cert - '-k', ley - '-p', port - ] - watch: false - - pm2.connect ()-> - pm2.start options, (err, process) -> - if err then throw err - winston.info "The server #{id} has been started." - pm2.disconnect() - - - -else if argv._[0] is 'stop' - pm2 = require 'pm2' - winston = require 'winston' # todo: consider using `bunyan` - - pm2.connect ()-> - pm2.stop "vbb-rest-#{argv._[1]}", (err) -> - if err then throw err - pm2.delete "vbb-rest-#{argv._[1]}", (err) -> - if err then throw err - winston.info "The server #{id} has been stopped." - pm2.disconnect() - - - -else - process.stdout.write help - process.exit 1 diff --git a/docs/autocomplete.md b/docs/autocomplete.md deleted file mode 100644 index 9f89d73..0000000 --- a/docs/autocomplete.md +++ /dev/null @@ -1,48 +0,0 @@ -# `GET /autocomplete?query=…` - -**Fuzzy-finds stations** matching `query`. - - - -## Example - -Request: - -```http -GET :/autocomplete?query=alexanderplatz&results=2 -``` - -Response: - -```http -HTTP/1.1 200 OK -Content-Type: application/json; charset=utf-8 -Cache-Control: no-cache -Content-Length: 470 -Accept-Ranges: bytes -Date: Tue, 04 Aug 2015 01:59:03 GMT -Connection: close -``` - -```json -[ - { - "id": 9100005, - "name": "U Alexanderplatz (Berlin) [Tram]" - }, - { - "id": 9100026, - "name": "S+U Alexanderplatz Bhf/Gontardstr. (Berlin)" - } -] - -``` - - - -## Parameters - -| parameter | default | type | description | -|:----------|:--------|:-----|:------------| -| `query` | – | `String` | Can be any query. *Required* | -| `results` | `6` | `Integer` | The number of results. | diff --git a/docs/departures.md b/docs/departures.md deleted file mode 100644 index d97d878..0000000 --- a/docs/departures.md +++ /dev/null @@ -1,86 +0,0 @@ -# `GET /locations/{station}` - -**Queries the next departures at a station.** - - - -## Example - -Request: - -```http -GET @:/departures/9007102?bus=false&results=4 -``` - -`9100003` is the `id` for the [station *Gesundbrunnen*](https://www.google.de/maps/place/Bahnhof+Berlin+Gesundbrunnen/@52.5487914,13.3893007,17z). This value can be found using the [`/locations` endpoint](locations.md). - -Response: - -```http -HTTP/1.1 200 OK -Content-Type: application/json; charset=utf-8 -Cache-Control: no-cache -Content-Length: 620 -Accept-Ranges: bytes -Date: Tue, 04 Aug 2015 02:24:50 GMT -Connection: close -``` - -```json -[ - { - "stop": 9007102, - "line": "S25", - "type": "suburban", - "direction": "S+U Potsdamer Platz Bhf (Berlin)", - "when": 1438655040000, - "realtime": 1438655040000 - }, { - "stop": 9007102, - "line": "S41", - "type": "suburban", - "direction": "Ringbahn S 41", - "when": 1438655100000, - "realtime": 1438655100000 - }, { - "stop": 9007102, - "line": "U8", - "type": "subway", - "direction": "S+U Wittenau (Berlin) [U8]", - "when": 1438655160000, - "realtime": 1438655280000 - }, { - "stop": 9007102, - "line": "S2", - "type": "suburban", - "direction": "S Bernau Bhf", - "when": 1438655160000, - "realtime": 1438655160000 - } -] -``` - -`when` and `realtime` are [UNIX timestamps](https://en.wikipedia.org/wiki/Unix_time#Encoding_time_as_a_number). - - - -## `station` - -The `id` of the station. It can be found using the [`/locations` endpoint](locations.md). - - - -## Parameters - -| parameter | default | type | description | -|:----------|:--------|:-----|:------------| -| `results` | `10` | `Integer` | The number of results. | -| `when` | `new Date()` | `Date` | Self-explanatory. | -| `direction` | – | `Integer` | The `id` of the last station of the line. | -| `suburban` | `true` | `Integer` | If [S-Bahn trains](https://en.wikipedia.org/wiki/Berlin_S-Bahn) should be included in the search results. | -| `subway` | `true` | `Integer` | If [U-Bahn trains](https://en.wikipedia.org/wiki/Berlin_U-Bahn) should be included in the search results. | -| `tram` | `true` | `Integer` | If [tramway vehicles](https://en.wikipedia.org/wiki/Trams_in_Berlin) should be included in the search results. | -| `bus` | `true` | `Integer` | If [buses](https://en.wikipedia.org/wiki/Bus_transport_in_Berlin) should be included in the search results. | -| `ferry` | `true` | `Integer` | If [ferries](https://en.wikipedia.org/wiki/Ferry_transport_in_Berlin) should be included in the search results. | -| `express` | `false` | `Integer` | If [IC](https://en.wikipedia.org/wiki/Intercity_%28Deutsche_Bahn%29)/[EC](https://en.wikipedia.org/wiki/EuroCity)/[ICE](https://en.wikipedia.org/wiki/Intercity-Express) should be included in the search results. | -| `regional` | `true` | `Integer` | If [RE](https://en.wikipedia.org/wiki/Regional-Express)/[RB](https://en.wikipedia.org/wiki/Regionalbahn)/[IRE](https://en.wikipedia.org/wiki/Interregio-Express) trains should be included in the search results. | diff --git a/docs/index.md b/docs/index.md deleted file mode 100644 index 6106593..0000000 --- a/docs/index.md +++ /dev/null @@ -1,8 +0,0 @@ -# *vbb-rest* HTTP documentation - -A *vbb-rest* server exposes the following endpoints. - -- [`GET /autocomplete?query=…`](autocomplete.md) -- [`GET /locations?query=…`](locations.md) -- [`GET /routes/{from}/{to}`](routes.md) -- [`GET /departures/{station}`](departures.md) diff --git a/docs/locations.md b/docs/locations.md deleted file mode 100644 index eb43277..0000000 --- a/docs/locations.md +++ /dev/null @@ -1,73 +0,0 @@ -# `GET /locations?query=…` - -**Finds stations, addresses and [POI](https://en.wikipedia.org/wiki/Point_of_interest)s** matching `query`. - - - -## Example - -Request: - -```http -GET @:/locations?query=alexanderplatz&addresses=false&results=2 -``` - -Response: - -```http -HTTP/1.1 200 OK -Content-Type: application/json; charset=utf-8 -Cache-Control: no-cache -Content-Length: 470 -Accept-Ranges: bytes -Date: Tue, 04 Aug 2015 01:59:03 GMT -Connection: close -``` - -```json -[ - { - "title": "Alexanderplatz Bhf (Berlin) (S+U)", - "latitude": 52.521507999999997, - "longitude": 13.411267, - "id": 9100003, - "type": "station", - "products": { - "bus": true, - "express": false, - "ferry": false, - "regional": true, - "suburban": true, - "subway": false, - "tram": false - } - }, { - "title": "Alexanderplatz (Berlin) [Tram] (U)", - "latitude": 52.522388999999997, - "longitude": 13.414493999999999, - "id": 9100005, - "type": "station", - "products": { - "bus": false, - "express": false, - "ferry": false, - "regional": false, - "suburban": false, - "subway": false, - "tram": true - } - } -] -``` - - - -## Parameters - -| parameter | default | type | description | -|:----------|:--------|:-----|:------------| -| `query` | – | `String` | Can be any query. *Required* | -| `results` | `5` | `Integer` | The number of results, limited to `1000` by HAFAS. | -| `stations` | `true` | `Boolean` | If stations should be included in the search results. | -| `addresses` | `true` | `Boolean` | If addresses should be included in the search results. | -| `pois` | `true` | `Boolean` | If [POI](https://en.wikipedia.org/wiki/Point_of_interest)s should be included in the search results. | diff --git a/docs/routes.md b/docs/routes.md deleted file mode 100644 index dca9fe7..0000000 --- a/docs/routes.md +++ /dev/null @@ -1,124 +0,0 @@ -# `GET /routes/{from}/{to}` - -**Finds routes between `from` and `to`.** - - - -## Example - -Request: - -```http -GET @:/routes/9100003/@52.5112362|13.3358289?results=1&suburban=false -``` - -`9100003` is the `id` for the [station *Alexanderplatz*](https://www.google.de/maps/place/Alexanderplatz/@52.5219184,13.4132147,17z). `@52.5112362|13.3358289` are GPS coordinates for the [restaurant *Schleusenkrug*](https://www.google.de/maps/place/Schleusenkrug/@52.5112362,13.3358289,17z). Both values can be found using the [`/locations` endpoint](locations.md). - -Response: - -```http -HTTP/1.1 200 OK -Content-Type: application/json; charset=utf-8 -Cache-Control: no-cache -Content-Length: 904 -Accept-Ranges: bytes -Date: Tue, 04 Aug 2015 02:12:39 GMT -Connection: close -``` - -```json -[ - { - "duration": 1140, - "parts": [ - { - "from": { - "id": 9100003, - "latitude": 52.521507999999997, - "longitude": 13.411267, - "notes": { - "escalator": true, - "lift": true - }, - "title": "S+U Alexanderplatz Bhf (Berlin)", - "type": "station", - "when": 1438654740000 - }, - "to": { - "id": 9023201, - "latitude": 52.506881999999997, - "longitude": 13.332926, - "notes": { - "escalator": true, - "lift": true - }, - "title": "S+U Zoologischer Garten Bhf (Berlin)", - "type": "station", - "when": 1438655400000 - }, - "transport": "public", - "type": "regional", - "line": "RE2", - "direction": "Wismar, Bahnhof", - "notes": {} - }, { - "from": { - "id": 9023201, - "latitude": 52.506881999999997, - "longitude": 13.332926, - "notes": { - "escalator": true, - "lift": true - }, - "title": "S+U Zoologischer Garten Bhf (Berlin)", - "type": "station", - "when": 1438655400000 - }, - "to": { - "latitude": 52.511142999999997, - "longitude": 13.336107999999999, - "title": "10623 Berlin-Tiergarten, Müller-Breslau-Str.", - "type": "address", - "when": 1438655880000 - }, - "transport": "walk" - } - ] - } -] -``` - -`duration` is the trips overall duration in seconds. - -`when` is an [UNIX timestamp](https://en.wikipedia.org/wiki/Unix_time#Encoding_time_as_a_number). - - - -## `from` - -**Must either be the `id` or GPS coordinates of the start location.** The GPS coordinates must begin with `@` and must be separated by `|`. - - - -## `to` - -**Must either be the `id` or GPS coordinates of the destination.** The GPS coordinates must begin with `@` and must be separated by `|`. - - - -## Parameters - -| parameter | default | type | description | -|:----------|:--------|:-----|:------------| -| `via` | – | `Integer` | The `id` of an optional waypoint location. | -| `results` | `4` | `Integer` | The number of results, limited to `6` by HAFAS. | -| `when` | `new Date()` | `Date` | Self-explanatory. | -| `changes` | – | `Integer` | The maximum number of changes, limited to `3` by HAFAS. | -| `changeTimeFactor` | `1` | `Float` | The walking speed, between `1` and `5`. `5` represents 5 times more time to change. | -| `suburban` | `true` | `Integer` | If [S-Bahn trains](https://en.wikipedia.org/wiki/Berlin_S-Bahn) should be included in the search results. | -| `subway` | `true` | `Integer` | If [U-Bahn trains](https://en.wikipedia.org/wiki/Berlin_U-Bahn) should be included in the search results. | -| `tram` | `true` | `Integer` | If [tramway vehicles](https://en.wikipedia.org/wiki/Trams_in_Berlin) should be included in the search results. | -| `bus` | `true` | `Integer` | If [buses](https://en.wikipedia.org/wiki/Bus_transport_in_Berlin) should be included in the search results. | -| `ferry` | `true` | `Integer` | If [ferries](https://en.wikipedia.org/wiki/Ferry_transport_in_Berlin) should be included in the search results. | -| `express` | `false` | `Integer` | If [IC](https://en.wikipedia.org/wiki/Intercity_%28Deutsche_Bahn%29)/[EC](https://en.wikipedia.org/wiki/EuroCity)/[ICE](https://en.wikipedia.org/wiki/Intercity-Express) should be included in the search results. | -| `regional` | `true` | `Integer` | If [RE](https://en.wikipedia.org/wiki/Regional-Express)/[RB](https://en.wikipedia.org/wiki/Regionalbahn)/[IRE](https://en.wikipedia.org/wiki/Interregio-Express) trains should be included in the search results. | diff --git a/index.js b/index.js new file mode 100644 index 0000000..4992690 --- /dev/null +++ b/index.js @@ -0,0 +1,5 @@ +'use strict' + +const path = require('path') + +// todo diff --git a/package.json b/package.json index 63ee661..471c614 100644 --- a/package.json +++ b/package.json @@ -1,61 +1,14 @@ { - "name": "vbb-rest", - "description": "An HTTP REST server for Berlin & Brandenburg public transport.", - "version": "0.3.0", - "main": "src/Server.coffee", - "bin": { - "vbb-rest": "./bin/vbb-rest", - "vbb-restd": "./bin/vbb-restd" - }, - "preferGlobal": true, - "keywords": [ - "vbb", - "rest", - "http", - "server", - "berlin", - "brandenburg", - "public transport", - "api", - "open data", - "hafas", - "json", - "rest", - "fahrinfo", - "coffeescript", - "javascript" - ], - "author": { - "name": "Jannis R", - "email": "mail@jannisr.de", - "url": "http://jannisr.de" - }, - "homepage": "https://github.com/derhuerst/vbb-rest", - "repository": { - "type": "git", - "url": "git://github.com/derhuerst/vbb-rest.git" - }, - "bugs": { - "url": "https://github.com/derhuerst/vbb-rest/issues" - }, - "license": "ISC", - "dependencies": { - "coffee-script": "^1.10.0", - "minimist": "^1.2.0", - "winston": "^1.1.2", - "hapi": "^8.8.1", - "hapi-auth-basic": "^3.0.0", - "vbb-hafas": "^0.6.0", - "vbb-static": "^0.7.0", - "vbb-stations-autocomplete": "^0.3.0", - "boom": "^2.10.1", - "underscore.parse": "^0.1.1", - "pm2": "^0.14.7", - "shortid": "^2.2.4" - }, - "directories": { - "docs": "./docs", - "bin": "./bin", - "lib": "./src" - } + "name": "vbb-rest", + "description": "A sane public transport API for Berlin.", + "version": "0.3.0", + "main": "todo", + "files": ["todo"], + "author": "Jannis R ", + "homepage": "https://github.com/derhuerst/vbb-rest", + "repository": "derhuerst/vbb-rest", + "bugs": "https://github.com/derhuerst/vbb-rest/issues", + "license": "ISC", + "keywords": ["public", "transport", "api", "http", "rest"], + "engines" : {"node": ">=4"} } diff --git a/self-signed-certificate.zsh b/self-signed-certificate.zsh deleted file mode 100755 index 3293f58..0000000 --- a/self-signed-certificate.zsh +++ /dev/null @@ -1,10 +0,0 @@ -openssl genrsa -des3 -out server.pass.key 2048 -openssl rsa -in server.pass.key -out server.key -openssl req -new -key server.key -out server.csr -openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt - -echo "SHA1 fingerprint:\n" -openssl x509 -in server.crt -sha1 -noout -fingerprint - -rm server.pass.key -rm server.csr diff --git a/src/Server.coffee b/src/Server.coffee deleted file mode 100644 index 8f0051b..0000000 --- a/src/Server.coffee +++ /dev/null @@ -1,105 +0,0 @@ -hapi = require 'hapi' -httpBasicAuth = require 'hapi-auth-basic' -hafas = require 'vbb-hafas' - -services = require './services' - - - - - -auth = (req, user, pw, cb) -> - cb null, true, - apiKey: user - - -module.exports = - - - - server: null - hafas: null # VBB api client - - logger: null - - onOptions: (req, reply) -> - response = reply 'GET' - response.header 'Accept', 'GET' - - - - init: (cert, key, port, logger) -> - @hafas = hafas() - - if not cert? then throw new Error 'Missing `cert` parameter' - if not key? then throw new Error 'Missing `key` parameter' - if not port? then throw new Error 'Missing `port` parameter' - @server = new hapi.Server() - @server.connection - tls: - cert: cert - key: key - port: port - @server.bind this - @server.register httpBasicAuth, (err) => - @server.auth.strategy 'api-token', 'basic', true, - validateFunc: auth - - if not logger? then throw new Error 'Missing `logger` parameter' - @logger = logger - - # todo: `bind` option - # todo: `auth` option - # todo: `cors` option - # todo: `validate` option? - - # todo: API client timeouts - # todo: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#rate-limiting - # todo: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#authentication - - @server.route - method: 'GET' - path: '/locations' - handler: services.hafas.locations - - @server.route - method: 'GET' - path: '/routes/{from}/{to}' - handler: services.hafas.routes - - @server.route - method: 'GET' - path: '/{method}/{id}' - handler: services.static.byId - - @server.route - method: 'GET' - path: '/stations/{id}/departures' - handler: services.static.departures - - @server.route - method: 'GET' - path: '/stations/autocomplete' - handler: services.static.autocomplete - - @server.route - method: 'GET' - path: '/{method}' - handler: services.static.filter - - @server.route - method: 'OPTIONS' - path: '/' - handler: @onOptions - - return this - - - - listen: (cb) -> - @server.start cb - return this - - stop: (cb) -> - @server.stop cb - return this diff --git a/src/services/hafas/index.coffee b/src/services/hafas/index.coffee deleted file mode 100644 index 29fc0e6..0000000 --- a/src/services/hafas/index.coffee +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = - - locations: require './locations' - routes: require './routes' diff --git a/src/services/hafas/locations.coffee b/src/services/hafas/locations.coffee deleted file mode 100644 index 7bf4d4b..0000000 --- a/src/services/hafas/locations.coffee +++ /dev/null @@ -1,28 +0,0 @@ -boom = require 'boom' -parse = require('underscore.parse').parse - - - - - -module.exports = (req, reply) -> - - if not req.query.query? - reply boom.badRequest 'Missing `query` parameter' - - options = {} - if req.query.results? - options.results = parseInt req.query.results - if req.query.stations? - options.stations = parse req.query.stations - if req.query.addresses? - options.addresses = parse req.query.addresses - if req.query.pois? - options.pois = parse req.query.pois - - if req.auth.credentials and req.auth.credentials.apiKey - options.apiKey = req.auth.credentials.apiKey - @hafas.locations req.query.query, options - .then (results) -> - response = reply results - response.type 'application/json' diff --git a/src/services/hafas/routes.coffee b/src/services/hafas/routes.coffee deleted file mode 100644 index 61a04bc..0000000 --- a/src/services/hafas/routes.coffee +++ /dev/null @@ -1,58 +0,0 @@ -parse = require('underscore.parse').parse - - - - - -module.exports = (req, reply) -> - - options = {} - - from = req.params.from - if from.charAt(0) is '@' # GPS notation - from = from.substr(1).split '|' - options.fromLatitude = parseFloat from[0] - options.fromLongitude = parseFloat from[1] - else options.from = parseInt from - - to = req.params.to - if to.charAt(0) is '@' # GPS notation - to = to.substr(1).split '|' - options.toLatitude = parseFloat to[0] - options.toLongitude = parseFloat to[1] - else options.to = parseInt to - - if req.query.via? - options.via = parseInt req.query.via - - if req.query.results? - options.results = parseInt req.query.results - if req.query.when? - options.when = new Date req.query.when - if req.query.changeTimeFactor? - options.changeTimeFactor = parseFloat req.query.changeTimeFactor - options.products = {} - for product in [ - 'suburban', - 'subway', - 'tram', - 'bus', - 'ferry', - 'express', - 'regional' - ] - if req.query[product]? - options.products[product] = parse req.query[product] - - if req.auth.credentials and req.auth.credentials.apiKey - options.apiKey = req.auth.credentials.apiKey - @hafas.routes options - .then (results) -> - for result in results - result.duration /= 1000 - for part in result.parts - part.from.when = part.from.when.getTime() # unix timestamp - part.to.when = part.to.when.getTime() # unix timestamp - - response = reply results - response.type 'application/json' diff --git a/src/services/index.coffee b/src/services/index.coffee deleted file mode 100644 index 690155f..0000000 --- a/src/services/index.coffee +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = - - static: require './static/index' - hafas: require './hafas/index' diff --git a/src/services/static/autocomplete.coffee b/src/services/static/autocomplete.coffee deleted file mode 100644 index d39c38f..0000000 --- a/src/services/static/autocomplete.coffee +++ /dev/null @@ -1,24 +0,0 @@ -boom = require 'boom' -Autocomplete = require 'vbb-stations-autocomplete' -vbbStatic = require 'vbb-static' -Q = require 'q' - - - - - -autocomplete = Autocomplete() - -module.exports = (req, reply) -> - - if not req.query.input - reply boom.badRequest 'Missing `input` parameter.' - - autocomplete.suggest req.query.input - .then (results) -> - return Q.all results.map (result) -> - return vbbStatic.stations true, result.id - .then (results) -> results[0] - .then (results) -> - response = reply results - response.type 'application/json' diff --git a/src/services/static/by-id.coffee b/src/services/static/by-id.coffee deleted file mode 100644 index d578c75..0000000 --- a/src/services/static/by-id.coffee +++ /dev/null @@ -1,19 +0,0 @@ -boom = require 'boom' -vbbStatic = require 'vbb-static' -parse = require('underscore.parse').parse - - - - - -module.exports = (req, reply) -> - - if not vbbStatic[req.params.method] - reply boom.badRequest 'Invalid `method`.' - if not req.params.id - reply boom.badRequest 'Invalid `id`.' - - vbbStatic[req.params.method] true, parse req.params.id - .then (results) -> - response = reply results - response.type 'application/json' diff --git a/src/services/static/departures.coffee b/src/services/static/departures.coffee deleted file mode 100644 index eeb36f2..0000000 --- a/src/services/static/departures.coffee +++ /dev/null @@ -1,39 +0,0 @@ -parse = require('underscore.parse').parse - - - - - -module.exports = (req, reply) -> - - options = {} - if req.query.results? - options.results = parseInt req.query.results - if req.query.when? - options.when = new Date req.query.when - if req.query.direction? - options.direction = parseInt req.query.direction - options.products = {} - for product in [ - 'suburban', - 'subway', - 'tram', - 'bus', - 'ferry', - 'express', - 'regional' - ] - if req.query[product]? - options.products[product] = parse req.query[product] - - if req.auth.credentials and req.auth.credentials.apiKey - options.apiKey = req.auth.credentials.apiKey - @hafas.departures parseInt(req.params.id), options - .then (results) -> - for result in results - result.when = result.when.getTime() # unix timestamp - if result.realtime - result.realtime = result.realtime.getTime() # unix timestamp - - response = reply results - response.type 'application/json' diff --git a/src/services/static/filter.coffee b/src/services/static/filter.coffee deleted file mode 100644 index a057b05..0000000 --- a/src/services/static/filter.coffee +++ /dev/null @@ -1,21 +0,0 @@ -boom = require 'boom' -vbbStatic = require 'vbb-static' - - - - - -module.exports = (req, reply) -> - - if not vbbStatic[req.params.method] - reply boom.badRequest 'Invalid `method`.' - - filter = {} - for key, value in req.query - if req.query.hasOwnProperty key - filter[key] = value - - vbbStatic[req.params.method] true, filter - .then (results) -> - response = reply results - response.type 'application/json' diff --git a/src/services/static/index.coffee b/src/services/static/index.coffee deleted file mode 100644 index d2900e5..0000000 --- a/src/services/static/index.coffee +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = - - byId: require './by-id' - filter: require './filter' - departures: require './departures' - autocomplete: require './autocomplete' diff --git a/urls.http b/urls.http deleted file mode 100644 index 1ad7e09..0000000 --- a/urls.http +++ /dev/null @@ -1,14 +0,0 @@ -/agencies?… -/agencies/VBB -/stations?… -/stations/9009101 -/stations/9009101/departures -/stations/autocomplete?query=… -/lines?… -/lines/1173 -/trips?… -/trips/1 -/schedules?… -/schedules/1 -/locations?query=… -/routes/9009101/9009102 diff --git a/vbb-rest.sublime-project b/vbb-rest.sublime-project deleted file mode 100644 index 68b4741..0000000 --- a/vbb-rest.sublime-project +++ /dev/null @@ -1,37 +0,0 @@ -{ - "folders": - [ - { - "file_exclude_patterns": - [ - "*.sublime-workspace" - ], - "folder_exclude_patterns": - [ - "bin", - "docs", - "src", - "node_modules" - ], - "name": "vbb-rest", - "path": "." - }, - { - "name": "bin", - "path": "bin" - }, - { - "name": "src", - "path": "src" - }, - { - "name": "docs", - "path": "docs" - } - ], - "settings": - { - "tab_size": 4, - "translate_tabs_to_spaces": false - } -}