Skip to content

Commit

Permalink
[Astaire] various non-semantic changes
Browse files Browse the repository at this point in the history
Whitespace cleanup, some ideomatizations etc.
  • Loading branch information
Carl Masak committed Dec 7, 2009
1 parent 572cc57 commit f53a394
Showing 1 changed file with 34 additions and 41 deletions.
75 changes: 34 additions & 41 deletions lib/Astaire.pm
@@ -1,4 +1,5 @@
#!/usr/bin/perl6
use v6;

use Web::Request;
use Web::Response;

Expand All @@ -7,85 +8,77 @@ class Handler {
has Block $.code;
has Str $.http_method;

method matches( $path ){
method matches($path) {
my %result;
my $clean_path = $path.subst( / ^\/ /, '', :g );
my $condition = $.condition.subst( / ^\/ /, '', :g ).subst( / \. /, '\.', :g ).subst( / \/ /, '\/', :g ).subst( / \* /, '(.*)', :g );
my $clean_path = $path.subst(/ ^\/ /, '');
my $condition
= $.condition.subst(/ ^\/ /, '', :g )\
.subst(/ \. /, '\.', :g )\
.subst(/ \/ /, '\/', :g )\
.subst(/ \* /, '(.*)', :g );
$condition = "/^ $condition \$/";
#"$condition against $clean_path".say;
# RAKUDO : There must be a nicer way to do this ( eg. no eval ) once we have regex interpolation stuff
# RAKUDO : submethod BUILD doesn't work ( forgets its args ), we should eval the regex only on BUILD and then store it
my $condition_regex = (eval " $condition ");
# RAKUDO: Doing eval here until we get variable interpolation in
# regexes.
# RAKUDO: submethod BUILD doesn't work (forgets its args), we
# should eval the regex only on BUILD and then store it
my $condition_regex = eval $condition;
my $match = $clean_path.match($condition_regex);
%result<splat> = @($match).map({ ~$_ });
%result<success> = ?($match);
%result<success> = ?$match;
return %result;
}

method explode( Str $target ){
my @path = $target.split('/');
@path.shift() if @path[0] eq '';
return @path
}

};
}

class Dispatch {
has @.handlers;
has Handler @.handlers handles <push>;

method push ( Handler $handler ){
@.handlers.push( $handler );
}

method dispatch ( Web::Request $request ){
method dispatch(Web::Request $request) {
my Web::Response $response .= new();

for @.handlers -> $candidate {
my %match = $candidate.matches( $request.path_info );
if %match{'success'} and $candidate.http_method eq $request.request_method {
if %match<success>
&& $candidate.http_method eq $request.request_method {
my $code = $candidate.code;
$response.write( $code(|%match<splat>) );
return $response;
}
}

#Not found
# Not found
$response.status = 404;
return $response;
}
};

}

#Rack compliant application
# Rack-compliant application
class AstaireApp {

has Dispatch $.dispatch is rw;

method call ( Web::Request $request ){
return $.dispatch.dispatch( $request );
method call(Web::Request $request) {
return $.dispatch.dispatch($request);
}
};
}

module Astaire {

my Dispatch $dispatch .= new();

sub get( Pair $param ) is export {
my ( $condition, $code ) = $param.kv;
sub get(Pair $param) is export {
my ($condition, $code) = $param.kv;
_push_to_dispatch( $condition, $code,'GET' );
};

sub post( Pair $param ) is export {
my ( $condition, $code ) = $param.kv;
sub post(Pair $param) is export {
my ($condition, $code) = $param.kv;
_push_to_dispatch( $condition, $code,'POST' );
};

sub _push_to_dispatch ( $condition, $code, $http_method ){
$dispatch.push( Handler.new( condition => $condition, code => $code, http_method => $http_method ) );
sub _push_to_dispatch ($condition, $code, $http_method) {
$dispatch.push( Handler.new(:$condition, :$code, :$http_method) );
}

sub application () is export {
my AstaireApp $application .= new( dispatch => $dispatch );
my AstaireApp $application .= new(:$dispatch);
return $application;
}
};
}

0 comments on commit f53a394

Please sign in to comment.