Fix status_code for ->error_404 | Fix /search
wreis [Sun, 17 Oct 2010 13:03:18 +0000 (10:03 -0300)]
Searching now allows module/dist names with double colon, white space, etc

lib/BackPAN/Web.pm

index c1062a0..51e01ee 100644 (file)
@@ -13,7 +13,7 @@ use File::stat;
 use DateTime;
 use Log::Log4perl 'get_logger';
 
-our $VERSION = '0.12';
+our $VERSION = '0.13';
 
 default_config(
     template_dir => 'root/html',
@@ -89,8 +89,8 @@ sub error_404 {
 
 sub html_response {
     my ( $self, $args ) = @_;
-    my ( $header, $body ) = @$args{qw/header body/};
-    return [ 200, [
+    my ( $status, $header, $body ) = @$args{qw/status_code header body/};
+    return [ $status || 200, [
         $header ? ( %$header ) : (),
         'Content-type' => 'text/html',
     ], ref $body ? $body->to_fh : [ $body ] ];
@@ -354,14 +354,21 @@ sub authors_page_content {
         ->apply($self->add_paging($authors->{'pager'}));
 }
 
+sub _mangle_query_string {
+    my ( $self, $q ) = @_;
+    $q =~ s{\s+|::|\+}{-}g;
+    $q =~ s{-$}{};
+    return $q =~ s{\*}{}g ? "$q%" : "%$q%";
+}
+
 sub search {
     my ( $self, $q, $query_params ) = @_;
-    my $query = lc "%$q%";
+    my $query_str = lc $self->_mangle_query_string($q);
     return $self->dists($query_params)->search({
         -or => [
-            { 'LOWER(me.name)' => { -like => $query } },
-            { 'LOWER(me.first_author)' => { -like => $query } },
-            { 'LOWER(me.latest_author)' => { -like => $query } },
+            { 'LOWER(me.name)' => { -like => $query_str } },
+            { 'LOWER(me.first_author)' => { -like => $query_str } },
+            { 'LOWER(me.latest_author)' => { -like => $query_str } },
         ],
     });
 }
@@ -369,7 +376,6 @@ sub search {
 dispatch {
     subdispatch sub () {
         $self->_build_request_obj_from($_[+PSGI_ENV]);
-        my $body;
         [
             sub (/) {
                 $self->html_response({ body => $self->index_page_content });
@@ -390,57 +396,76 @@ dispatch {
             sub ( /releases|/releases/ + ?* ) {
                 my $release_rs = $self->releases($_[1]);
                 if ( $release_rs->count ) {
-                    $body = $self->releases_page_content($release_rs)
+                    my $body = $self->releases_page_content($release_rs)
                         ->select('#nav-releases')->add_to_attribute(class => 'active');
+                    return $self->html_response({ body => $body });
                 }
                 else {
-                    $body = $self->error_404;
+                    return $self->html_response({
+                        status_code => 404,
+                        body => $self->error_404,
+                    });
                 }
-                $self->html_response({ body => $body });
             },
 
             sub ( /dists|/dists/ + ?* ) {
                 my $dist_rs = $self->dists($_[1]);
                 if ( $dist_rs->count ) {
-                    $body = $self->dists_page_content($dist_rs)
+                    my $body = $self->dists_page_content($dist_rs)
                         ->select('#nav-dists')->add_to_attribute(class => 'active');
+                    return $self->html_response({ body => $body });
                 }
                 else {
-                    $body = $self->error_404;
+                    return $self->html_response({
+                        status_code => 404,
+                        body => $self->error_404,
+                    });
                 }
-                $self->html_response({ body => $body });
             },
 
             sub ( /distribution/*|/distribution/*/ + ?* ) {
                 if ( my $dist = $self->get_dist($_[1]) ) {
-                    $body = $self->dist_info_page_content($dist, $_[2]);
+                    my $body = $self->dist_info_page_content($dist, $_[2]);
+                    return $self->html_response({ body => $body });
                 }
                 else {
-                    $body = $self->error_404;
+                    return $self->html_response({
+                        status_code => 404,
+                        body => $self->error_404,
+                    });
                 }
-                $self->html_response({ body => $body });
             },
 
             sub ( /authors|/authors/ + ?* ) {
                 if ( my $authors = $self->authors($_[1]) ) {
-                    $body = $self->authors_page_content($authors)
+                    my $body = $self->authors_page_content($authors)
                         ->select('#nav-authors')->add_to_attribute(class => 'active');
+                    return $self->html_response({ body => $body });
                 }
                 else {
-                    $body = $self->error_404;
+                    return $self->html_response({
+                        status_code => 404,
+                        body => $self->error_404,
+                    });
                 }
-                $self->html_response({ body => $body });
             },
 
             sub ( /search|/search/ + ?q=&* ) {
-                my $dist_rs = $self->search(@_[1,2]);
+                my ( $self, $query_str, $query_params ) = @_;
+                my $dist_rs = $self->search($query_str, $query_params);
                 if ( $dist_rs->count ) {
-                    $body = $self->dists_page_content($dist_rs);
+                    my $body = $self->dists_page_content($dist_rs)
+                        ->select('#q')->add_to_attribute(
+                            value => $query_str
+                        );
+                    return $self->html_response({ body => $body });
                 }
                 else {
-                    $body = $self->error_404;
+                    return $self->html_response({
+                        status_code => 404,
+                        body => $self->error_404,
+                    });
                 }
-                $self->html_response({ body => $body });
             },
         ],
     },