| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Dancer::Plugin::FormattedOutput; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
46564
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
38
|
|
|
4
|
1
|
|
|
1
|
|
7
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
40
|
|
|
5
|
1
|
|
|
1
|
|
489
|
use Dancer ':syntax'; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Dancer::Plugin; |
|
7
|
|
|
|
|
|
|
use feature 'switch'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Dancer::Plugin::FormattedOutput - Provide output in a variety of formats |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 VERSION |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Version 0.01 |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use constant { |
|
22
|
|
|
|
|
|
|
# Formats |
|
23
|
|
|
|
|
|
|
JSON_FT => "json", |
|
24
|
|
|
|
|
|
|
XML_FT => "xml", |
|
25
|
|
|
|
|
|
|
YAML_FT => "yaml", |
|
26
|
|
|
|
|
|
|
TEXT_FT => "text", |
|
27
|
|
|
|
|
|
|
HTML_FT => "html", |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Content Types |
|
30
|
|
|
|
|
|
|
JSONP_CT => "text/javascript", |
|
31
|
|
|
|
|
|
|
JSON_CT => "application/json", |
|
32
|
|
|
|
|
|
|
XML_CT => 'text/xml', |
|
33
|
|
|
|
|
|
|
YAML_CT => 'application/yaml', |
|
34
|
|
|
|
|
|
|
TEXT_CT => "text/plain", |
|
35
|
|
|
|
|
|
|
}; |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Similar in functionality to the standard Dancer serialisation routines, |
|
40
|
|
|
|
|
|
|
this module provides functions for serialising output to a variety of formats. |
|
41
|
|
|
|
|
|
|
Where it differs from the default Dancer functionality is that it: |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=over |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=item Correctly sets the content type |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item Allows html as a format using templates |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item Allows per-call configuration of default format |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item Works with jsonp |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=back |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
use Dancer::Plugin::FormattedOutput; |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
get '/some/route' => sub { |
|
58
|
|
|
|
|
|
|
my $data = get_data(); |
|
59
|
|
|
|
|
|
|
format "template" => $data; |
|
60
|
|
|
|
|
|
|
}; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
get '/some/other/route' => sub { |
|
63
|
|
|
|
|
|
|
my $data = get_data(); |
|
64
|
|
|
|
|
|
|
format $data; |
|
65
|
|
|
|
|
|
|
}; |
|
66
|
|
|
|
|
|
|
... |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 EXPORT |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
The function "format" is automatically exported. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 format |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
This function is exported in the calling namespace, and it manages |
|
77
|
|
|
|
|
|
|
the formatting of data into the various available formats. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
It can be called as: |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
format($data) |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
format("template_name" => $data); |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
format($data, "default_format"); |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
format("template_name", $data, "default_format"); |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
register format_output => sub { |
|
92
|
|
|
|
|
|
|
my ($data, $template, $format, $callback, $default_format); |
|
93
|
|
|
|
|
|
|
if (@_ == 1) { |
|
94
|
|
|
|
|
|
|
$data = shift; |
|
95
|
|
|
|
|
|
|
} elsif (@_ == 2 && ! ref $_[0]) { |
|
96
|
|
|
|
|
|
|
($template, $data) = @_; |
|
97
|
|
|
|
|
|
|
} elsif (@_ == 2 && ! ref $_[1]) { |
|
98
|
|
|
|
|
|
|
($data, $default_format) = @_; |
|
99
|
|
|
|
|
|
|
} elsif (@_ == 3) { |
|
100
|
|
|
|
|
|
|
($template, $data, $default_format) = @_; |
|
101
|
|
|
|
|
|
|
} else { |
|
102
|
|
|
|
|
|
|
die "Wrong number of arguments to format: got @_"; |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
$format = params->{ plugin_setting->{"format_parameter"} || "format" }; |
|
106
|
|
|
|
|
|
|
$callback = params->{ plugin_setting->{"callback_parameter"} || "callback" }; |
|
107
|
|
|
|
|
|
|
$default_format = $default_format || plugin_setting->{default_format} || JSON_FT; |
|
108
|
|
|
|
|
|
|
$format = $format || $default_format; |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
given($format) { |
|
111
|
|
|
|
|
|
|
when(/^\.?json$/i) { |
|
112
|
|
|
|
|
|
|
return return_json($data, $callback); |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
when(/^\.?xml$/i) { |
|
115
|
|
|
|
|
|
|
return return_xml($data); |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
when(/^\.?ya?ml$/i) { |
|
118
|
|
|
|
|
|
|
return return_yaml($data); |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
when(/^\.?html?$/i) { |
|
121
|
|
|
|
|
|
|
return template $template, $data; |
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
when(/^\.?te?xt$/i) { |
|
124
|
|
|
|
|
|
|
return return_text($data); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
default { |
|
127
|
|
|
|
|
|
|
die "Could not handle format: $format"; |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
}; |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 return_json |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
The formatter for json. It appends the callback if any, |
|
135
|
|
|
|
|
|
|
and sets the content type to application/json or |
|
136
|
|
|
|
|
|
|
text/javascript as appropriate |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub return_json { |
|
141
|
|
|
|
|
|
|
my ($data, $callback) = @_; |
|
142
|
|
|
|
|
|
|
my $ret = $callback ? "$callback(" : ''; |
|
143
|
|
|
|
|
|
|
$ret .= to_json($data); |
|
144
|
|
|
|
|
|
|
$ret .= ");" if $callback; |
|
145
|
|
|
|
|
|
|
content_type($callback ? JSONP_CT : JSON_CT); |
|
146
|
|
|
|
|
|
|
return $ret; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 return_xml |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
The formatter for xml. It sets the content type, and does a |
|
152
|
|
|
|
|
|
|
basic transformation to xml. |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub return_xml { |
|
157
|
|
|
|
|
|
|
my $data = shift; |
|
158
|
|
|
|
|
|
|
content_type(XML_CT); |
|
159
|
|
|
|
|
|
|
return to_xml($data); |
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 return_yaml |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
The formatter for yaml. It sets the content type and does a |
|
165
|
|
|
|
|
|
|
basic transformation to yaml. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=cut |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub return_yaml { |
|
170
|
|
|
|
|
|
|
my $data = shift; |
|
171
|
|
|
|
|
|
|
content_type(YAML_CT); |
|
172
|
|
|
|
|
|
|
to_yaml($data); |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 return_text |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
The formatter for text. It sets the content type, and, |
|
178
|
|
|
|
|
|
|
if the data is a hashref and has a key named "text", returns |
|
179
|
|
|
|
|
|
|
the value of that key. Otherwise it returns a Data::Dumper |
|
180
|
|
|
|
|
|
|
version of the data. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub return_text { |
|
185
|
|
|
|
|
|
|
my $data = shift; |
|
186
|
|
|
|
|
|
|
content_type(TEXT_CT); |
|
187
|
|
|
|
|
|
|
if (ref $data eq 'HASH' and $data->{text}) { |
|
188
|
|
|
|
|
|
|
return $data->{text}; |
|
189
|
|
|
|
|
|
|
} else { |
|
190
|
|
|
|
|
|
|
return to_dumper($data); |
|
191
|
|
|
|
|
|
|
} |
|
192
|
|
|
|
|
|
|
} |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
register_plugin; |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 AUTHOR |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Alex Kalderimis, C<< >> |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 BUGS |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
|
203
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
|
204
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 SUPPORT |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
perldoc Dancer::Plugin::FormattedOutput |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
You can also look for information at: |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=over 4 |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
L |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
L |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item * Search CPAN |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
L |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=back |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Copyright 2011 Alex Kalderimis. |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
244
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
|
245
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=cut |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
1; # End of Dancer::Plugin::FormattedOutput |