File Coverage

lib/Acme/CPANAuthors/Utils/Kwalitee.pm
Criterion Covered Total %
statement 25 29 86.2
branch 4 8 50.0
condition 2 6 33.3
subroutine 7 8 87.5
pod 1 1 100.0
total 39 52 75.0


line stmt bran cond sub pod time code
1             package Acme::CPANAuthors::Utils::Kwalitee;
2              
3 2     2   569 use strict;
  2         3  
  2         56  
4 2     2   7 use warnings;
  2         2  
  2         38  
5 2     2   1202 use HTTP::Tiny;
  2         76191  
  2         89  
6 2     2   832 use JSON::PP ();
  2         10359  
  2         414  
7              
8             my $ua;
9              
10 1     1   46 sub _uri { "http://api.cpanauthors.org/kwalitee/" . shift }
11              
12             sub _ua {
13 1     1   1 my $class = shift;
14 1 50       5 $ua = $_[0] if @_;
15 1   33     6 $ua ||= HTTP::Tiny->new(env_proxy => 1);
16 1         104 $ua;
17             }
18              
19             sub fetch {
20 1     1 1 2 my ($class, $id) = @_;
21              
22 1 50       3 return unless $id;
23              
24 1         2 my $res = $class->_ua->get(_uri(lc $id));
25 1 50 33     1839809 unless ($res->{success} && $res->{status} == 200) {
26 0         0 $class->_error("$res->{status} $res->{reason}");
27             }
28              
29 1         4 my $json = eval { JSON::PP::decode_json($res->{content}) };
  1         7  
30 1 50       444128 if ($@) {
31 0         0 $class->_error($@);
32             }
33 1         16 return $json;
34             }
35              
36             sub _error {
37 0     0     my ($class, $error) = @_;
38              
39 0           die "API SERVER ERROR\n"
40             . "Couldn't parse kwalitee info from the api server\n"
41             . " $error\n"
42             . "Sorry for the inconvenience. If this lingers long,\n"
43             . "please drop a line to .\n";
44             }
45              
46             1;
47              
48             __END__