Permalink
Browse files

Dancer::Route->call now returns an object with 'head' and 'body', so …

…we can build smart responses and provide helper functions within action defintions
  • Loading branch information...
Alexis Sukrieh
Alexis Sukrieh committed Jul 20, 2009
1 parent 4527e4e commit 6168e1437c6c9f6abaf6fd97db66256f1a6db2e8
Showing with 52 additions and 63 deletions.
  1. +6 −49 TODO
  2. +5 −0 bin/sample.pl
  3. +24 −7 lib/Dancer.pm
  4. +5 −1 lib/Dancer/HTTP.pm
  5. +7 −1 lib/Dancer/Route.pm
  6. +3 −3 t/routes/001_params_matching.t
  7. +1 −1 t/routes/002_wildcards_matching.t
  8. +1 −1 t/routes/003_regexp_routes.t
View
55 TODO
@@ -1,52 +1,3 @@
-* Conditions support for route matching:
-
- get '/foo', {agent => 'Songbird (\d\.\d)[\d\/]*?'} => sub {
- ...
- };
-
-* New method for handling actions
-
- In the current implementation, an action is run and its return value is
- used as content to print.
-
- We should have a way to compile the action in order to see:
- - if a status has been specified by the action
- - if a content-type has been specified by the action
- - if the action ended up with a string to render
- - if the action ended up with a redirect
- - if the action ended up with a passing
-
- In short, the Action->run mechanism should be aware of what's happening
- inside the action, parse it, and then run it.
-
- I have no idea yet on how to implement this...
-
- Once this is done, the following TODO items will be easy to implement.
-
-* Header definition, per-action
-
- If not specified, use the global setting value.
-
- get '/' => sub {
- ...
-
- content_type 'text/plain';
- "Hello there";
- };
-
-* Status definition, per-action
-
- If not specified, use the "200 OK" status when action is found
-
- get '/' => sub {
- ...
-
- if ($error) {
- status 'not_found';
- return "Document Not found my Lord";
- }
- };
-
* Route passing support
get '/hello/:name' => sub {
@@ -59,6 +10,12 @@
"Hey sukria, welcome dude!"
};
+* Conditions support for route matching:
+
+ get '/foo', {agent => 'Songbird (\d\.\d)[\d\/]*?'} => sub {
+ ...
+ };
+
* Template rendering support
get '/' => sub {
View
@@ -10,6 +10,11 @@
get '/hello/:name' => sub {
my ($params) = @_;
+ if ($params->{name} ne 'sukria') {
+ status('not_found');
+ content_type("text/plain");
+ return "Not found";
+ }
return "Hey ".$params->{name}.", how are you?";
};
View
@@ -5,8 +5,9 @@ use warnings;
use vars qw($VERSION $AUTHORITY @EXPORT);
use Dancer::Config 'setting';
-use Dancer::HTTP 'status';
+use Dancer::HTTP;
use Dancer::Route;
+use Dancer::Response;
use HTTP::Server::Simple::CGI;
use base 'Exporter', 'HTTP::Server::Simple::CGI';
@@ -16,12 +17,16 @@ $VERSION = '0.1';
set
get
post
+ status
+ content_type
);
# syntax sugar for our fellow users :)
sub set { setting(@_) }
sub get { Dancer::Route->add('get', @_) }
sub post { Dancer::Route->add('post', @_) }
+sub status { Dancer::Response::status(@_) }
+sub content_type { Dancer::Response::content_type(@_) }
# The run method to call for starting the job
sub dance {
@@ -42,15 +47,13 @@ sub handle_request {
my $handler = Dancer::Route->find($path, $method);
if ($handler) {
- print status('ok');
- print $cgi->header(setting('content_type'));
my $params = _merge_params(scalar($cgi->Vars), $handler->{params});
- print Dancer::Route->call($handler, $params), "\n";
-
- print STDERR "== $method $path 200 OK (".join(', ', keys(%$params)).")\n" if setting('access_log');
+ my $resp = Dancer::Route->call($handler, $params);
+ print_response($resp, $cgi, $method, $path);
+
}
else {
- print status('not_found');
+ print Dancer::HTTP::status('not_found');
print $cgi->header,
$cgi->start_html('Not found'),
$cgi->h1('Not found'),
@@ -72,6 +75,20 @@ sub _merge_params {
return { %{$cgi_params}, %{$route_params} };
}
+sub print_response {
+ my ($resp, $cgi, $method, $path) = @_;
+
+ my $ct = $resp->{head}{content_type} || setting('content_type');
+ my $st = Dancer::HTTP::status($resp->{head}{status}) || Dancer::HTTP::status('ok');
+
+ print $st;
+ print $cgi->header($ct);
+ print $resp->{body};
+ print "\r\n";
+
+ print STDERR "== $method $path $st";
+}
+
'Dancer';
__END__
View
@@ -14,6 +14,10 @@ my $HTTP_CODES = {
forbidden => '503 Forbidden',
};
-sub status { "HTTP/1.0 " . $HTTP_CODES->{lc($_[0])} . "\r\n" }
+sub status {
+ my $name = shift;
+ return undef unless exists $HTTP_CODES->{lc($name)};
+ return "HTTP/1.0 " . $HTTP_CODES->{lc($name)} . "\r\n";
+}
'Dancer::HTTP';
View
@@ -32,7 +32,13 @@ sub find {
sub call {
my ($class, $handler, $params) = @_;
$params ||= $handler->{params};
- $handler->{code}->($params);
+
+ my $content = $handler->{code}->($params);
+ my $response = Dancer::Response->current;
+ return {
+ head => $response,
+ body => $content
+ };
}
sub match {
@@ -18,15 +18,15 @@ BEGIN {
my $handle = Dancer::Route->find('/');
ok( $handle, 'route found for /');
-is(Dancer::Route->call($handle), 'first', 'first route is OK');
+is(Dancer::Route->call($handle)->{body}, 'first', 'first route is OK');
$handle = Dancer::Route->find('/hello');
ok( !defined($handle), 'no route found for /hello');
$handle = Dancer::Route->find('/hello/sukria');
ok( $handle, 'route found for /hello/sukria');
-is(Dancer::Route->call($handle), 'sukria', 'simple param match found');
+is(Dancer::Route->call($handle)->{body}, 'sukria', 'simple param match found');
$handle = Dancer::Route->find('/hello/sukria/bar');
ok( $handle, 'route found for /hello/sukria/bar');
-is(Dancer::Route->call($handle), 'sukria', 'wrapped param match found');
+is(Dancer::Route->call($handle)->{body}, 'sukria', 'wrapped param match found');
@@ -33,6 +33,6 @@ foreach my $test (@tests) {
$handle = Dancer::Route->find($path);
ok( defined($handle), "route found for path `$path'");
is_deeply(
- Dancer::Route->call($handle), $expected,
+ Dancer::Route->call($handle)->{body}, $expected,
"match data for path `$path' looks good");
}
@@ -30,6 +30,6 @@ foreach my $test (@tests) {
$handle = Dancer::Route->find($path);
ok( defined($handle), "route found for path `$path'");
is_deeply(
- Dancer::Route->call($handle), $expected,
+ Dancer::Route->call($handle)->{body}, $expected,
"match data for path `$path' looks good");
}

0 comments on commit 6168e14

Please sign in to comment.