| 1 | package CPAN::Testers::WWW::Reports::Query::Reports; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | 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 | use WWW::Mechanize; | ||
| 74 | use JSON::XS; | ||
| 75 | |||
| 76 | #---------------------------------------------------------------------------- | ||
| 77 | # Variables | ||
| 78 | |||
| 79 | 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 | my $mech = WWW::Mechanize->new(); | ||
| 83 | $mech->agent_alias( 'Linux Mozilla' ); | ||
| 84 | |||
| 85 | # ------------------------------------- | ||
| 86 | # Program | ||
| 87 | |||
| 88 | sub new { | ||
| 89 | my($class, %hash) = @_; | ||
| 90 | my $self = {}; | ||
| 91 | bless $self, $class; | ||
| 92 | |||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub raw { | ||
| 97 | my $self = shift; | ||
| 98 | return $self->{content}; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub date { | ||
| 102 | my $self = shift; | ||
| 103 | my $date = shift || return; | ||
| 104 | |||
| 105 | return unless($date =~ /^\d{4}\-\d{2}\-\d{2}$/); | ||
| 106 | |||
| 107 | return unless($self->_request( "date=$date" )); | ||
| 108 | |||
| 109 | return $self->_parse(); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub range { | ||
| 113 | my $self = shift; | ||
| 114 | my $range = shift || return; | ||
| 115 | |||
| 116 | return unless($range =~ /^(\d+)?\-(\d+)?$/ || $range =~ /^(\d+)$/); | ||
| 117 | |||
| 118 | return unless($self->_request( "range=$range" )); | ||
| 119 | |||
| 120 | return $self->_parse(); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _request { | ||
| 124 | my $self = shift; | ||
| 125 | my $param = shift; | ||
| 126 | $self->{error} = ''; | ||
| 127 | |||
| 128 | my $url = join( '?', $URL, $param ); | ||
| 129 | #print STDERR "# URL: $url\n"; | ||
| 130 | eval { $mech->get( $url ); }; | ||
| 131 | if($@ || !$mech->success()) { | ||
| 132 | #print STDERR "# ERROR: $@, ".$mech->success."\n"; | ||
| 133 | $self->{error} = $@; | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | #print STDERR "# CONTENT=".$mech->content."\n"; | ||
| 138 | $self->{content} = $mech->content; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _parse { | ||
| 142 | my $self = shift; | ||
| 143 | my $data; | ||
| 144 | eval { $data = decode_json($self->{content}) }; | ||
| 145 | return $data unless($@ || !$data); | ||
| 146 | |||
| 147 | $self->{error} = $@ || 'no data returned'; | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub error { | ||
| 152 | my $self = shift; | ||
| 153 | return $self->{error}; | ||
| 154 | } | ||
| 155 | |||
| 156 | q("With thanks to the 2012 QA Hackathon"); | ||
| 157 | |||
| 158 | __END__ |