blib/lib/Dallycot/TextResolver/Request.pm

filter: Statementstmt 41.3% 38 / 92 Branchbran 10.0% 2 / 20 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 64.2% 9 / 14 Podpod 0.0% 0 / 1 totaltotal 39.3% 52 / 132 CC 12 SCAR 38.1
CC
12
Cov
37%
__ANON__
110.0
_stringify_acceptable_type
3.0
run
2.0
CRAP 45.0
« blib/lib/Dallycot/Resolver/Request.pm ↑ Summary blib/lib/Dallycot/Library/Core/DateTime.pm »
1 package Dallycot::TextResolver::Request;
2 10 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Manages request cycle for text resources
5  
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use experimental qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 ▶use Promises qw(deferred);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use URI::WithBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Dallycot::Value::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 has ua => (
19 is => 'ro',
20 required => 1
21 );
22  
23 10 has url => (
24 is => 'ro',
25 isa => 'Str',
26 required => 1
27 );
28  
29 10 has redirects => (
30 is => 'ro',
31 isa => 'Int',
32 default => 10
33 );
34  
35 10 has canonical_url => (
36 is => 'ro',
37 isa => 'Str'
38 );
39  
40 10 my %acceptable_types = (
41 'application/xml' => 1.0,
42 'application/xhtml+xml' => 1.0,
43 'text/html' => 0.9,
44 'text/plain' => 0.8
45 );
46  
47 10 my $accept_headers = join( ",", map { _stringify_acceptable_type($_) } keys %acceptable_types );
48  
49 40 ▶sub _stringify_acceptable_type {
Subroutine _stringify_acceptable_type: called CC=3 SCAR=11.0
50 40 my ($type) = @_;
51  
52 40 ▶ if ( exists $acceptable_types{$type} && $acceptable_types{$type} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $acceptable_types{$type} and $acceptable_types{$type} < 1) { } 20 20 40
Condition66%2/3
exists $acceptable_types{$type} and $acceptable_types{$type} < 1
!ll&&!rl&&r
02020
MC/DC50%1/2
exists $acceptable_types{$type} and $acceptable_types{$type} < 1
exists $acceptable_types{$type} $acceptable_types{$type} < 1
Truth table66%2/3
A and B
A= exists $acceptable_types{$type}B= $acceptable_types{$type} < 1
ABresult
0X0
100
111
53 20 return $type . ";q=" . $acceptable_types{$type};
54 }
55 else {
56 20 return $type;
57 }
58 }
59  
60 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
61 0 my ($self) = @_;
62  
63 0 my $deferred = deferred;
64 0 my $url = $self->url;
65 0 my $request = $self->ua->build_tx( GET => $url );
66 0 $request->req->headers->accept($accept_headers);
67 0 my $base_uri = URI::WithBase->new( $self->url )->base;
68  
69 $self->ua->start(
70 $request,
71 0 ▶ sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
72 0 my ( $ua, $response ) = @_;
73 0 ▶ if ( $response->success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->success) { } 0 0 0
74 0 my $res = $response->res;
75 0 ▶ if ( $res->code == 200 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->code == 200) { } 0 0 0
elsif ($res->code == 303) { } 0 0 0
76  
77 # regular response - we can parse this and work with it
78 # we'll load a handler based on the content type
79 0 my $content_type_header = $res->headers->content_type;
80 0 my @bits = split( /;/, $content_type_header );
81 0 my $content_type = shift @bits;
82 0 my $object_type;
83 0 given ($content_type) {
84 0 when ('application/xml') {
85 0 $object_type = 'Dallycot::Value::XML';
86 }
87 0 when ('application/xhtml+xml') {
88 0 $object_type = 'Dallycot::Value::HTML';
89 }
90 0 when ('text/html') {
91 0 $object_type = 'Dallycot::Value::HTML';
92 }
93 0 when ('text/plain') {
94 0 $object_type = 'Dallycot::Value::String';
95 }
96 0 default {
97 0 $deferred->reject("Unrecognized content type ($content_type)");
98 0 return;
99 }
100 }
101 0 $deferred->resolve( Dallycot::Value::String->new( $res->content->build_body ) );
102 }
103 elsif ( $res->code == 303 ) { # See Other
104 # look for an 'Alternatives' header
105 0 my $new_uri;
106 0 ▶ if ( 0 >= $self->redirects ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 >= $self->redirects) 0 0 0
107 0 $deferred->reject("Unable to fetch $url: too many redirects");
108 0 return;
109 }
110  
111 0 ▶ if ( $res->headers->header('alternatives') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->headers->header('alternatives')) { } 0 0 0
elsif ($res->headers->location) { } 0 0 0
112 0 my $alts = $res->headers->header('alternatives');
113 0 my @options = $alts =~ m{
114 (:?{"(.+?)"\s+([0-9.]+)\s+{type (.+?)}},?\s*)*
115 }xm;
116 0 my %types;
117 0 ▶ while ( my ( $path, $val, $type ) = splice( @options, 0, 3 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@options, 0, 3)) 0 0 0
118 0 $types{$type} = [ $val, $path ];
119 }
120 0 my @sorted_types = grep { $acceptable_types{$_} }
121 0 sort { $types{$a}->[0] <=> $types{$b}->[0] } keys %types;
122  
123 # we'll take the first one we get
124 0 ▶ if (@sorted_types) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_types) 0 0 0
125 0 $new_uri = $types{ $sorted_types[0] }->[1];
126 }
127 }
128 elsif ( $res->headers->location ) {
129 0 $new_uri = $res->headers->location;
130 }
131 0 ▶ if ($new_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_uri) { } 0 0 0
132 $self->new(
133 ua => $ua,
134 url => $new_uri,
135 redirects => $self->redirects - 1,
136 canonical_url => $self->canonical_url
137 0 ▶ )->run->done( sub { $deferred->resolve(@_); }, sub { $deferred->reject(@_); } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 }
139 else {
140 # we give up... nothing to see here
141 0 $deferred->reject("Unable to fetch $url: redirect with no suitable location");
142 }
143 }
144 else {
145 0 $deferred->reject( "Unable to fetch $url: " . $res->status_line );
146 }
147 }
148 else {
149 0 my $err = $response->error;
150 0 $deferred->reject("Unable to fetch $url: $err->{message}");
151 }
152 },
153 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 $deferred->reject(@_);
155 }
156 0 );
157  
158 0 return $deferred->promise;
159 }
160  
161 10 __PACKAGE__ -> meta -> make_immutable;
162  
163 10 1;
« blib/lib/Dallycot/Resolver/Request.pm ↑ Summary blib/lib/Dallycot/Library/Core/DateTime.pm »