Skip to content

Commit

Permalink
optional keys for regex routes
Browse files Browse the repository at this point in the history
  • Loading branch information
lvivski committed Jan 10, 2014
1 parent 5de5c76 commit 2880189
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
14 changes: 8 additions & 6 deletions lib/src/route.dart
Expand Up @@ -7,7 +7,7 @@ class Route {
Stream stream;
String _dir;

Route(String method, path, { List<String> keys: null }) :
Route(String method, path, { List<String> keys }) :
_method = method.toUpperCase(),
_path = _normalize(path, keys: keys) {
stream = _controller.stream;
Expand All @@ -18,7 +18,7 @@ class Route {
stream = _controller.stream;
}

Route.ws(dynamic path, { List<String> keys: null }) :
Route.ws(dynamic path, { List<String> keys }) :
_method = 'WS',
_path = _normalize(path, keys: keys) {
stream = _controller.stream.transform(new WebSocketTransformer()).map((WebSocket ws) => new Socket(ws));
Expand Down Expand Up @@ -53,19 +53,21 @@ class Route {
}
}

static Map _normalize(dynamic path, { List<String> keys: null, bool strict: false }) {
static Map _normalize(dynamic path, { List<String> keys, bool strict: false }) {
if (keys == null) {
keys = [];
}

if (path is RegExp) {
return {
'regexp': path,
'keys': (keys == null) ? [] : keys
'keys': keys
};
}
if (path is List) {
path = '(${path.join('|')})';
}

keys = [];

if (!strict) {
path += '/?';
}
Expand Down
12 changes: 6 additions & 6 deletions lib/src/server.dart
Expand Up @@ -27,42 +27,42 @@ class Server {
});
}

Stream<Socket> ws(path, { List<String> keys: null} ) {
Stream<Socket> ws(path, { List<String> keys } ) {
var route = new Route.ws(path, keys: keys);
_routes.add(route);

return route.stream;
}

Stream<Request> get(path, { List<String> keys: null}) {
Stream<Request> get(path, { List<String> keys }) {
var route = new Route('get', path, keys: keys);
_routes.add(route);

return route.stream;
}

Stream<Request> options(path, { List<String> keys: null}) {
Stream<Request> options(path, { List<String> keys }) {
var route = new Route('options', path, keys: keys);
_routes.add(route);

return route.stream;
}

Stream<Request> post(path, { List<String> keys: null}) {
Stream<Request> post(path, { List<String> keys }) {
var route = new Route('post', path, keys: keys);
_routes.add(route);

return route.stream;
}

Stream<Request> put(path, { List<String> keys: null}) {
Stream<Request> put(path, { List<String> keys }) {
var route = new Route('put', path, keys: keys);
_routes.add(route);

return route.stream;
}

Stream delete(path, { List<String> keys: null}) {
Stream delete(path, { List<String> keys }) {
var route = new Route('delete', path, keys: keys);
_routes.add(route);

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,5 +1,5 @@
name: start
version: 0.1.2
version: 0.1.3
author: Yehor Lvivski <lvivski@gmail.com>
description: Sinatra inspired Web framework
homepage: http://github.com/lvivski/start
Expand Down

0 comments on commit 2880189

Please sign in to comment.