Skip to content

Commit

Permalink
begin converting to use second parameter for $params
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Kilkelly committed Nov 1, 2015
1 parent ae0da48 commit 3bcb6dc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
2 changes: 1 addition & 1 deletion META.info
@@ -1,6 +1,6 @@
{
"name" : "HTTP::Router::Blind",
"version" : "1.0.0",
"version" : "2.0.0",
"description" : "A framework-agnostic HTTP Router",
"provides" : {
"HTTP::Router::Blind" : "lib/HTTP/Router/Blind.pm6"
Expand Down
20 changes: 13 additions & 7 deletions README.md
Expand Up @@ -24,17 +24,19 @@ $router.get("/about", sub (%env) {
[200, ['Content-Type' => 'text/plain'], ["About this site"]]
});

# string match with keyword params
$router.get("/user/:id", sub (%env) {
my $user-id = %env<params><id>;
# string match with keyword params,
# the second parameter to the handler function is a hash of params
# extracted from the URL
$router.get("/user/:id", sub (%env, %params) {
my $user-id = %params<id>;
[200, ['Content-Type' => 'text/plain'], ["It's user $user-id"]]
});

# regex match, with named capture-group,
# will match a request like '/items/42253',
# the regex match results are available as %env<params>;
$router.get(/\/items\/$<id>=(.*)/, sub (%env) {
my $id = %env<params><id>;
# the second parameter to the handler is the Regex match object;
$router.get(/\/items\/$<id>=(.*)/, sub (%env, $params) {
my $id = $params<id>;
[200, ['Content-Type' => 'text/plain'], ["got request for item $id"]]
});

Expand All @@ -47,9 +49,13 @@ $router.get('/secret', &do-something-special, sub (%env) {

# in our app function, we just call $router.dispatch
my $app = sub (%env) {
$router.dispatch(%env<REQUEST\_METHOD>, %env<REQUEST\_URI>, %env);
$router.dispatch(%env<REQUEST_METHOD>, %env<REQUEST_URI>, %env);
};

$http.handle($app);
```

# CHANGELOG

- 2.0.0 : for keyword and regex matches, pass the matched `params` as a second
parameter to the handler function, instead of setting `%env<params>`.
18 changes: 12 additions & 6 deletions lib/HTTP/Router/Blind.pm6
Expand Up @@ -29,10 +29,11 @@ method anymethod ($path, **@handlers) {
%!routes<ANY>.push(@($path, @handlers));
}


method !keyword-match ($path, $uri) {
my @p = $path.split('/');
my @u = $uri.split('/');
say @p.perl;
say @u.perl;
if @p.elems != @u.elems {
return;
}
Expand All @@ -54,7 +55,14 @@ method !apply-handlers (%env, @handlers) {
for @handlers -> &handler {
$result = &handler($result);
}
return $result;
$result;
}
method !apply-handlers-with-params (%env, @handlers, $params) {
my $result = %env;
for @handlers -> &handler {
$result = &handler($result, $params);
}
$result;
}

method dispatch ($method, $uri, %env) {
Expand All @@ -69,16 +77,14 @@ method dispatch ($method, $uri, %env) {
if $path.contains(':') {
my $params = self!keyword-match($path, $uri);
if $params {
%env<params> = %($params);
return self!apply-handlers(%env, @funcs);
return self!apply-handlers-with-params(%env, @funcs, $params);
}
}
}
if $path ~~ Regex {
my $match = $uri ~~ $path;
if $match {
%env<params> = $match;
return self!apply-handlers(%env, @funcs);
return self!apply-handlers-with-params(%env, @funcs, $match);
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions t/01-keyword-match.t
@@ -0,0 +1,34 @@
use v6;

BEGIN { @*INC.unshift('lib') };

use Test;
plan 2;

use HTTP::Router::Blind;

my %env;
my $result;
my $router = HTTP::Router::Blind.new();

# simple, one keyword
$router.get: '/one/:name', -> %env, $params {
$params<name>;
};

$result = $router.dispatch: 'GET', '/one/jim', %env;
ok $result eq 'jim', 'basic keyword match works';

# multi-handlers with keyword params
sub checker (%env, $params) {
if $params<thing> eq "yes" {
%env<checked> = True;
}
%env;
}
$router.get: '/othercheck/:thing', &checker, -> %env, $params {
%env;
};

$result = $router.dispatch('GET', '/othercheck/yes', %env);
ok $result<checked> == True, 'multi-handler with keyword params works';
12 changes: 6 additions & 6 deletions t/basic.t
Expand Up @@ -27,8 +27,8 @@ if $result ~~ 'this-is-get' {
};

# :keyword route
$router.get("/stuff/:id/thing/:foo", sub (%env) {
%env<params>;
$router.get("/stuff/:id/thing/:foo", sub (%env, $params) {
$params;
});

$result = $router.dispatch('GET', "/stuff/422/thing/wat", %env);
Expand All @@ -41,8 +41,8 @@ if $result[0] == 404 {
}

# Regex route with named capture group
$router.get(/\/items\/$<id>=(.*)/, sub (%env) {
%env<params><id>;
$router.get(/\/items\/$<id>=(.*)/, sub (%env, $params) {
$params<id>;
});

$result = $router.dispatch('GET', '/items/4221', %env);
Expand All @@ -51,8 +51,8 @@ if $result ~~ '4221' {
};

# Regex route with positional capture group
$router.get(/\/reg\/(.*)\/(.*)/, sub (%env) {
%env<params>[0], %env<params>[1]
$router.get(/\/reg\/(.*)\/(.*)/, sub (%env, $params) {
$params[0], $params[1]
});

$result = $router.dispatch('GET', '/reg/aaa/bbb', %env);
Expand Down

0 comments on commit 3bcb6dc

Please sign in to comment.