| 1 | package Dancer2::Plugin::SendAs; | ||
| 2 | # ABSTRACT: (DEPRECATED) Dancer2 plugin to send data as specific content type | ||
| 3 | |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 2 | ▶ | use Dancer2::Plugin; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 2 | ▶ | use Class::Load 'try_load_class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use List::Util 'first'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 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__ | ||