| 1 | package Test2::Harness::UI::Controller::Sweeper; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use Test2::Harness::UI::Sweeper; | ||
| 8 | use Test2::Harness::UI::Response qw/resp error/; | ||
| 9 | use Test2::Harness::Util::JSON qw/encode_json encode_pretty_json/; | ||
| 10 | |||
| 11 | use parent 'Test2::Harness::UI::Controller'; | ||
| 12 | use Test2::Harness::UI::Util::HashBase; | ||
| 13 | |||
| 14 | sub title { 'Sweeper' } | ||
| 15 | |||
| 16 | sub handle { | ||
| 17 | my $self = shift; | ||
| 18 | my ($route) = @_; | ||
| 19 | |||
| 20 | my $req = $self->{+REQUEST}; | ||
| 21 | my $res = resp(200); | ||
| 22 | |||
| 23 | die error(401 => 'Login required') unless $req->user; | ||
| 24 | die error(404 => 'Missing route') unless $route; | ||
| 25 | my $count = $route->{count} or die error(404 => 'No count'); | ||
| 26 | my $units = $route->{units} or die error(404 => 'No units'); | ||
| 27 | |||
| 28 | my $interval = "$count $units"; | ||
| 29 | |||
| 30 | my $sweeper = Test2::Harness::UI::Sweeper->new( | ||
| 31 | interval => $interval, | ||
| 32 | config => $self->{+CONFIG}, | ||
| 33 | ); | ||
| 34 | |||
| 35 | my $purged = $sweeper->sweep; | ||
| 36 | |||
| 37 | my $ct = lc($req->parameters->{'content-type'} || $req->parameters->{'Content-Type'} || ''); | ||
| 38 | my $wants_json = $ct eq 'application/json'; | ||
| 39 | $res->content_type($wants_json ? 'application/json' : 'text/html; charset=utf-8'); | ||
| 40 | |||
| 41 | if ($wants_json) { | ||
| 42 | $res->raw_body($purged); | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | $res->raw_body("<pre>" . encode_pretty_json($purged) . "</pre>"); | ||
| 46 | } | ||
| 47 | |||
| 48 | return $res; | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; | ||
| 52 | |||
| 53 | __END__ |