line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Plugin::SendAs; |
2
|
|
|
|
|
|
|
# ABSTRACT: (DEPRECATED) Dancer2 plugin to send data as specific content type |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
420438
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
67
|
|
5
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
434
|
use Dancer2::Plugin; |
|
1
|
|
|
|
|
8399
|
|
|
1
|
|
|
|
|
5
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
2234
|
use Class::Load 'try_load_class'; |
|
1
|
|
|
|
|
10453
|
|
|
1
|
|
|
|
|
46
|
|
10
|
1
|
|
|
1
|
|
6
|
use Encode; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
58
|
|
11
|
1
|
|
|
1
|
|
3
|
use List::Util 'first'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
171
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.002'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
register send_as => sub { |
16
|
|
|
|
|
|
|
my ( $dsl, $type, $data ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# allow lower cased serializer names |
19
|
|
|
|
|
|
|
my $serializer_class = first { try_load_class($_) } |
20
|
|
|
|
|
|
|
map { "Dancer2::Serializer::$_" } |
21
|
|
|
|
|
|
|
( uc $type, $type ); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my %options = (); |
24
|
|
|
|
|
|
|
my $content; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
if ( $serializer_class ) { |
27
|
|
|
|
|
|
|
my $serializer = $serializer_class->new; |
28
|
|
|
|
|
|
|
$content = $serializer->serialize( $data ); |
29
|
|
|
|
|
|
|
$options{content_type} = $serializer->content_type; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
else { |
32
|
|
|
|
|
|
|
# send as HTML |
33
|
|
|
|
|
|
|
# TODO use content type of app |
34
|
|
|
|
|
|
|
$content = Encode::encode( 'UTF-8', $data ); |
35
|
|
|
|
|
|
|
$options{content_type} = 'text/html; charset=UTF-8'; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$dsl->app->send_file( \$content, %options ); |
39
|
|
|
|
|
|
|
}, { prototype => '$@' }; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
register_plugin; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
1; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
__END__ |