Data/Riak/HTTP/Response.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 85 CC 6 SCAR 29.5
CC
6
Cov
0%
_create_result
42.0
create_results
2.0
BEGIN
2.0
CRAP 19.1
1 package Data::Riak::HTTP::Response;
2 {
3 $Data::Riak::HTTP::Response::VERSION = '2.0';
4 }
5  
6 use strict;
7 use warnings;
8  
9 use Moose;
10 use URI;
11 use HTTP::Headers::ActionPack 0.05;
12  
13 use overload '""' => 'as_string', fallback => 1;
14  
15 my $_deconstruct_parts;
16  
17 has 'parts' => (
18 is => 'ro',
19 isa => 'ArrayRef[HTTP::Message]',
20 lazy => 1,
21 default => sub {
22 my $self = shift;
23 my @parts = $_deconstruct_parts->( $self->http_response );
24 return \@parts;
25 }
26 );
27  
28 has 'http_response' => (
29 is => 'ro',
30 isa => 'HTTP::Response',
31 required => 1,
32 handles => {
33 code => 'code',
34 status_code => 'code',
35 message => 'content',
36 value => 'content',
37 is_success => 'is_success',
38 is_error => 'is_error',
39 as_string => 'as_string',
40 header => 'header',
41 headers => 'headers'
42 }
43 );
44  
45 $_deconstruct_parts = sub {
46 my $message = shift;
47 return () unless $message->content;
48 my @parts = $message->parts;
49 return $message unless @parts;
50 return map { $_deconstruct_parts->( $_ ) } @parts;
51 };
52  
53 with 'Data::Riak::Transport::Response';
54  
55 sub create_results {
56 my ($self, $riak, $request) = @_;
57  
58 return map {
59 $self->_create_result($riak, $request, $_)
60 } @{ $self->parts };
61 }
62  
63 my %header_values = (
64 etag => 'etag',
65 content_type => 'content-type',
66 vector_clock => 'x-riak-vclock',
67 last_modified => 'last_modified',
68 );
69  
70 sub _create_result {
71 my ($self, $riak, $request, $http_message) = @_;
72  
73 HTTP::Headers::ActionPack->new->inflate( $http_message->headers );
74  
75 my %result_args = (
76 riak => $riak,
77 status_code => $self->http_response->code, # FIXME: http specific
78 value => $http_message->content,
79 (map {
80 my $v = $http_message->header($header_values{$_});
81 defined $v ? ($_ => $v) : ();
82 } keys %header_values),
83 );
84  
85 if ($request->result_does('Data::Riak::Result::WithLocation')) {
86 $result_args{location} = $http_message->can('request')
87 ? $http_message->request->uri
88 : (URI->new( $http_message->header('location') )
89 || die 'Cannot determine location from ' . $http_message);
90 }
91  
92 if ($request->result_does('Data::Riak::Result::WithLinks')) {
93 my $links = $http_message->header('link');
94 $result_args{links} = [map {
95 Data::Riak::Link->from_link_header($_)
96 } $links ? $links->iterable : ()],
97 }
98  
99 return $request->new_result(\%result_args);
100 }
101  
102 __PACKAGE__->meta->make_immutable;
103 no Moose;
104  
105 1;
106  
107 __END__