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   670 use strict;
  2         2  
  2         85  
4 2     2   10 use warnings;
  2         2  
  2         59  
5 2     2   2907 use HTTP::Tiny;
  2         88049  
  2         66  
6 2     2   711 use JSON::PP ();
  2         10191  
  2         430  
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       7 $ua = $_[0] if @_;
15 1   33     6 $ua ||= HTTP::Tiny->new(env_proxy => 1);
16 1         90 $ua;
17             }
18              
19             sub fetch {
20 1     1 1 2 my ($class, $id) = @_;
21              
22 1 50       4 return unless $id;
23              
24 1         2 my $res = $class->_ua->get(_uri(lc $id));
25 1 50 33     1645050 unless ($res->{success} && $res->{status} == 200) {
26 0         0 $class->_error("$res->{status} $res->{reason}");
27             }
28              
29 1         2 my $json = eval { JSON::PP::decode_json($res->{content}) };
  1         7  
30 1 50       467418 if ($@) {
31 0         0 $class->_error($@);
32             }
33 1         15 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__