Support filtering by version in App::Web::NAOdash
[nethack-naodash.git] / lib / App / Web / NAOdash.pm
index 985aaccd5f3bf94be8816edd21e6e949cd35987e..be36928bd3f0f61a3ea4fb8125162d270fd06747 100644 (file)
@@ -13,6 +13,7 @@ use File::Slurp;
 use HTML::TreeBuilder;
 use JSON::MaybeXS qw/encode_json/;
 use NetHack::NAOdash qw/naodash_user/;
+use Plack::Request;
 
 my ($dash, $css, $css_hash);
 
@@ -49,7 +50,7 @@ sub format_time {
 }
 
 sub make_html {
-       my ($name, $result) = @_;
+       my ($name, $query, $result) = @_;
        my @checks = @{$result->{checks}};
        my %numbers = %{$result->{numbers}};
        $numbers{totalrealtime} = format_time $numbers{totalrealtime};
@@ -70,6 +71,8 @@ sub make_html {
                warn "No element for check $id" unless $el; ## no critic (RequireCarping)
                $el->delete_content->push_content($num);
        }
+       my $ahref = $tree->look_down(href => "?$query");
+       $ahref->replace_with(join '', $ahref->content_list) if $ahref;
        $tree->as_HTML;
 }
 
@@ -87,12 +90,17 @@ sub reply {
 
 sub call {
        my ($self, $env) = @_;
-       return reply 400, 'Bad request: user contains characters outside [a-zA-Z0-9_]' unless $env->{PATH_INFO} =~ m{^/(\w+)$};
+       my $req = Plack::Request->new($env);
+       return reply 400, 'Bad request: user contains characters outside [a-zA-Z0-9_]' unless $req->path =~ m{^/(\w+)$};
        my $name = $1;
-       my $result = eval { naodash_user $name } or return reply 500, $@;
+       my %args = (
+               include_versions => [$req->query_parameters->get_all('include_versions')],
+               exclude_versions => [$req->query_parameters->get_all('exclude_versions')],
+       );
+       my $result = eval { naodash_user \%args, $name } or return reply 500, $@;
 
        return reply 200, encode_json($result), 'application/json' if $self->{json};
-       return reply 200, make_html($name, $result), 'text/html';
+       return reply 200, make_html($name, $req->query_string, $result), 'text/html';
 }
 
 1;
@@ -124,6 +132,12 @@ It handles URLs of the form C</username>, where I<username> is a NAO
 username. It retrieves the xlogfile from NAO and returns the result of
 the analysis.
 
+Two query parameters are accepted: include_versions and
+exclude_versions, both of which can take multiple values by
+specifiying them multiple times. They are passed directly to the
+B<naodash_user> function, see the documentation of L<NetHack::NAOdash>
+for an explanation of their function.
+
 The constructor takes a single named parameter, I<json>, that is false
 by default. The result will be returned as HTML is I<json> is false,
 as JSON if I<json> is true.
This page took 0.01124 seconds and 4 git commands to generate.