line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CPAN::Testers::WWW::Reports::Query::Report; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
310296
|
use strict; |
|
6
|
|
|
|
|
20
|
|
|
6
|
|
|
|
|
250
|
|
4
|
6
|
|
|
6
|
|
37
|
use warnings; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
409
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
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
|
|
|
6
|
|
6150
|
use CPAN::Testers::Report; |
|
6
|
|
|
|
|
382580
|
|
|
6
|
|
|
|
|
198
|
|
65
|
6
|
|
|
6
|
|
50
|
use JSON::XS; |
|
6
|
|
|
|
|
177
|
|
|
6
|
|
|
|
|
524
|
|
66
|
6
|
|
|
6
|
|
11636
|
use WWW::Mechanize; |
|
6
|
|
|
|
|
1507413
|
|
|
6
|
|
|
|
|
3471
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
#---------------------------------------------------------------------------- |
69
|
|
|
|
|
|
|
# Variables |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $HOST = 'http://www.cpantesters.org'; |
72
|
|
|
|
|
|
|
my $PATH = '%s/cpan/report/%s?json=1'; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $mech = WWW::Mechanize->new(); |
75
|
|
|
|
|
|
|
$mech->agent_alias( 'Linux Mozilla' ); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# ------------------------------------- |
78
|
|
|
|
|
|
|
# Program |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub new { |
81
|
7
|
|
|
7
|
1
|
32643
|
my($class, %hash) = @_; |
82
|
7
|
|
|
|
|
44
|
my $self = {}; |
83
|
|
|
|
|
|
|
|
84
|
7
|
|
100
|
|
|
93
|
$self->{as_json} = $hash{as_json} || 0; |
85
|
7
|
|
100
|
|
|
68
|
$self->{as_hash} = $hash{as_hash} || 0; |
86
|
7
|
|
66
|
|
|
102
|
$self->{host} = $hash{host} || $HOST; |
87
|
|
|
|
|
|
|
|
88
|
7
|
|
|
|
|
328
|
bless $self, $class; |
89
|
|
|
|
|
|
|
|
90
|
7
|
|
|
|
|
35
|
return $self; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub report { |
94
|
7
|
|
|
7
|
1
|
5244
|
my $self = shift; |
95
|
7
|
|
|
|
|
35
|
my %hash = @_; |
96
|
|
|
|
|
|
|
|
97
|
7
|
|
|
|
|
34
|
$self->{error} = ''; |
98
|
|
|
|
|
|
|
|
99
|
7
|
|
|
|
|
62
|
my $url = sprintf $PATH, $self->{host}, $hash{report}; |
100
|
7
|
|
|
|
|
17
|
eval { $mech->get( $url ); }; |
|
7
|
|
|
|
|
67
|
|
101
|
7
|
50
|
33
|
|
|
10057305
|
if($@ || !$mech->success()) { |
102
|
0
|
|
|
|
|
0
|
$self->{error} = "No response from server: $@"; |
103
|
0
|
|
|
|
|
0
|
return; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
7
|
|
|
|
|
213
|
$self->{content} = $mech->content; |
107
|
|
|
|
|
|
|
|
108
|
7
|
|
|
|
|
157
|
my $data; |
109
|
7
|
|
|
|
|
11
|
eval { |
110
|
7
|
|
|
|
|
1023
|
$data = decode_json($self->{content}); |
111
|
|
|
|
|
|
|
}; |
112
|
|
|
|
|
|
|
|
113
|
7
|
50
|
33
|
|
|
50
|
if($@ || !$data) { |
114
|
0
|
|
0
|
|
|
0
|
$self->{error} = "JSON decoding error: " . ($@ || 'no data returned'); |
115
|
0
|
|
|
|
|
0
|
return; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
7
|
50
|
|
|
|
29
|
return unless($data->{success}); |
119
|
|
|
|
|
|
|
|
120
|
7
|
100
|
66
|
|
|
70
|
my $as = ($hash{as_json} || (!defined $hash{as_json} && $self->{as_json})) ? 'json' : ''; |
121
|
7
|
100
|
66
|
|
|
61
|
$as ||= ($hash{as_hash} || (!defined $hash{as_hash} && $self->{as_hash})) ? 'hash' : 'fact'; |
|
|
|
66
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
7
|
100
|
|
|
|
595
|
return encode_json($data->{result}) if($as eq 'json'); |
124
|
4
|
100
|
|
|
|
23
|
return $data->{result} if($as eq 'hash'); |
125
|
|
|
|
|
|
|
|
126
|
2
|
|
|
|
|
15
|
return $self->_parse($data->{result}); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _parse { |
130
|
2
|
|
|
2
|
|
5
|
my ($self,$data) = @_; |
131
|
2
|
|
|
|
|
3
|
my $hash; |
132
|
|
|
|
|
|
|
|
133
|
2
|
50
|
|
|
|
14
|
if(!$data) { |
134
|
0
|
|
|
|
|
0
|
$self->{error} = 'no data returned'; |
135
|
0
|
|
|
|
|
0
|
return; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
2
|
|
|
|
|
4
|
for my $content (@{ $data->{content} }) { |
|
2
|
|
|
|
|
7
|
|
139
|
4
|
|
|
|
|
174
|
$content->{content} = encode_json($content->{content}); |
140
|
|
|
|
|
|
|
} |
141
|
2
|
|
|
|
|
154
|
$data->{content} = encode_json($data->{content}); |
142
|
|
|
|
|
|
|
|
143
|
2
|
|
|
|
|
53
|
my $fact = CPAN::Testers::Report->from_struct( $data ) ; |
144
|
2
|
|
|
|
|
43963
|
return $fact; |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
0
|
$self->{hash} = $data; |
147
|
0
|
|
|
|
|
0
|
return $data; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub error { |
151
|
7
|
|
|
7
|
1
|
31604
|
my $self = shift; |
152
|
7
|
|
|
|
|
42
|
return $self->{error}; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
q("With thanks to the 2014 QA Hackathon"); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__ |