File Coverage

blib/lib/MetaCPAN/Helper.pm
Criterion Covered Total %
statement 15 39 38.4
branch 0 10 0.0
condition 0 6 0.0
subroutine 5 9 55.5
pod 3 3 100.0
total 23 67 34.3


line stmt bran cond sub pod time code
1             package MetaCPAN::Helper;
2             $MetaCPAN::Helper::VERSION = '0.03';
3 1     1   743 use 5.006;
  1         2  
  1         34  
4 1     1   4 use strict;
  1         1  
  1         31  
5 1     1   13 use warnings;
  1         1  
  1         25  
6 1     1   1783 use Moo;
  1         11958  
  1         4  
7 1     1   1200 use Carp;
  1         2  
  1         350  
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 dist2releases {
39 0     0 1   my $self = shift;
40 0           my $dist_name = _get_dist_name(shift);
41              
42 0           my $filter = { distribution => $dist_name };
43 0           my $releases = $self->client->release($filter);
44              
45 0           return $releases;
46             }
47              
48             sub dist2latest_release {
49 0     0 1   my $self = shift;
50 0           my $dist_name = _get_dist_name(shift);
51              
52 0           my $filter = {
53             all => [
54             { distribution => $dist_name },
55             { status => "latest" }
56             ]
57             };
58              
59 0           my $release = $self->client->release($filter);
60              
61 0 0         return ( $release->total == 1 ? $release->next : undef );
62             }
63              
64             sub _get_dist_name {
65 0     0     my $val = shift;
66 0 0         ref($val) eq 'MetaCPAN::Client::Distribution' and return $val->name;
67 0 0         !ref($val) and return $val;
68 0           croak "invalid distribution name";
69             }
70              
71             1;
72              
73             =head1 NAME
74              
75             MetaCPAN::Helper - a MetaCPAN client that provides some high-level helper functions
76              
77             =head1 SYNOPSIS
78              
79             use MetaCPAN::Helper;
80              
81             my $helper = MetaCPAN::Helper->new();
82             my $module = 'MetaCPAN::Client';
83             my $distname = $helper->module2dist($module);
84             print "$module is in dist '$distname'\n";
85              
86             =head1 DESCRIPTION
87              
88             This module is a helper class built on top of L,
89             providing methods which provide simple high-level functions for answering
90             common "CPAN lookup questions".
91              
92             B: this is an early release, and the interface is likely to change.
93             Feedback on the interface is very welcome.
94              
95             You could just use L directly yourself,
96             which might make sense in a larger application.
97             This class is aimed at people writing smaller one-off scripts.
98              
99             =head1 METHODS
100              
101             =head2 module2dist( $MODULE_NAME | $MODULE_OBJ )
102              
103             Takes the name of a module or a L object,
104             and returns the name of the distribution which
105             I contains that module, according to the MetaCPAN API.
106              
107             At the moment this will ignore any developer releases,
108             and take the latest non-developer release of the module.
109              
110             If the distribution name in the dist's metadata doesn't match the
111             name produced by L, then be aware that this method
112             returns the name according to C.
113             This doesn't happen very often (less than 0.5% of CPAN distributions).
114              
115             =head2 dist2releases( $DIST_NAME | $DIST_OBJ )
116              
117             Takes the name of a distribution or a L object,
118             and returns the L iterator of all releases
119             (as L objects)
120             associated with that distribution.
121              
122             =head2 dist2latest_release( $DIST_NAME | $DIST_OBJ )
123              
124             Takes the name of a distribution or a L object,
125             and returns the L
126             object of the "latest" release of that distribution.
127              
128             =head1 SEE ALSO
129              
130             L - the definitive client for querying L.
131              
132             =head1 REPOSITORY
133              
134             L
135              
136             =head1 CONTRIBUTORS
137              
138             =over 4
139              
140             =item *
141              
142             L
143              
144             =item *
145              
146             L
147              
148             =back
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             This software is copyright (c) 2015 the MetaCPAN project.
153              
154             This is free software; you can redistribute it and/or modify it under
155             the same terms as the Perl 5 programming language system itself.
156