File Coverage

blib/lib/Rest/HtmlVis.pm
Criterion Covered Total %
statement 8 57 14.0
branch 0 32 0.0
condition 0 3 0.0
subroutine 3 8 37.5
pod 2 4 50.0
total 13 104 12.5


line stmt bran cond sub pod time code
1             package Rest::HtmlVis;
2              
3 1     1   13147 use 5.008_005;
  1         2  
4 1     1   3 use strict;
  1         2  
  1         19  
5 1     1   2 use warnings FATAL => 'all';
  1         5  
  1         459  
6              
7             our $VERSION = '0.11'; # Set automatically by milla
8              
9             my $based = {
10             'default.base' => 'Rest::HtmlVis::Base',
11             'default.content' => 'Rest::HtmlVis::Content',
12             };
13              
14             sub new {
15 0     0 1   my ($class, $params) = @_;
16              
17 0           my $htmlVis;
18              
19 0           my $self = bless {}, $class;
20              
21             ### Set uri path
22 0           $self->{baseurl} = "/static";
23             # Don't delete key, because of original hash
24 0 0         $self->{baseurl} = $params->{'default.baseurl'} if exists $params->{'default.baseurl'};
25              
26             ### Add htmlvis
27 0           foreach my $key (sort keys %$params){
28 0 0         next if $key eq 'default.baseurl';
29 0           $self->loadVisObject($key, $params->{$key});
30             }
31              
32             ### Set params
33 0           foreach my $key (sort keys %$based) {
34 0 0         $self->loadVisObject($key, $based->{$key}) unless exists $params->{$key};
35             }
36              
37 0           return $self;
38             }
39              
40             sub baseurl {
41 0     0 0   my ($self) = shift;
42 0           return $self->{baseurl};
43             }
44              
45             sub loadVisObject {
46 0     0 0   my ($self, $key, $class) = @_;
47              
48 0           my ($rtrn, $err) = _try_load($class);
49 0 0         if ($rtrn){
50 0           my $vis = $class->new($self->baseurl);
51 0           my $order = $vis->getOrder;
52 0 0         push(@{$self->{htmlVis}{$order}}, {
  0            
53             key => $key,
54             object => $vis
55             }) if $vis->isa('Rest::HtmlVis::Key');
56             }else{
57 0           print STDERR "ERROR: to load vis class".$err."\n";
58             }
59             }
60              
61             sub html {
62 0     0 1   my ($self, $struct, $env) = @_;
63              
64             ### manage keys
65 0           my $head_parts = '';
66 0           my $onload_parts = '';
67 0           my $html_parts = '';
68              
69 0           my $rowBlocks = 0; # count number of blocks in row
70              
71             ### Add blocks
72 0           foreach my $order (sort keys %{$self->{htmlVis}}) {
  0            
73 0           foreach my $obj (@{$self->{htmlVis}{$order}}) {
  0            
74              
75 0           my $vis = $obj->{object};
76 0 0         next unless $vis->setStruct($obj->{key}, $struct, $env);
77              
78 0           my $head = $vis->head($self->{local});
79 0 0         $head_parts .= $head if $head;
80              
81 0           my $onload = $vis->onload();
82 0 0         $onload_parts .= $onload if $onload;
83              
84 0           my $html = $vis->html();
85 0 0         if ($html){
86 0           $rowBlocks += $vis->blocks();
87 0 0 0       my $newRow = ($vis->newRow() or $rowBlocks > 12) ? 1 : 0;
88              
89 0 0         $html_parts .= '
' if $newRow;
90 0           $html_parts .= $html;
91 0 0         $html_parts .= '' if $newRow;
92 0 0         $rowBlocks = 0 if $newRow;
93             }
94             }
95             }
96              
97 0           return "\n\n\n$head_parts\n\n\n$html_parts\n\n";
98             }
99              
100             ### Try load library
101             sub _try_load {
102 0     0     my $mod = shift;
103              
104 0 0         return 0 unless $mod;
105 0 0         return 1 if ($mod->can("html")); # because of local class in psgi
106 0 0         eval("use $mod; 1") ? return 1 : return (0, $@);
107             }
108              
109             1;
110              
111             =encoding utf-8
112              
113             =head1 NAME
114              
115             Rest::HtmlVis - Rest API visualizer in HTML
116              
117             =head1 SYNOPSIS
118              
119             Transform perl hash to html.
120             Each key in perl hash is transormed to the piece of html, js and css which are include in main html.
121              
122             Example:
123              
124             use Rest::HtmlVis;
125              
126             my $htmlvis = Rest::HtmlVis->new({
127             events => Rest::HtmlVis::Events
128             });
129              
130             $htmlvis->html({
131              
132             events => [
133             ],
134              
135             links => {
136             rel => 'root',
137             href => /,
138             name => Root resource
139             }
140              
141             form => {
142             GET => {
143             from => {
144             type => 'time',
145             default => time(),
146             }
147             },
148             POST => {
149             DATA => {
150             type => "text"
151             temperature => 25
152             },
153              
154             }
155             }
156             });
157              
158              
159             HtmlVis has default blocks that are show everytime:
160              
161             =over 4
162              
163             =item * default.baseurl
164              
165             Set default prefix for links in html. Default is '/static'
166              
167             =item * default.base
168              
169             See L.
170              
171             =item * default.content
172              
173             See L.
174              
175             =back
176              
177             These blocks can be rewrite when the base or content key is set in constructor params.
178              
179             =head1 SUBROUTINES/METHODS
180              
181             =head2 new( params )
182              
183             Create new htmlvis object. You have to specify params for keys that should be transformed.
184              
185             =head3 params
186              
187             Define keys in input hash and library that manage this key.
188              
189             Example:
190              
191             { events => Rest::HtmlVis::Events }
192              
193             Third-party js library are primary mapped to /static URL.
194             You have to manage this url by your http server and map this url to share directory.
195             For example in Plack:
196            
197             use File::ShareDir;
198             my $share = File::ShareDir::dist_dir('Rest-HtmlVis') || "../Rest-HtmlVis/share/";
199             mount "/static" => Plack::App::File->new(root => $share);
200              
201             =cut
202              
203             =head2 html( hash_struct )
204              
205             Convert input hash struct to html. Return html string.
206              
207             =cut
208              
209             =head1 TUTORIAL
210              
211             L
212              
213             =head1 AUTHOR
214              
215             Václav Dovrtěl Evaclav.dovrtel@gmail.comE
216              
217             =head1 BUGS
218              
219             Please report any bugs or feature requests to github repository.
220              
221             =head1 ACKNOWLEDGEMENTS
222              
223             Inspired by L
224              
225             =head1 COPYRIGHT
226              
227             Copyright 2015- Václav Dovrtěl
228              
229             =head1 LICENSE
230              
231             This library is free software; you can redistribute it and/or modify
232             it under the same terms as Perl itself.
233              
234             =cut