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