X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=br.pl;h=6ac30cc34351a3c56acebf76823cb2166aa3a32f;hb=a7a7a4b960d599b530856a1499d2aacb414aa9ba;hp=604dc59478767c27d805c780b085f985364ab3c5;hpb=483736bb9eab6b22efc73e2085cd525484a714f8;p=scpubgit%2FJSON-Tree-Viewer.git diff --git a/br.pl b/br.pl index 604dc59..6ac30cc 100644 --- a/br.pl +++ b/br.pl @@ -1,6 +1,21 @@ package TB_Temp_Packname; use Web::Simple; +use Scalar::Util qw(blessed); +use IO::All; +use JSON; + +has root => (is => 'lazy'); + +has json => (is => 'lazy'); + +sub _build_root { + io->dir("/home/matthewt/tmp/introspection-data/host/services-dev/stable/node/host/") +} + +sub _build_json { + JSON->new->relaxed +} sub dispatch_request { my ($self) = @_; @@ -9,16 +24,35 @@ sub dispatch_request { ref($_[0][0]) eq 'HASH' ? $self->render_table($_[0][0]) : $_[0] } }, - sub (/) { - return [{ - columns => [ qw(one two three) ], - data => [ - { one => 1, two => 2, three => 3 } - ], - }] + sub (/**/) { + $self->structure($self->descend($self->root, split '/', $_[1])); + }, +} + +sub structure { + my ($self, $data) = @_; + if (ref($data) eq 'HASH') { + $data = [ @{$data}{sort keys %$data} ]; + my @cols = sort keys %{$data->[0]}; + return [ { + columns => \@cols, + data => $data, + } ]; + } else { + die "Confused by $data"; } } +sub descend { + my ($self, $target, @path) = @_; + return $target unless @path; + my $step = shift @path; + if (blessed($target) and $target->isa('IO::All::File')) { + $target = $self->json->decode(scalar $target->all); + } + $self->descend($target->{$step}, @path); +} + sub render_table { my ($self, $data) = @_; use HTML::Tags; @@ -28,13 +62,19 @@ sub render_table { ); [ 200, [ 'Content-type' => 'text/html' ], [ HTML::Tags::to_html_string( + , , "\n", , "\n", (map {; - ' ', , (map { } @$_), , "\n" + ' ', , + (map { } @$_), + , "\n" } @rows),
, $_,
, $self->render_el($_),
, "\n", + , , "\n", ) ] ]; } +sub render_el { ref($_[1]) eq 'ARRAY' ? join(', ', @{$_[1]}) : $_[1] } + __PACKAGE__->run_if_script;