File Coverage

blib/lib/WebService/Google/Closure/Response.pm
Criterion Covered Total %
statement 27 33 81.8
branch 4 4 100.0
condition n/a
subroutine 7 8 87.5
pod n/a
total 38 45 84.4


line stmt bran cond sub pod time code
1             package WebService::Google::Closure::Response;
2              
3 3     3   19 use Moose;
  3         5  
  3         25  
4 3     3   21915 use MooseX::Types::Moose qw( ArrayRef Str Int );
  3         7  
  3         39  
5 3     3   18435 use JSON;
  3         8  
  3         33  
6              
7 3     3   471 use WebService::Google::Closure::Types qw( ArrayRefOfWarnings ArrayRefOfErrors Stats );
  3         6  
  3         27  
8              
9             has format => (
10             is => 'ro',
11             isa => Str,
12             trigger => sub { my $self = shift; die "Bad format - only json" unless $self->format eq 'json' },
13             );
14              
15             has content => (
16             is => 'ro',
17             isa => Str,
18             trigger => \&_set_content,
19             );
20              
21             has code => (
22             is => 'ro',
23             isa => Str,
24             predicate => 'has_code',
25             writer => '_set_compiledCode',
26             );
27              
28             has warnings => (
29             is => 'ro',
30             isa => ArrayRefOfWarnings,
31             init_arg => undef,
32             predicate => 'has_warnings',
33             writer => '_set_warnings',
34             coerce => 1,
35             );
36              
37             has errors => (
38             is => 'ro',
39             isa => ArrayRefOfErrors,
40             init_arg => undef,
41             predicate => 'has_errors',
42             writer => '_set_errors',
43             coerce => 1,
44             );
45              
46             has stats => (
47             is => 'ro',
48             isa => Stats,
49             init_arg => undef,
50             predicate => 'has_stats',
51             writer => '_set_statistics',
52             coerce => 1,
53             );
54              
55             has is_success => (
56             is => 'ro',
57             lazy_build => 1,
58             );
59              
60             sub _build_is_success {
61 5     5   12 my $self = shift;
62 5 100       323 if ( $self->has_errors ) {
63 2         125 return 0;
64             }
65 3         151 return 1;
66             }
67              
68             sub _set_content {
69 6     6   17 my $self = shift;
70              
71 6         94 my $json = JSON->new();
72 6         349 my $content = $json->decode( $self->content );
73 6         18 foreach my $key ( keys %{ $content } ) {
  6         30  
74 15 100       158 next unless $content->{ $key };
75 13         34 my $set = '_set_' . $key;
76 13         809 $self->$set( $content->{ $key } );
77             }
78             }
79              
80             # bail out on server errors
81             sub _set_serverErrors {
82 0     0     my ($self, $err) = @_;
83              
84 0           my $text = '';
85 0           foreach my $e ( @{ $err } ) {
  0            
86 0           $text .= $e->{ error };
87             }
88 0           die $text;
89             }
90              
91 3     3   11796 no Moose;
  3         7  
  3         23  
92             __PACKAGE__->meta->make_immutable;
93             1;
94              
95             __END__
96              
97             =head1 NAME
98              
99             WebService::Google::Closure::Response - Response object from compiling Javascript with Closure
100              
101             =head1 SYNOPSIS
102              
103             my $res = WebService::Google::Closure->new(
104             js_code => $js_code,
105             )->compile;
106              
107             if ( $res->is_success ) {
108             print "Shenanigans ahead:\n";
109             print $res->code;
110             }
111             else {
112             foreach my $err ( @{ $res->errors } ) {
113             $txt .= sprintf("%s line (%d) char [%d].\n",
114             $err->text,
115             $err->lineno,
116             $err->charno);
117             }
118             die $txt;
119             }
120              
121             =head1 METHODS
122              
123             =head2 $response->is_success
124              
125             Boolean saying if the compilation was successful or not.
126              
127             =head2 $response->code
128              
129             Returns a string with the compiled javascript code.
130              
131             =head2 $response->has_warnings
132              
133             Boolean saying if the compilation generated any warnings
134              
135             =head2 $response->warnings
136              
137             An array reference of L<WebService::Google::Type::Warning> objects.
138              
139             =head2 $response->has_errors
140              
141             Boolean saying if the compilation generated any errors
142              
143             =head2 $response->errors
144              
145             An array reference of L<WebService::Google::Type::Error> objects.
146              
147             =head2 $response->has_stats
148              
149             Boolean saying if statistics are available
150              
151             =head2 $response->stats
152              
153             A L<WebService::Google::Type::Stats> object.
154              
155             =head1 LICENSE AND COPYRIGHT
156              
157             Copyright 2010-2011 Magnus Erixzon.
158              
159             This program is free software; you can redistribute it and/or modify it
160             under the terms of either: the GNU General Public License as published
161             by the Free Software Foundation; or the Artistic License.
162              
163             See http://dev.perl.org/licenses/ for more information.
164              
165             =cut