File Coverage

blib/lib/MetaCPAN/Helper.pm
Criterion Covered Total %
statement 15 33 45.4
branch 0 6 0.0
condition 0 12 0.0
subroutine 5 8 62.5
pod 3 3 100.0
total 23 62 37.1


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