File Coverage

lib/Acme/CPANAuthors.pm
Criterion Covered Total %
statement 74 91 81.3
branch 23 44 52.2
condition 3 3 100.0
subroutine 14 16 87.5
pod 10 10 100.0
total 124 164 75.6


line stmt bran cond sub pod time code
1             package Acme::CPANAuthors;
2              
3 7     7   513684 use strict;
  7         17  
  7         300  
4 7     7   33 use warnings;
  7         21  
  7         360  
5 7     7   47 use Carp;
  7         20  
  7         628  
6 7     7   3586 use Acme::CPANAuthors::Utils qw( cpan_authors cpan_packages );
  7         18  
  7         10365  
7              
8             our $VERSION = '0.27';
9              
10             sub new {
11 3     3 1 619208 my ($class, @categories) = @_;
12              
13 3 50       17 @categories = _list_categories() unless @categories;
14              
15 3         9 my %authors;
16 3         10 foreach my $category ( @categories ) {
17 3         17 %authors = ( %authors, _get_authors_of($category) );
18             }
19              
20             bless {
21 3         28 categories => \@categories,
22             authors => \%authors,
23             }, $class;
24             }
25              
26             sub count {
27 2     2 1 14 my $self = shift;
28              
29 2         4 return scalar keys %{ $self->{authors} };
  2         16  
30             }
31              
32             sub id {
33 4     4 1 1517 my ($self, $id) = @_;
34              
35 4 100       14 unless ( $id ) {
36 2         4 my @ids = sort keys %{ $self->{authors} };
  2         12  
37 2         10 return @ids;
38             }
39             else {
40 2 50       16 return $self->{authors}{$id} ? 1 : 0;
41             }
42             }
43              
44             sub name {
45 5     5 1 2387 my ($self, $id) = @_;
46              
47 5 100       18 unless ( $id ) {
48 2         4 return sort values %{ $self->{authors} };
  2         13  
49             }
50             else {
51 3         25 return $self->{authors}{$id};
52             }
53             }
54              
55             sub categories {
56 2     2 1 522 my $self = shift;
57 2         6 return @{$self->{categories}};
  2         13  
58             }
59              
60             sub distributions {
61 0     0 1 0 my ($self, $id) = @_;
62              
63 0 0       0 return unless $id;
64              
65 0         0 my @packages;
66 0         0 foreach my $package ( cpan_packages->distributions ) {
67 0 0       0 if ( $package->cpanid eq $id ) {
68 0         0 push @packages, $package;
69             }
70             }
71              
72 0         0 return @packages;
73             }
74              
75             sub latest_distributions {
76 0     0 1 0 my ($self, $id) = @_;
77              
78 0 0       0 return unless $id;
79              
80 0         0 my @packages;
81 0         0 foreach my $package ( cpan_packages->latest_distributions ) {
82 0 0       0 if ( $package->cpanid eq $id ) {
83 0         0 push @packages, $package;
84             }
85             }
86              
87 0         0 return @packages;
88             }
89              
90             sub avatar_url {
91 1     1 1 8 my ($self, $id, %options) = @_;
92              
93 1 50       4 return unless $id;
94              
95 1 50       2 eval {require Gravatar::URL; 1}
  1         8  
  1         6  
96             or warn($@), return;
97 1 50       6 my $author = cpan_authors->author($id) or return;
98              
99 1         4 my $default = delete $options{default};
100 1 50       41 return Gravatar::URL::gravatar_url(
101             email => $author->email,
102             %options,
103             default => Gravatar::URL::gravatar_url(
104             # Fall back to the CPAN address, as used by metacpan, which will in
105             # turn fall back to a generated image.
106             email => $id . '@cpan.org',
107             %options,
108             $default ? ( default => $default ) : (),
109             ),
110             );
111             }
112              
113             sub kwalitee {
114 1     1 1 227088 my ($self, $id) = @_;
115              
116 1 50       5 return unless $id;
117              
118 1         825 require Acme::CPANAuthors::Utils::Kwalitee;
119 1         18 return Acme::CPANAuthors::Utils::Kwalitee->fetch($id);
120             }
121              
122             sub look_for {
123 5     5 1 165125 my ($self, $id_or_name) = @_;
124              
125 5 50       14 return unless defined $id_or_name;
126 5 100       12 unless (ref $id_or_name eq 'Regexp') {
127 4         55 $id_or_name = qr/$id_or_name/i;
128             }
129              
130 5         7 my @found;
131 5 50       9 my @categories = ref $self ? @{ $self->{categories} } : ();
  0         0  
132 5 50       14 @categories = _list_categories() unless @categories;
133 5         12 foreach my $category ( @categories ) {
134 15         22 my %authors = _get_authors_of($category);
135 15         41 while ( my ($id, $name) = each %authors ) {
136 155 100 100     570 if ($id =~ /$id_or_name/ or $name =~ /$id_or_name/) {
137 6         29 push @found, {
138             id => $id,
139             name => $name,
140             category => $category,
141             };
142             }
143             }
144             }
145 5         21 return @found;
146             }
147              
148             sub _list_categories {
149 5     5   796 require Module::Find;
150 30         60 return grep { $_ !~ /^(?:Register|Utils|Not|Search|Factory)$/ }
151 5         1490 map { s/^Acme::CPANAuthors:://; $_ }
  30         7753  
  30         44  
152             Module::Find::findsubmod( 'Acme::CPANAuthors' );
153             }
154              
155             sub _get_authors_of {
156 18     18   30 my $category = shift;
157              
158 18         24 $category =~ s/^Acme::CPANAuthors:://;
159              
160 18 50       55 return if $category =~ /^(?:Register|Utils|Search)$/;
161              
162 18         28 my $package = "Acme::CPANAuthors\::$category";
163 18 100       118 unless ($package->can('authors')) {
164 5         337 eval "require $package";
165 5 50       72 if ( $@ ) {
166 0         0 carp "$category CPAN Authors are not registered yet: $@";
167 0         0 return;
168             }
169             # some may actually lack 'authors' interface
170 5 50       52 return unless $package->can('authors');
171             }
172 18         45 $package->authors;
173             }
174              
175             1;
176              
177             __END__