File Coverage

blib/lib/AWS/S3/ResponseParser.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1              
2             package AWS::S3::ResponseParser;
3              
4 6     6   22 use Moose;
  6         6  
  6         41  
5 6     6   28796 use XML::LibXML;
  0            
  0            
6             use XML::LibXML::XPathContext;
7              
8             has 'expect_nothing' => (
9             is => 'ro',
10             isa => 'Bool',
11             required => 1,
12             default => 0,
13             trigger => sub {
14             my ( $self, $expect_nothing) = @_;
15             if ( $expect_nothing ) {
16             my $code = $self->response->code;
17             if ( $code =~ m{^2\d\d} && !$self->response->content ) {
18             return; # not sure what jdrago wanted this to do originally
19             }
20             else {
21             if ( $self->_parse_errors() ) {
22             # die $self->friendly_error();
23             }
24             else {
25             return;
26             }
27             }
28             }
29             }
30             );
31              
32             has 'response' => (
33             is => 'ro',
34             isa => 'HTTP::Response',
35             required => 1,
36             );
37              
38             has 'type' => (
39             is => 'ro',
40             isa => 'Str',
41             required => 1,
42             );
43              
44             has 'libxml' => (
45             is => 'ro',
46             isa => 'XML::LibXML',
47             required => 1,
48             default => sub { return XML::LibXML->new() },
49             );
50              
51             has 'error_code' => (
52             is => 'rw',
53             isa => 'Str',
54             required => 0,
55             );
56              
57             has 'error_message' => (
58             is => 'rw',
59             isa => 'Str',
60             required => 0,
61             );
62              
63             has 'xpc' => (
64             is => 'ro',
65             isa => 'XML::LibXML::XPathContext',
66             required => 0,
67             lazy => 1,
68             clearer => '_clear_xpc',
69             default => sub {
70             my $self = shift;
71              
72             my $src = $self->response->content;
73             return unless $src =~ m/^[[:space:]]*</s;
74             my $doc = $self->libxml->parse_string( $src );
75              
76             my $xpc = XML::LibXML::XPathContext->new( $doc );
77             $xpc->registerNs( 's3', 'http://s3.amazonaws.com/doc/2006-03-01/' );
78              
79             return $xpc;
80             }
81             );
82              
83             has 'friendly_error' => (
84             is => 'ro',
85             isa => 'Maybe[Str]',
86             lazy => 1,
87             required => 0,
88             default => sub {
89             my $s = shift;
90              
91             return unless $s->error_code || $s->error_message;
92             $s->type . " call had errors: [" . $s->error_code . "] " . $s->error_message;
93             }
94             );
95              
96             sub _parse_errors {
97             my $self = shift;
98              
99             my $src = $self->response->content;
100              
101             # Do not try to parse non-xml:
102             unless ( $src =~ m/^[[:space:]]*</s ) {
103             ( my $code = $src ) =~ s/^[[:space:]]*\([0-9]*\).*$/$1/s;
104             $self->error_code( $code );
105             $self->error_message( $src );
106             return 1;
107             } # end unless()
108              
109             ## Originally at this point the re-setting of xpc would happen
110             ## Does not seem to be needed but it may be a problem area
111             ## Feel free to delete - Evan Carroll 2012/06/14
112             #### $s->_clear_xpc;
113              
114             if ( $self->xpc->findnodes( "//Error" ) ) {
115             $self->error_code( $self->xpc->findvalue( "//Error/Code" ) );
116             $self->error_message( $self->xpc->findvalue( "//Error/Message" ) );
117             return 1;
118             }
119              
120             return 0;
121             }
122              
123             __PACKAGE__->meta->make_immutable;