show dwarn
[scpubgit/JSON-Tree-Viewer.git] / br.pl
CommitLineData
483736bb 1package TB_Temp_Packname;
2
3use Web::Simple;
56168e97 4use Scalar::Util qw(blessed);
5use IO::All;
6use JSON;
7
8has root => (is => 'lazy');
9
10has json => (is => 'lazy');
11
12sub _build_root {
13 io->dir("/home/matthewt/tmp/introspection-data/host/services-dev/stable/node/host/")
14}
15
16sub _build_json {
17 JSON->new->relaxed
18}
483736bb 19
20sub dispatch_request {
21 my ($self) = @_;
22 sub () {
23 response_filter {
24 ref($_[0][0]) eq 'HASH' ? $self->render_table($_[0][0]) : $_[0]
25 }
26 },
56168e97 27 sub (/**/) {
28 $self->structure($self->descend($self->root, split '/', $_[1]));
29 },
30}
31
32sub structure {
33 my ($self, $data) = @_;
34 if (ref($data) eq 'ARRAY') {
35 my @cols = sort keys %{$data->[0]};
36 return [ {
37 columns => \@cols,
38 data => $data,
39 } ];
40 } else {
41 die "Confused by $data";
42 }
43}
44
45sub descend {
46 my ($self, $target, @path) = @_;
47 my $step = shift @path;
48 if (blessed($target) and $target->isa('IO::All::File')) {
49 $target = $self->json->decode(scalar $target->all);
483736bb 50 }
56168e97 51 $self->descend(::Dwarn($target->{$step}), @path);
483736bb 52}
53
54sub render_table {
55 my ($self, $data) = @_;
56 use HTML::Tags;
57 my @rows = (
58 $data->{columns},
59 map [ @{$_}{@{$data->{columns}}} ], @{$data->{data}}
60 );
61 [ 200, [ 'Content-type' => 'text/html' ], [
62 HTML::Tags::to_html_string(
56168e97 63 <html>, <body>, "\n",
483736bb 64 <table>, "\n",
65 (map {;
66 ' ', <tr>, (map { <td>, $_, </td> } @$_), </tr>, "\n"
67 } @rows),
68 </table>, "\n",
56168e97 69 </body>, </html>, "\n",
483736bb 70 )
71 ] ];
72}
73
74__PACKAGE__->run_if_script;