blib/lib/Catalyst/View/REST/Data/Serializer.pm

filter: Statementstmt 45.0% 18 / 40 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 37.5% 24 / 64 CC 8 SCAR 38.0
CC
8
Cov
33%
process
72.0
BEGIN
1.0
BEGIN
1.0
CRAP 44.7
↑ Summary
1 package Catalyst::View::REST::Data::Serializer;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw/Catalyst::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = "0.02";
10  
11 1 __PACKAGE__->mk_classdata('_serializer');
12  
13 =head1 NAME
14  
15 Catalyst::View::REST::Data::Serializer - Data::Serializer View Class
16  
17 =head1 SYNOPSIS
18  
19 # lib/MyApp/View/REST.pm
20 package MyApp::View::REST;
21  
22 use base 'Catalyst::View::REST::Data::Serializer';
23  
24 1;
25  
26 $c->forward('MyApp::View::REST');
27  
28 =head1 DESCRIPTION
29  
30 This is the C<Data::Serializer> view class. It can be used to use any
31 number of Serialization methods (YAML, Storable, Data::Dumper) to implement
32 a REST view. It also supports optional compression, encryption, and a host
33 of other useful goodies.
34  
35 =head2 CONFIGURATION OPTIONS
36  
37 Any of the options you can pass to L<Data::Serializer> you can put into
38 $c->config->{'serializer'}, and have them passed on to it. If you don't
39 pass any options, the following are used:
40  
41 serializer => 'Data::Dumper',
42 digester => 'SHA1',
43 cipher => 'Blowfish',
44 secret => undef,
45 portable => '1',
46 compress => '0',
47 serializer_token => '1',
48 options => {},
49  
50 They are the same as the Data::Serializer defaults. The two additional
51 options are:
52  
53 =head3 astext
54  
55 Setting this to a true value will allow you to pass the "astext=1" param to
56 any request processed by this View. The results will be the contents of
57 $c->stash passed through to Data::Dumper, as opposed to your Serialized object.
58  
59 This should be turned off in production environments concerned about security.
60 It's great for debugging, though!
61  
62 =head3 raw
63  
64 Setting this to a true value will cause Data::Serializer to call the "raw" version
65 of the regular serialize function (raw_serialize). The effect is the same as just using
66 the underlying Serializer directly.
67  
68 =head2 OVERLOADED METHODS
69  
70 =head3 process
71  
72 Serializes $c->stash to $c->response->output. If you pass "astext=1" as a
73 param, and the $c->config->{'serializer'}->{'astext'} option is true, then
74 it will return the output of the stash via Data::Dumper.
75  
76 =cut
77  
78 0 ▶sub process {
Subroutine process: not called CC=8 SCAR=42.8
Pod documented
79 0 my ($self, $c) = @_;
80 0 my $serializer = $self->_serializer;
81 0 ▶ unless ($serializer) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($serializer) 0 0 0
82 0 my %defaults = (
83 serializer => 'Data::Dumper',
84 digester => 'SHA1',
85 cipher => 'Blowfish',
86 secret => undef,
87 portable => '1',
88 compress => '0',
89 serializer_token => '1',
90 options => {},
91 );
92 0 ▶ while (my ($k, $v) = each %defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %defaults) 0 0 0
93 0 ▶ if (!exists($c->config->{'serializer'}->{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $c->config->{"serializer"}{$k}) 0 0 0
94 0 $c->config->{'serializer'}->{$k} = $v;
95 }
96 }
97 0 my $astext = 0;
98 0 ▶ if (exists($c->config->{'serializer'}->{'astext'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c->config->{"serializer"}{"astext"}) 0 0 0
99 0 $astext = $c->config->{'serializer'}->{'astext'};
100 0 delete($c->config->{'serializer'}->{'astext'});
101 }
102 0 $serializer = Data::Serializer->new(%{ $c->config->{'serializer'} },);
103 0 $self->_serializer($serializer);
104 0 $c->config->{'serializer'}->{'astext'} = $astext;
105 }
106 0 $c->response->headers->content_type('text/plain');
107 0 ▶ if ($c->req->param("astext") && $c->config->{'serializer'}->{'astext'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->param('astext') and $c->config->{'serializer'}{'astext'}) { } 0 0 0
Condition0%0/3
$c->req->param("astext") and $c->config->{"serializer"}{"astext"}
!ll&&!rl&&r
000
MC/DC0%0/2
$c->req->param("astext") and $c->config->{"serializer"}{"astext"}
$c->req->param("astext") $c->config->{"serializer"}{"astext"}
Truth table0%0/3
A and B
A= $c->req->param("astext")B= $c->config->{"serializer"}{"astext"}
ABresult
0X0
100
111
108 0 $c->response->output(Data::Dumper->Dump([ $c->stash ]));
109 } else {
110 0 ▶ if ($c->config->{'serializer'}->{'raw'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->config->{'serializer'}{'raw'}) { } 0 0 0
111 0 $c->response->output($serializer->raw_serialize($c->stash));
112 } else {
113 0 $c->response->output($serializer->serialize($c->stash));
114 }
115 }
116 0 return 1;
117 }
118  
119 =head1 SEE ALSO
120  
121 L<Catalyst>, L<Data::Serializer>
122  
123 =head1 AUTHOR
124  
125 Adam Jacob, C<adam@stalecoffee.org>
126  
127 =head1 COPYRIGHT
128  
129 This program is free software, you can redistribute it and/or modify it under
130 the same terms as Perl itself.
131  
132 =cut
133  
134 1 1;
↑ Summary