| 1 | package MVC::Neaf::View::JS; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 25 | our $VERSION = '0.2901'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | MVC::Neaf::View::JS - JSON-based view for Not Even A Framework. | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | See L<MVC::Neaf>. | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | use MVC::Neaf; | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | # define route ... | ||||||||||||||||||||||||||||||||
| 19 | sub { | ||||||||||||||||||||||||||||||||
| 20 | return { | ||||||||||||||||||||||||||||||||
| 21 | # your data ... | ||||||||||||||||||||||||||||||||
| 22 | -view => 'JS', # this is the default as of 0.20 | ||||||||||||||||||||||||||||||||
| 23 | -jsonp => 'my.jsonp.callback', # this is optional | ||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | Will result in your application returning raw data in JSON/JSONP format | ||||||||||||||||||||||||||||||||
| 28 | instead or rendering a template. | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 25 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | 25 | ▶ | use MVC::Neaf::Util qw(JSON); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 25 | ▶ | use parent qw(MVC::Neaf::View); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 25 | my $js_id_re = qr/[A-Z_a-z][A-Z_a-z\d]*/; | |||||||||||||||||||||||||||||||
| 40 | 25 | my $jsonp_re = qr/^$js_id_re(?:\.$js_id_re)*$/; | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | =head2 new( %options ) | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | %options may include: | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | =over | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | =item * want_pretty - sort keys & indent output | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | =item * want_sorted - sort keys (this defaults to want_pretty) | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | =item * preserve_dash - don't strip dashed options. Useful for debugging. | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | =back | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 25 | my %new_keys; | |||||||||||||||||||||||||||||||
| 59 | 25 | $new_keys{$_}++ for qw( neaf_base_dir preserve_dash want_pretty want_sorted ); | |||||||||||||||||||||||||||||||
| 60 | 23 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 61 | 23 | my ($class, %opt) = @_; | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 23 | my @extra = grep { !$new_keys{$_} } keys %opt; | |||||||||||||||||||||||||||||||
| 64 | 23 | ▶ | croak "NEAF $class->new: unexpected keys @extra" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 65 | if @extra; | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | $opt{want_sorted} = $opt{want_pretty} | ||||||||||||||||||||||||||||||||
| 68 | 23 | ▶ | unless defined $opt{want_sorted}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | # No utf8 here (yet), will encode upon leaving the perimeter | ||||||||||||||||||||||||||||||||
| 70 | 23 | my $codec = JSON->new->allow_blessed->convert_blessed | |||||||||||||||||||||||||||||||
| 71 | ->allow_unknown->allow_nonref; | ||||||||||||||||||||||||||||||||
| 72 | $codec->pretty(1) | ||||||||||||||||||||||||||||||||
| 73 | 23 | ▶ | if $opt{want_pretty}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | $codec->canonical(1) | ||||||||||||||||||||||||||||||||
| 75 | 23 | ▶ | if $opt{want_sorted}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 23 | return bless { | |||||||||||||||||||||||||||||||
| 78 | %opt, | ||||||||||||||||||||||||||||||||
| 79 | codec => $codec, | ||||||||||||||||||||||||||||||||
| 80 | }, $class; | ||||||||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | =head2 render( \%data ) | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | Returns a scalar with JSON-encoded data. | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 44 | ▶ | sub render { | ||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 90 | 44 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 44 | my $callback = $data->{-jsonp}; | |||||||||||||||||||||||||||||||
| 93 | 44 | my $type = $data->{-type}; | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 44 | ▶ | if( exists $data->{-payload} || exists $data->{-serial} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
exists $data->{"-payload"} or exists $data->{"-serial"}
MC/DC50%1/2
exists $data->{"-payload"} or exists $data->{"-serial"}
exists $data->{"-payload"}
exists $data->{"-serial"}
Truth table66%2/3
A or B
A= exists $data->{"-payload"}B= exists $data->{"-serial"}
| |||||||||||||||||||||||||||||||||
| 96 | 5 | ▶ | $data = $data->{-payload} || $data->{-serial}; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$data->{'-payload'} || $data->{'-serial'}
MC/DC50%1/2
$data->{'-payload'} || $data->{'-serial'}
$data->{'-payload'}
$data->{'-serial'}
Truth table66%2/3
A || B
A= $data->{'-payload'}B= $data->{'-serial'}
| |||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | elsif ( !$self->{preserve_dash} ) { | ||||||||||||||||||||||||||||||||
| 99 | # This is the default - get rid of control keys, but | ||||||||||||||||||||||||||||||||
| 100 | # don't spoil original data | ||||||||||||||||||||||||||||||||
| 101 | 39 | $data = do { | |||||||||||||||||||||||||||||||
| 102 | 39 | my %shallow_copy; | |||||||||||||||||||||||||||||||
| 103 | /^-/ or $shallow_copy{$_} = $data->{$_} | ||||||||||||||||||||||||||||||||
| 104 | 39 | ▶ | for keys %$data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | 39 | \%shallow_copy; | |||||||||||||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 44 | my $content = $self->{codec}->encode( $data ); | |||||||||||||||||||||||||||||||
| 110 | 44 | ▶ | return $callback && $callback =~ $jsonp_re | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$callback and $callback =~ /$jsonp_re/
MC/DC100%2/2
$callback and $callback =~ /$jsonp_re/
$callback
$callback =~ /$jsonp_re/
Truth table100%3/3
A and B
A= $callbackB= $callback =~ /$jsonp_re/
| |||||||||||||||||||||||||||||||||
| 111 | ? ("$callback($content);", "application/javascript; charset=utf-8") | ||||||||||||||||||||||||||||||||
| 112 | : ($content, "application/json; charset=utf-8"); | ||||||||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | This module is part of L<MVC::Neaf> suite. | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>. | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||
| 122 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||
| 123 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 25 | 1; | |||||||||||||||||||||||||||||||