| 1 | package CPAN::Testers::WWW::Reports::Query::Reports; | ||||||||||||||||||||||||||||
| 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.09'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | CPAN::Testers::WWW::Reports::Query::Reports - Retrieve CPAN Testers metadata direct from the CPAN Testers website. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | This module queries the CPAN Testers website and retrieves a data set. For a | ||||||||||||||||||||||||||||
| 17 | date request, the data set returned relates to the ids that can be retrieved | ||||||||||||||||||||||||||||
| 18 | for that date. A range request will return the records for the requested IDs. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # establish the object | ||||||||||||||||||||||||||||
| 23 | my $query = CPAN::Testers::WWW::Reports::Query::Reports->new; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # get list of ids for a particular date | ||||||||||||||||||||||||||||
| 26 | my $result = $query->date( | ||||||||||||||||||||||||||||
| 27 | '2012-02-08' # must be YYYY-MM-DD format | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # $query is a hash ref | ||||||||||||||||||||||||||||
| 31 | print "From: $result->{from}, To: $result->{to}, Range: $result->{range}\n"; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # $result->{list} is an array of the actual ids posted for the given date. | ||||||||||||||||||||||||||||
| 34 | # note that this list may not include all ids within $result->{range}. | ||||||||||||||||||||||||||||
| 35 | print "List: " . join(', ',@{$result->{list}}) . "\n"; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # get metabase for a range of ids | ||||||||||||||||||||||||||||
| 39 | my $result = $query->range( | ||||||||||||||||||||||||||||
| 40 | '20080300-20120330' | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # '20120330' # just get <id> | ||||||||||||||||||||||||||||
| 43 | # '20120330-' # from <id> until the latest [see caveat] | ||||||||||||||||||||||||||||
| 44 | # '-20120330' # previous <n> reports up to <id> [see caveat] | ||||||||||||||||||||||||||||
| 45 | # '-' # the latest <n> reports [see caveat] | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # $result is a hash ref, with the reports ids as the top level keys | ||||||||||||||||||||||||||||
| 49 | my @ids = sort keys %$result; | ||||||||||||||||||||||||||||
| 50 | my $id = $ids[0]; | ||||||||||||||||||||||||||||
| 51 | print "id = $id, dist = '$result->{$id}{dist}', version = '$result->{$id}{version}'\n"; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # get the raw data for all results, or a specific version if supplied | ||||||||||||||||||||||||||||
| 55 | my $data = $query->raw; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # get the last error | ||||||||||||||||||||||||||||
| 59 | my $error = $query->error; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head2 Caveat | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Note that when using the range parameter, at most only 2500 records will be | ||||||||||||||||||||||||||||
| 64 | returned. This is to avoid accidental requests for all records! | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | This value may change in the future. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 71 | # Library Modules | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 7 | ▶ | use WWW::Mechanize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 7 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 77 | # Variables | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 7 | my $URL = 'http://www.cpantesters.org/cgi-bin/reports-metadata.cgi'; | |||||||||||||||||||||||||||
| 80 | #$URL = 'http://reports/cgi-bin/reports-metadata.cgi'; # local test version | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 7 | my $mech = WWW::Mechanize->new(); | |||||||||||||||||||||||||||
| 83 | 7 | $mech->agent_alias( 'Linux Mozilla' ); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # ------------------------------------- | ||||||||||||||||||||||||||||
| 86 | # Program | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub new { | ||||||||||||||||||||||||||||
| 89 | 2 | ▶ | my($class, %hash) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 2 | my $self = {}; | |||||||||||||||||||||||||||
| 91 | 2 | bless $self, $class; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | return $self; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub raw { | ||||||||||||||||||||||||||||
| 97 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 5 | return $self->{content}; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub date { | ||||||||||||||||||||||||||||
| 102 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
date: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 3 | ▶ | my $date = shift || return; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || (return)
MC/DC100%1/1
shift() || (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | ▶ | return unless($date =~ /^\d{4}\-\d{2}\-\d{2}$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | return unless($self->_request( "date=$date" )); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | return $self->_parse(); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | sub range { | ||||||||||||||||||||||||||||
| 113 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
range: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 6 | ▶ | my $range = shift || return; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || (return)
MC/DC100%1/1
shift() || (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 5 | ▶ | return unless($range =~ /^(\d+)?\-(\d+)?$/ || $range =~ /^(\d+)$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$range =~ /^(\d+)?\-(\d+)?$/ or $range =~ /^(\d+)$/
MC/DC100%2/2
$range =~ /^(\d+)?\-(\d+)?$/ or $range =~ /^(\d+)$/
$range =~ /^(\d+)?\-(\d+)?$/
$range =~ /^(\d+)$/
Truth table100%3/3
A or B
A= $range =~ /^(\d+)?\-(\d+)?$/B= $range =~ /^(\d+)$/
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 4 | ▶ | return unless($self->_request( "range=$range" )); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 4 | return $self->_parse(); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub _request { | ||||||||||||||||||||||||||||
| 124 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_request: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 125 | 5 | my $param = shift; | |||||||||||||||||||||||||||
| 126 | 5 | $self->{error} = ''; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 5 | my $url = join( '?', $URL, $param ); | |||||||||||||||||||||||||||
| 129 | #print STDERR "# URL: $url\n"; | ||||||||||||||||||||||||||||
| 130 | 5 | eval { $mech->get( $url ); }; | |||||||||||||||||||||||||||
| 131 | 5 | ▶ | 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
| |||||||||||||||||||||||||||||
| 132 | #print STDERR "# ERROR: $@, ".$mech->success."\n"; | ||||||||||||||||||||||||||||
| 133 | 0 | $self->{error} = $@; | |||||||||||||||||||||||||||
| 134 | 0 | return; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | #print STDERR "# CONTENT=".$mech->content."\n"; | ||||||||||||||||||||||||||||
| 138 | 5 | $self->{content} = $mech->content; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub _parse { | ||||||||||||||||||||||||||||
| 142 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=4 SCAR=17.9
| |||||||||||||||||||||||||||||
| 143 | 5 | my $data; | |||||||||||||||||||||||||||
| 144 | 5 | eval { $data = decode_json($self->{content}) }; | |||||||||||||||||||||||||||
| 145 | 5 | ▶ | return $data unless($@ || !$data); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $data
MC/DC0%0/2
$@ or not $data
$@
not $data
Truth table33%1/3
A or not(B)
A= $@B= not $data
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $self->{error} = $@ || 'no data returned'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'no data returned'
MC/DC0%0/1
$@ || 'no data returned'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||
| 148 | 0 | return; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub error { | ||||||||||||||||||||||||||||
| 152 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 9 | return $self->{error}; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 7 | q("With thanks to the 2012 QA Hackathon"); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||