File Coverage

blib/lib/MetaCPAN/Helper.pm
Criterion Covered Total %
statement 15 54 27.7
branch 0 16 0.0
condition 0 12 0.0
subroutine 5 12 41.6
pod 4 6 66.6
total 24 100 24.0


line stmt bran cond sub pod time code
1             package MetaCPAN::Helper;
2             $MetaCPAN::Helper::VERSION = '0.04';
3 1     1   588 use 5.006;
  1         3  
  1         33  
4 1     1   4 use strict;
  1         1  
  1         26  
5 1     1   9 use warnings;
  1         1  
  1         25  
6 1     1   490 use Moo;
  1         11374  
  1         5  
7 1     1   1142 use Carp;
  1         1  
  1         506  
8              
9             has client => (
10             is => 'ro',
11             default => sub {
12             require MetaCPAN::Client;
13             return MetaCPAN::Client->new();
14             },
15             );
16              
17             sub module2dist {
18 0     0 1   my $self = shift;
19 0           my $_m = shift;
20 0 0         ref($_m) eq 'MetaCPAN::Client::Module' and return $_m->distribution;
21 0 0         ref($_m) and croak "invalid module name";
22              
23 0           my $module_name = $_m;
24              
25 0           my $query = { all => [
26             { status => 'latest' },
27             { maturity => 'released' },
28             { 'module.name' => $module_name },
29             ]
30             };
31 0           my $params = { fields => [qw(distribution)] };
32 0   0       my $result_set = $self->client->module($query, $params) || return undef;
33 0   0       my $module = $result_set->next || return undef;
34              
35 0   0       return $module->distribution || undef;
36             }
37              
38             sub release2repo {
39 0     0 0   my $self = shift;
40 0           my $_r = shift;
41              
42 0 0         my $release = ref($_r) eq 'MetaCPAN::Client::Release'
    0          
43             ? $_r
44             : !ref($_r)
45             ? $self->client->release($_r)
46             : croak "invalid release name";
47              
48 0   0       my $res = $release->resources || return undef;
49 0   0       my $rep = $res->{repository} || return undef;
50              
51 0   0       return ( $rep->{url} || undef );
52             }
53              
54             sub dist2repo {
55 0     0 0   my $self = shift;
56 0           my $dist_name = _get_dist_name(shift);
57              
58 0           my $lr = $self->dist2latest_release($dist_name);
59              
60 0           return $self->release2repo($lr);
61             }
62              
63             sub dist2releases {
64 0     0 1   my $self = shift;
65 0           my $dist_name = _get_dist_name(shift);
66              
67 0           my $filter = { distribution => $dist_name };
68 0           my $releases = $self->client->release($filter);
69              
70 0           return $releases;
71             }
72              
73             sub dist2latest_release {
74 0     0 1   my $self = shift;
75 0           my $dist_name = _get_dist_name(shift);
76              
77 0           my $filter = {
78             all => [
79             { distribution => $dist_name },
80             { status => "latest" }
81             ]
82             };
83              
84 0           my $release = $self->client->release($filter);
85              
86 0 0         return ( $release->total == 1 ? $release->next : undef );
87             }
88              
89             sub dist2favorite_count {
90 0     0 1   my $self = shift;
91 0           my $dist_name = _get_dist_name(shift);
92              
93 0           my $filter = { distribution => $dist_name };
94              
95 0           my $favorite = $self->client->favorite($filter);
96              
97 0 0         return ( ref $favorite ? $favorite->total : undef );
98             }
99              
100              
101             sub _get_dist_name {
102 0     0     my $val = shift;
103 0 0         ref($val) eq 'MetaCPAN::Client::Distribution' and return $val->name;
104 0 0         !ref($val) and return $val;
105 0           croak "invalid distribution name";
106             }
107              
108             1;
109              
110             =head1 NAME
111              
112             MetaCPAN::Helper - a MetaCPAN client that provides some high-level helper functions
113              
114             =head1 SYNOPSIS
115              
116             use MetaCPAN::Helper;
117              
118             my $helper = MetaCPAN::Helper->new();
119             my $module = 'MetaCPAN::Client';
120             my $distname = $helper->module2dist($module);
121             print "$module is in dist '$distname'\n";
122              
123             =head1 DESCRIPTION
124              
125             This module is a helper class built on top of L,
126             providing methods which provide simple high-level functions for answering
127             common "CPAN lookup questions".
128              
129             B: this is an early release, and the interface is likely to change.
130             Feedback on the interface is very welcome.
131              
132             You could just use L directly yourself,
133             which might make sense in a larger application.
134             This class is aimed at people writing smaller one-off scripts.
135              
136             =head1 METHODS
137              
138             =head2 module2dist( $MODULE_NAME | $MODULE_OBJ )
139              
140             Takes the name of a module or a L object,
141             and returns the name of the distribution which
142             I contains that module, according to the MetaCPAN API.
143              
144             At the moment this will ignore any developer releases,
145             and take the latest non-developer release of the module.
146              
147             If the distribution name in the dist's metadata doesn't match the
148             name produced by L, then be aware that this method
149             returns the name according to C.
150             This doesn't happen very often (less than 0.5% of CPAN distributions).
151              
152             =head release2repo( $RELEASE_NAME | $RELEASE_OBJ )
153              
154             Takes the name of a release or a L object,
155             and returns the repo URL string or undef if not found.
156              
157             =head dist2repo( $DIST_NAME | $DIST_OBJ )
158              
159             Takes the name of a distribution or a L object,
160             and returns the repo URL string or undef if not found, of its latest release.
161              
162             =head2 dist2releases( $DIST_NAME | $DIST_OBJ )
163              
164             Takes the name of a distribution or a L object,
165             and returns the L iterator of all releases
166             (as L objects)
167             associated with that distribution.
168              
169             =head2 dist2latest_release( $DIST_NAME | $DIST_OBJ )
170              
171             Takes the name of a distribution or a L object,
172             and returns the L
173             object of the "latest" release of that distribution.
174              
175             =head2 dist2favorite_count( $DIST_NAME | $DIST_OBJ )
176              
177             Takes the name of a distribution or a L object,
178             and returns the favorites count for that distribution.
179              
180             =head1 SEE ALSO
181              
182             L - the definitive client for querying L.
183              
184             =head1 REPOSITORY
185              
186             L
187              
188             =head1 CONTRIBUTORS
189              
190             =over 4
191              
192             =item *
193              
194             L
195              
196             =item *
197              
198             L
199              
200             =back
201              
202             =head1 COPYRIGHT AND LICENSE
203              
204             This software is copyright (c) 2015 the MetaCPAN project.
205              
206             This is free software; you can redistribute it and/or modify it under
207             the same terms as the Perl 5 programming language system itself.
208