| 1 | package CPAN::Testers::WWW::Reports::Query::AJAX; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | CPAN::Testers::WWW::Reports::Query::AJAX - Get specific CPAN Testers results | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | my $query = CPAN::Testers::WWW::Reports::Query::AJAX->new( | ||||||||||||||||||||||||||||
| 17 | dist => 'App-Maisha', | ||||||||||||||||||||||||||||
| 18 | version => '0.12', # optional, will default to latest version | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # basic results | ||||||||||||||||||||||||||||
| 22 | printf "ALL: %d\n" . | ||||||||||||||||||||||||||||
| 23 | "PASS: %d\n" . | ||||||||||||||||||||||||||||
| 24 | "FAIL: %d\n" . | ||||||||||||||||||||||||||||
| 25 | "NA: %d\n" . | ||||||||||||||||||||||||||||
| 26 | "UNKNOWN: %d\n" . | ||||||||||||||||||||||||||||
| 27 | "%age PASS: %d\n" . | ||||||||||||||||||||||||||||
| 28 | "%age FAIL: %d\n" . | ||||||||||||||||||||||||||||
| 29 | "%age NA: %d\n" . | ||||||||||||||||||||||||||||
| 30 | "%age UNKNOWN: %d\n", | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | $query->all, | ||||||||||||||||||||||||||||
| 33 | $query->pass, | ||||||||||||||||||||||||||||
| 34 | $query->fail, | ||||||||||||||||||||||||||||
| 35 | $query->na, | ||||||||||||||||||||||||||||
| 36 | $query->unknown, | ||||||||||||||||||||||||||||
| 37 | $query->pc_pass, | ||||||||||||||||||||||||||||
| 38 | $query->pc_fail, | ||||||||||||||||||||||||||||
| 39 | $query->pc_na, | ||||||||||||||||||||||||||||
| 40 | $query->pc_unknown; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # get the raw data for all results, or a specific version if supplied | ||||||||||||||||||||||||||||
| 43 | my $data = $query->raw; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # basic filters (see new() for details) | ||||||||||||||||||||||||||||
| 46 | my $query = CPAN::Testers::WWW::Reports::Query::AJAX->new( | ||||||||||||||||||||||||||||
| 47 | dist => 'App-Maisha', | ||||||||||||||||||||||||||||
| 48 | version => '0.12', | ||||||||||||||||||||||||||||
| 49 | osname => 'Win32', | ||||||||||||||||||||||||||||
| 50 | patches => 1, | ||||||||||||||||||||||||||||
| 51 | perlmat => 1, | ||||||||||||||||||||||||||||
| 52 | perlver => '5.10.0', | ||||||||||||||||||||||||||||
| 53 | format => 'xml' # xml is default, text also supported | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | printf "Win32 PASS: %d\n", $query->pass; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | This module queries the CPAN Testers website (via the AJAX interface) and | ||||||||||||||||||||||||||||
| 61 | retrieves a simple data set of results. It then parses these to answer a few | ||||||||||||||||||||||||||||
| 62 | simple questions. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 67 | # Library Modules | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 7 | ▶ | use WWW::Mechanize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 72 | # Variables | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7 | my $URL = 'http://www.cpantesters.org/cgi-bin/reports-summary.cgi?'; | |||||||||||||||||||||||||||
| 75 | #$URL = 'http://reports/cgi-bin/reports-summary.cgi?'; # local test version | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 7 | my %rules = ( | |||||||||||||||||||||||||||
| 78 | dist => qr/^([-\w.]+)$/i, | ||||||||||||||||||||||||||||
| 79 | version => qr/^([-\w.]+)$/i, | ||||||||||||||||||||||||||||
| 80 | perlmat => qr/^([0-2])$/i, | ||||||||||||||||||||||||||||
| 81 | patches => qr/^([0-2])$/i, | ||||||||||||||||||||||||||||
| 82 | perlver => qr/^([\w.]+)$/i, | ||||||||||||||||||||||||||||
| 83 | osname => qr/^([\w.]+)$/i, | ||||||||||||||||||||||||||||
| 84 | format => qr/^(text|html|xml)$/i | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 7 | my @fields = keys %rules; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 7 | my $mech = WWW::Mechanize->new(); | |||||||||||||||||||||||||||
| 90 | 7 | $mech->agent_alias( 'Linux Mozilla' ); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # ------------------------------------- | ||||||||||||||||||||||||||||
| 93 | # Program | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub new { | ||||||||||||||||||||||||||||
| 96 | 8 | ▶ | my($class, %hash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 8 | my $self = { | |||||||||||||||||||||||||||
| 98 | success => 0, | ||||||||||||||||||||||||||||
| 99 | error => '' | ||||||||||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||||||||||
| 101 | 8 | bless $self, $class; | |||||||||||||||||||||||||||
| 102 | 8 | my @valid = qw(format); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 8 | for my $key (@fields) { | |||||||||||||||||||||||||||
| 105 | 56 | ▶ | next unless($hash{$key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 18 | $hash{$key} =~ s/$rules{$key}/$1/; | |||||||||||||||||||||||||||
| 107 | 18 | ▶ | next unless($hash{$key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 18 | $self->{options}{$key} = $hash{$key}; | |||||||||||||||||||||||||||
| 110 | 18 | push @valid, $key; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 8 | ▶ | $self->{options}{format} ||= 'xml'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"options"}{"format"} ||= "xml"
MC/DC100%1/1
$self->{"options"}{"format"} ||= "xml"
$self->{"options"}{"format"}
Truth table100%2/2
A
A= $self->{"options"}{"format"}
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # ajax request | ||||||||||||||||||||||||||||
| 116 | 8 | my $url = $URL; | |||||||||||||||||||||||||||
| 117 | 8 | $url .= join( '&', map { "$_=$self->{options}{$_}" } @valid ); | |||||||||||||||||||||||||||
| 118 | #print "URL: $url\n"; | ||||||||||||||||||||||||||||
| 119 | 8 | eval { $mech->get( $url ); }; | |||||||||||||||||||||||||||
| 120 | 8 | ▶ | if($@ || !$mech->success()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $mech->success
MC/DC0%0/2
$@ or not $mech->success
$@
not $mech->success
Truth table33%1/3
A or not(B)
A= $@B= not $mech->success
| |||||||||||||||||||||||||||||
| 121 | 0 | $self->{error} = $@; | |||||||||||||||||||||||||||
| 122 | 0 | return $self; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | #print "URI: " . $mech->uri . "\n"; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 8 | $self->_parse( $mech->content() ); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 8 | $self->{success} = 1; | |||||||||||||||||||||||||||
| 130 | 8 | return $self; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 8 | ▶ | sub is_success { $_[0]->{success}; } | ||||||||||||||||||||||||||
|
Subroutine
is_success: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 8 | ▶ | sub error { $_[0]->{error}; } | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 6 | ▶ | sub all { $_[0]->_basic('all'); } | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 6 | ▶ | sub pass { $_[0]->_basic('pass'); } | ||||||||||||||||||||||||||
|
Subroutine
pass: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 6 | ▶ | sub fail { $_[0]->_basic('fail'); } | ||||||||||||||||||||||||||
|
Subroutine
fail: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 6 | ▶ | sub na { $_[0]->_basic('na'); } | ||||||||||||||||||||||||||
|
Subroutine
na: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 6 | ▶ | sub unknown { $_[0]->_basic('unknown'); } | ||||||||||||||||||||||||||
|
Subroutine
unknown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 6 | ▶ | sub pc_pass { $_[0]->_basic_pc('pass'); } | ||||||||||||||||||||||||||
|
Subroutine
pc_pass: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 6 | ▶ | sub pc_fail { $_[0]->_basic_pc('fail'); } | ||||||||||||||||||||||||||
|
Subroutine
pc_fail: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 6 | ▶ | sub pc_na { $_[0]->_basic_pc('na'); } | ||||||||||||||||||||||||||
|
Subroutine
pc_na: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 6 | ▶ | sub pc_unknown { $_[0]->_basic_pc('unknown'); } | ||||||||||||||||||||||||||
|
Subroutine
pc_unknown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub _basic { | ||||||||||||||||||||||||||||
| 148 | 30 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_basic: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 149 | 30 | my $grade = shift; | |||||||||||||||||||||||||||
| 150 | 30 | ▶ | my $version = $self->{options}{version} || $self->{recent}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{'options'}{'version'} || $self->{'recent'}
MC/DC0%0/2
$self->{'options'}{'version'} || $self->{'recent'}
$self->{'options'}{'version'}
$self->{'recent'}
Truth table66%2/3
A || B
A= $self->{'options'}{'version'}B= $self->{'recent'}
| |||||||||||||||||||||||||||||
| 151 | 30 | return $self->{result}{$version}{$grade}; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub _basic_pc { | ||||||||||||||||||||||||||||
| 155 | 24 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_basic_pc: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 156 | 24 | my $grade = shift; | |||||||||||||||||||||||||||
| 157 | 24 | ▶ | my $version = $self->{options}{version} || $self->{recent}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{'options'}{'version'} || $self->{'recent'}
MC/DC0%0/2
$self->{'options'}{'version'} || $self->{'recent'}
$self->{'options'}{'version'}
$self->{'recent'}
Truth table66%2/3
A || B
A= $self->{'options'}{'version'}B= $self->{'recent'}
| |||||||||||||||||||||||||||||
| 158 | 24 | ▶ | return 0 unless($self->{result}{$version}{'all'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 24 | my $pc = sprintf "%3.10f", $self->{result}{$version}{$grade} / $self->{result}{$version}{'all'} * 100; | |||||||||||||||||||||||||||
| 160 | 24 | $pc =~ s/\.?0+$//; | |||||||||||||||||||||||||||
| 161 | 24 | return $pc; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | sub _parse { | ||||||||||||||||||||||||||||
| 165 | 8 | ▶ | my ($self,$content) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||
| 166 | 8 | $self->{content} = $content; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 8 | ▶ | if($self->{options}{format} eq 'txt') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 169 | 2 | my @lines = split("\n",$content); | |||||||||||||||||||||||||||
| 170 | 2 | for my $line (@lines) { | |||||||||||||||||||||||||||
| 171 | 19 | ▶ | next if($line =~ /^\s*$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 19 | my ($version,$all,$pass,$fail,$na,$unknown) = split(',',$line); | |||||||||||||||||||||||||||
| 173 | 19 | ▶ | next unless($version); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 19 | ▶ | if (!exists $self->{recent}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 2 | $self->{recent} = $version; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 19 | $self->{result}{$version}{pass} = $pass; | |||||||||||||||||||||||||||
| 178 | 19 | $self->{result}{$version}{fail} = $fail; | |||||||||||||||||||||||||||
| 179 | 19 | $self->{result}{$version}{na} = $na; | |||||||||||||||||||||||||||
| 180 | 19 | $self->{result}{$version}{unknown} = $unknown; | |||||||||||||||||||||||||||
| 181 | 19 | $self->{result}{$version}{all} = $all; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | } elsif($self->{options}{format} eq 'xml') { | ||||||||||||||||||||||||||||
| 185 | 4 | my @lines = split("\n",$content); | |||||||||||||||||||||||||||
| 186 | 4 | for my $line (@lines) { | |||||||||||||||||||||||||||
| 187 | 40 | ▶ | next if($line =~ /^\s*$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 40 | my ($all,$pass,$fail,$na,$unknown,$version) = $line =~ m{<version all="([^"]+)" pass="([^"]+)" fail="([^"]+)" na="([^"]+)" unknown="([^"]+)">([^<]+)</version>}; | |||||||||||||||||||||||||||
| 189 | 40 | ▶ | next unless($version); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 38 | ▶ | if (!exists $self->{recent}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 4 | $self->{recent} = $version; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 38 | $self->{result}{$version}{pass} = $pass; | |||||||||||||||||||||||||||
| 194 | 38 | $self->{result}{$version}{fail} = $fail; | |||||||||||||||||||||||||||
| 195 | 38 | $self->{result}{$version}{na} = $na; | |||||||||||||||||||||||||||
| 196 | 38 | $self->{result}{$version}{unknown} = $unknown; | |||||||||||||||||||||||||||
| 197 | 38 | $self->{result}{$version}{all} = $all; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | } elsif($self->{options}{format} eq 'html') { | ||||||||||||||||||||||||||||
| 201 | # TODO: need to pull out OT response | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # currently no parsing for other formats. | ||||||||||||||||||||||||||||
| 205 | # use raw to do it yourself :) | ||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | sub data { | ||||||||||||||||||||||||||||
| 209 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
data: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 8 | my $version = shift; | |||||||||||||||||||||||||||
| 211 | 8 | ▶ | return $self->{result}{$version} if($version); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 8 | return $self->{result}; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | sub raw { | ||||||||||||||||||||||||||||
| 216 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 217 | 8 | return $self->{content}; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 7 | 1; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | __END__ | ||||||||||||||||||||||||||||