blib/lib/Dallycot/Resolver/Request.pm

filter: Statementstmt 23.9% 23 / 96 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 35.7% 5 / 14 Podpod 0.0% 0 / 4 totaltotal 15.6% 28 / 179 CC 23 SCAR 40.8
CC
23
Cov
16%
handle_response_content
132.0
handle_redirect
56.0
handle_response
42.0
CRAP 59.0
« blib/lib/Dallycot/CLI.pm ↑ Summary blib/lib/Dallycot/TextResolver/Request.pm »
1 package Dallycot::Resolver::Request;
2 10 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Manages request cycle for data resources
5  
6 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Promises qw(deferred);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use URI::WithBase;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Encode qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 has ua => (
14 is => 'ro',
15 required => 1
16 );
17  
18 10 has url => (
19 is => 'ro',
20 isa => 'Str',
21 required => 1
22 );
23  
24 has base_url => (
25 is => 'ro',
26 isa => 'Str',
27 lazy => 1,
28 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
29 0 my ($self) = @_;
30  
31 0 ▶ return URI::WithBase->new( $self->url )->base || $self->url;
Condition0%0/3
'URI::WithBase'->new($self->url)->base || $self->url
l!l&&r!l&&!r
000
MC/DC0%0/2
'URI::WithBase'->new($self->url)->base || $self->url
'URI::WithBase'->new($self->url)->base $self->url
Truth table0%0/3
A || B
A= 'URI::WithBase'->new($self->url)->baseB= $self->url
ABresult
1X1
011
000
32 }
33 10 );
34  
35 10 has redirects => (
36 is => 'ro',
37 isa => 'Int',
38 default => 10
39 );
40  
41 10 has canonical_url => (
42 is => 'ro',
43 isa => 'Str'
44 );
45  
46 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
47 0 my ($self) = @_;
48  
49 0 my $deferred = deferred;
50 0 my $request = $self->ua->build_tx( GET => $self->url );
51 0 $request->req->headers->accept( RDF::Trine::Parser->default_accept_header );
52  
53 $self->ua->start(
54 $request,
55 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 my ( $ua, $response ) = @_;
57 0 $self->handle_response( $ua, $response, $deferred );
58 },
59 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 $deferred->reject(@_);
61 }
62 0 );
63 0 return $deferred->promise;
64 }
65  
66 0 ▶sub handle_response {
Subroutine handle_response: not called CC=6 SCAR=37.4
Pod undocumented
67 0 my ( $self, $ua, $response, $deferred ) = @_;
68  
69 0 ▶ if ( $response->success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->success) { } 0 0 0
70 0 my $res = $response->res;
71 0 ▶ if ( $res->code == 200 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->code == 200) { } 0 0 0
elsif ($res->code == 303 or $res->code == 302 or $res->code == 301) { } 0 0 0
72 0 $self->handle_response_content( $ua, $res, $deferred );
73 }
74 ▶ elsif ( $res->code == 303 || $res->code == 302 || $res->code == 301 ) { # See Other
Condition0%0/6
$res->code == 303 or $res->code == 302 or $res->code == 301
l!l&&r!l&&!r
000
$res->code == 303 or $res->code == 302
l!l&&r!l&&!r
000
MC/DC0%0/3
$res->code == 303 or $res->code == 302 or $res->code == 301
$res->code == 303 $res->code == 302 $res->code == 301
Truth table0%0/4
A or B or C
A= $res->code == 303B= $res->code == 302C= $res->code == 301
ABCresult
1XX1
01X1
0011
0000
75 # look for an 'Alternatives' header
76 0 $self->handle_redirect( $ua, $res, $deferred );
77 }
78 else {
79 0 $deferred->reject( "Unable to fetch $self->url: " . $res->message );
80 }
81 }
82 else {
83 0 my $err = $response->error;
84 0 $deferred->reject("Unable to fetch $self->url: $err->{message}");
85 }
86 0 return;
87 }
88  
89 0 ▶sub handle_response_content {
Subroutine handle_response_content: not called CC=11 SCAR=48.8
Pod undocumented
90 0 my ( $self, $ua, $res, $deferred ) = @_;
91  
92 # regular response - we can parse this and work with it
93 # we'll load a handler based on the content type
94 0 my $content_type_header = $res->headers->content_type;
95 0 my @bits = split( /;/, $content_type_header );
96 0 my $content_type = shift @bits;
97 0 my %content_params = map { split( /=/, $_, 2 ) } @bits;
98 0 ▶ $content_params{'charset'} //= 'ISO-8859-1';
Condition0%0/2
$content_params{"charset"} //= "ISO-8859-1"
l!l
00
MC/DC0%0/1
$content_params{"charset"} //= "ISO-8859-1"
$content_params{"charset"}
Truth table0%0/2
A
A= $content_params{"charset"}
Aresult
11
00
99 0 my $body = $res->content->build_body;
100  
101 #$body = encode('UTF-8', decode(lc($content_params{'charset'}), $body));
102  
103 0 my $model;
104 0 my $parser_class = eval { RDF::Trine::Parser->parser_by_media_type($content_type) };
105  
106 0 ▶ if ( $@ || !defined($parser_class) || $content_type eq 'text/html' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ or not defined $parser_class or $content_type eq 'text/html') { } 0 0 0
elsif ($parser_class) { } 0 0 0
Condition0%0/6
$@ or not defined $parser_class or $content_type eq "text/html"
l!l&&r!l&&!r
000
$@ or not defined $parser_class
l!l&&r!l&&!r
000
MC/DC0%0/3
$@ or not defined $parser_class or $content_type eq "text/html"
$@ not defined $parser_class $content_type eq "text/html"
Truth table0%0/4
A or not(B) or C
A= $@B= not defined $parser_classC= $content_type eq "text/html"
ABCresult
1XX1
01X1
0011
0000
107 0 ▶ if ( $content_type eq 'text/html' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type eq 'text/html') { } 0 0 0
108 0 require RDF::RDFa::Parser;
109  
110 # my $options = RDF::RDFa::Parser::Config->new('xhtml', '1.1');
111 0 my $rdfa = RDF::RDFa::Parser->new( $body, $self->url, RDF::RDFa::Parser::Config->tagsoup );
112 0 $model = $rdfa->graph;
113 0 ▶ $deferred->resolve(
Condition0%0/3
$self->canonical_url || $self->url
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->canonical_url || $self->url
$self->canonical_url $self->url
Truth table0%0/3
A || B
A= $self->canonical_urlB= $self->url
ABresult
1X1
011
000
114 bless [
115 $self->base_url, RDF::Trine::Node::Resource->new( $self->canonical_url || $self->url ),
116 $model
117 ] => 'Dallycot::Value::TripleStore'
118 );
119 }
120 else {
121 0 ▶ $deferred->reject( $@ || ( 'Unable to process content type "' . $content_type . '"' ) );
Condition0%0/2
$@ || 'Unable to process content type "' . $content_type . '"'
l!l
00
MC/DC0%0/1
$@ || 'Unable to process content type "' . $content_type . '"'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
122 }
123 }
124 elsif ($parser_class) {
125 0 my $parser = $parser_class->new();
126 0 my $store = RDF::Trine::Store::Memory->new();
127 0 $model = RDF::Trine::Model->new($store);
128 0 my $worked = eval {
129 0 $parser->parse_into_model( $self->base_url, $body, $model );
130 0 1;
131 };
132 0 ▶ if ($worked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($worked) { } 0 0 0
133 0 ▶ $deferred->resolve(
Condition0%0/3
$self->canonical_url || $self->url
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->canonical_url || $self->url
$self->canonical_url $self->url
Truth table0%0/3
A || B
A= $self->canonical_urlB= $self->url
ABresult
1X1
011
000
134 bless [
135 $self->base_url, RDF::Trine::Node::Resource->new( $self->canonical_url || $self->url ),
136 $model
137 ] => 'Dallycot::Value::TripleStore'
138 );
139 }
140 else {
141 0 $deferred->reject( 'Unable to process content type "' . $content_type . '": ' . $@ );
142 }
143 }
144 else {
145 0 $deferred->reject(
146 "Unable to parse content from $self->url: no parser for " . $res->headers->content_type );
147 }
148 0 return;
149 }
150  
151 0 ▶sub handle_redirect {
Subroutine handle_redirect: not called CC=7 SCAR=40.3
Pod undocumented
152 0 my ( $self, $ua, $res, $deferred ) = @_;
153  
154 0 my $new_uri;
155 0 ▶ if ( 0 >= $self->redirects ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 >= $self->redirects) 0 0 0
156 0 $deferred->reject("Unable to fetch $self->url: too many redirects");
157 0 return;
158 }
159  
160 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
161 0 my $alts = $res->headers->header('alternatives');
162 0 my @options = $alts =~ m{
163 (:?{"(.+?)"\s+([0-9.]+)\s+{type (.+?)}},?\s*)*
164 }xm;
165 0 my %types;
166 0 ▶ while ( my ( $path, $val, $type ) = splice( @options, 0, 3 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@options, 0, 3)) 0 0 0
167 0 $types{$type} = [ $val, $path ];
168 }
169 0 my @sorted_types = grep { RDF::Trine::Parser->parser_by_media_type($_) }
170 0 sort { $types{$a}->[0] <=> $types{$b}->[0] } keys %types;
171  
172 # we'll take the first one we get
173 0 ▶ if (@sorted_types) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sorted_types) 0 0 0
174 0 $new_uri = $types{ $sorted_types[0] }->[1];
175 }
176 }
177 elsif ( $res->headers->location ) {
178 0 $new_uri = $res->headers->location;
179 }
180 0 ▶ if ($new_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_uri) { } 0 0 0
181 $self->new(
182 ua => $ua,
183 url => $new_uri,
184 redirects => $self->redirects - 1,
185 canonical_url => $self->canonical_url
186 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
187 }
188 else {
189 # we give up... nothing to see here
190 0 $deferred->reject("Unable to fetch $self->url: redirect with no suitable location");
191 }
192 0 return;
193 }
194  
195 10 __PACKAGE__ -> meta -> make_immutable;
196  
197 10 1;
« blib/lib/Dallycot/CLI.pm ↑ Summary blib/lib/Dallycot/TextResolver/Request.pm »