lib/CPAN/Testers/WWW/Reports/Query/Report.pm

filter: Statementstmt 52.2% 35 / 67 Branchbran 6.2% 1 / 16 Conditioncond 10.0% 3 / 30 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 35.4% 50 / 141 SCAR 49.8
CC
21
Cov
35%
report
272.0
_parse
4.9
new
4.4
CRAP 144.9
↑ Summary blib/lib/CPAN/Testers/WWW/Reports/Query/Report.pm »
1 package CPAN::Testers::WWW::Reports::Query::Report;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '0.04';
7
8 #----------------------------------------------------------------------------
9  
10 =head1 NAME
11  
12 CPAN::Testers::WWW::Reports::Query::Report - Retrieve CPAN Testers report direct from the CPAN Testers website.
13  
14 =head1 DESCRIPTION
15
16 This module queries the CPAN Testers website and retrieves a specific report.
17
18 =head1 SYNOPSIS
19  
20 # default options
21 my %options = (
22 as_json => 0, # the default
23 as_hash => 0, # the default
24 host => 'http://cpantesters.org' # the default
25 );
26  
27 # establish the object
28 my $query = CPAN::Testers::WWW::Reports::Query::Report->new( %options );
29  
30 The default is to return a Metabase::Fact, as a CPAN::Testers::Report object.
31 If you wish to manipulate this differently, use the as_json or as_hash to
32 return more simplified forms.
33  
34 # get by id
35 my $result = $query->report( report => 40000000 );
36  
37 # get by GUID
38 $result = $query->report( report => '0b3fd09a-7e50-11e3-9609-5744ee331862' );
39  
40 # force return as JSON
41 my $result = $query->report( report => 40000000, as_json => 1 );
42  
43 # force return as a hash
44 my $result = $query->report( report => 40000000, as_hash => 1 );
45  
46 The as_json and as_hash options here will override the general options
47 supplied in the object constructor. If you've specified as_json or as_hash in
48 the object constructor, to override simply set 'as_json => 0' and/or
49 'as_hash => 0' in the method call.
50  
51 # get the last error
52 my $error = $query->error;
53  
54 If the result is returned as undef, either no report was found or the JSON
55 return is malformed. This could be due to network connection, or corrupt data
56 in the report. If the latter please notify the CPAN Testers discussion list,
57 so we can investigate and correct as appropriate.
58  
59 =cut
60
61 #----------------------------------------------------------------------------
62 # Library Modules
63  
64 6 ▶use CPAN::Testers::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 6 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 6 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 #----------------------------------------------------------------------------
69 # Variables
70  
71 6 my $HOST = 'http://www.cpantesters.org';
72 6 my $PATH = '%s/cpan/report/%s?json=1';
73  
74 6 my $mech = WWW::Mechanize->new();
75 6 $mech->agent_alias( 'Linux Mozilla' );
76  
77 # -------------------------------------
78 # Program
79  
80 sub new {
81 1 ▶ my($class, %hash) = @_;
Subroutine new: called CC=4 SCAR=14.8
Pod documented
82 1 my $self = {};
83
84 1 ▶ $self->{as_json} = $hash{as_json} || 0;
Condition50%1/2
$hash{'as_json'} || 0
l!l
01
MC/DC0%0/1
$hash{'as_json'} || 0
$hash{'as_json'}
Truth table50%1/2
A
A= $hash{'as_json'}
Aresult
11
00
85 1 ▶ $self->{as_hash} = $hash{as_hash} || 0;
Condition50%1/2
$hash{'as_hash'} || 0
l!l
01
MC/DC0%0/1
$hash{'as_hash'} || 0
$hash{'as_hash'}
Truth table50%1/2
A
A= $hash{'as_hash'}
Aresult
11
00
86 1 ▶ $self->{host} = $hash{host} || $HOST;
Condition33%1/3
$hash{'host'} || $HOST
l!l&&r!l&&!r
010
MC/DC0%0/2
$hash{'host'} || $HOST
$hash{'host'} $HOST
Truth table33%1/3
A || B
A= $hash{'host'}B= $HOST
ABresult
1X1
011
000
87  
88 1 bless $self, $class;
89  
90 1 return $self;
91 }
92  
93 sub report {
94 0 ▶ my $self = shift;
Subroutine report: not called CC=16 SCAR=56.1
Pod documented
95 0 my %hash = @_;
96
97 0 $self->{error} = '';
98  
99 0 my $url = sprintf $PATH, $self->{host}, $hash{report};
100 0 eval { $mech->get( $url ); };
101 0 ▶ if($@ || !$mech->success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $mech->success) 0 0 0
Condition0%0/3
$@ or not $mech->success
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $mech->success
$@ not $mech->success
Truth table0%0/3
A or not(B)
A= $@B= not $mech->success
ABresult
1X1
011
000
102 0 $self->{error} = "No response from server: $@";
103 0 return;
104 }
105  
106 0 $self->{content} = $mech->content;
107  
108 0 my $data;
109 0 eval {
110 0 $data = decode_json($self->{content});
111 };
112  
113 0 ▶ if($@ || !$data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $data) 0 0 0
Condition0%0/3
$@ or not $data
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $data
$@ not $data
Truth table0%0/3
A or not(B)
A= $@B= not $data
ABresult
1X1
011
000
114 0 ▶ $self->{error} = "JSON decoding error: " . ($@ || 'no data returned');
Condition0%0/2
$@ || 'no data returned'
l!l
00
MC/DC0%0/1
$@ || 'no data returned'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
115 0 return;
116 }
117  
118 0 ▶ unless($data->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"success"}) 0 0 0
119 0 $self->{error} = "no report found";
120 0 return;
121 }
122  
123 0 ▶ my $as = ($hash{as_json} || (!defined $hash{as_json} && $self->{as_json})) ? 'json' : '';
Branch: 1 branch
BranchTrueFalseTotal
$hash{'as_json'} || !defined($hash{'as_json'}) && $self->{'as_json'} ? : 0 0 0
Condition0%0/6
$hash{"as_json"} or not defined $hash{"as_json"} and $self->{"as_json"}
l!l&&r!l&&!r
000
not defined $hash{"as_json"} and $self->{"as_json"}
!ll&&!rl&&r
000
MC/DC0%0/3
$hash{"as_json"} or not defined $hash{"as_json"} and $self->{"as_json"}
$hash{"as_json"} not defined $hash{"as_json"} $self->{"as_json"}
Truth table0%0/4
A or not(B) and C
A= $hash{"as_json"}B= not defined $hash{"as_json"}C= $self->{"as_json"}
ABCresult
1XX1
0111
00X0
0100
124 0 ▶ $as ||= ($hash{as_hash} || (!defined $hash{as_hash} && $self->{as_hash})) ? 'hash' : 'fact';
Branch: 1 branch
BranchTrueFalseTotal
$hash{'as_hash'} || !defined($hash{'as_hash'}) && $self->{'as_hash'} ? : 0 0 0
Condition0%0/9
$as ||= $hash{"as_hash"} || !defined($hash{"as_hash"}) && $self->{"as_hash"} ? "hash" : "fact"
l!l&&r!l&&!r
000
$hash{"as_hash"} or not defined $hash{"as_hash"} and $self->{"as_hash"}
l!l&&r!l&&!r
000
not defined $hash{"as_hash"} and $self->{"as_hash"}
!ll&&!rl&&r
000
MC/DC0%0/4
$as ||= $hash{"as_hash"} || !defined($hash{"as_hash"}) && $self->{"as_hash"} ? "hash" : "fact"
$as $hash{"as_hash"} not defined $hash{"as_hash"} $self->{"as_hash"}
Truth table0%0/5
A ||= B or not(C) and D
A= $asB= $hash{"as_hash"}C= not defined $hash{"as_hash"}D= $self->{"as_hash"}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
125  
126 0 ▶ return encode_json($data->{result}) if($as eq 'json');
Branch: 1 branch
BranchTrueFalseTotal
if $as eq "json" 0 0 0
127 0 ▶ return $data->{result} if($as eq 'hash');
Branch: 1 branch
BranchTrueFalseTotal
if $as eq "hash" 0 0 0
128  
129 0 return $self->_parse($data->{result});
130 }
131  
132 sub _parse {
133 1 ▶ my ($self,$data) = @_;
Subroutine _parse: called CC=3 SCAR=16.0
134 1 my $hash;
135  
136 1 ▶ if(!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1 0 1
137 1 $self->{error} = 'no data returned';
138 1 return;
139 }
140  
141 0 for my $content (@{ $data->{content} }) {
142 0 $content->{content} = encode_json($content->{content});
143 }
144 0 $data->{content} = encode_json($data->{content});
145  
146 0 my $fact = CPAN::Testers::Report->from_struct( $data ) ;
147 0 return $fact;
148  
149 0 $self->{hash} = $data;
150 0 return $data;
151 }
152  
153 sub error {
154 1 ▶ my $self = shift;
Subroutine error: called CC=1 SCAR=0.0
Pod documented
155 1 return $self->{error};
156 }
157  
158 6 q("With thanks to the 2014 QA Hackathon");
159  
160 __END__
↑ Summary blib/lib/CPAN/Testers/WWW/Reports/Query/Report.pm »