Menlo/Index/MetaDB.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 132 CC 15 SCAR 47.0
CC
15
Cov
0%
search_packages
210.0
BUILD
6.0
index_age
2.0
CRAP 110.1
1 use 5.008001;
2 use strict;
3 use warnings;
4  
5 package Menlo::Index::MetaDB;
6 # ABSTRACT: Search index via CPAN MetaDB
7  
8 our $VERSION = "1.9019";
9  
10 use parent 'CPAN::Common::Index';
11  
12 use Class::Tiny qw/uri/;
13  
14 use Carp;
15 use CPAN::Meta::YAML;
16 use CPAN::Meta::Requirements;
17 use HTTP::Tiny;
18  
19 sub BUILD {
20 my $self = shift;
21 my $uri = $self->uri;
22 $uri = "http://cpanmetadb.plackperl.org/v1.0/"
23 unless defined $uri;
24 # ensure URI ends in '/'
25 $uri =~ s{/?$}{/};
26 $self->uri($uri);
27 return;
28 }
29  
30 sub search_packages {
31 my ( $self, $args ) = @_;
32 Carp::croak("Argument to search_packages must be hash reference")
33 unless ref $args eq 'HASH';
34  
35 return
36 unless exists $args->{package} && ref $args->{package} eq '';
37  
38 my $mod = $args->{package};
39  
40 if ($args->{version} || $args->{version_range}) {
41 my $res = HTTP::Tiny->new->get( $self->uri . "history/$mod" );
42 return unless $res->{success};
43  
44 my $range = defined $args->{version} ? "== $args->{version}" : $args->{version_range};
45 my $reqs = CPAN::Meta::Requirements->from_string_hash({ $mod => $range });
46  
47 my @found;
48 for my $line ( split /\r?\n/, $res->{content} ) {
49 if ($line =~ /^$mod\s+(\S+)\s+(\S+)$/) {
50 push @found, {
51 version => $1,
52 version_o => version::->parse($1),
53 distfile => $2,
54 };
55 }
56 }
57  
58 return unless @found;
59 $found[-1]->{latest} = 1;
60  
61 my $match;
62 for my $try (sort { $b->{version_o} <=> $a->{version_o} } @found) {
63 if ($reqs->accepts_module($mod => $try->{version_o})) {
64 $match = $try, last;
65 }
66 }
67  
68 if ($match) {
69 my $file = $match->{distfile};
70 $file =~ s{^./../}{}; # strip leading
71 return {
72 package => $mod,
73 version => $match->{version},
74 uri => "cpan:///distfile/$file",
75 ($match->{latest} ? () :
76 (download_uri => "http://backpan.perl.org/authors/id/$match->{distfile}")),
77 };
78 }
79 } else {
80 my $res = HTTP::Tiny->new->get( $self->uri . "package/$mod" );
81 return unless $res->{success};
82  
83 if ( my $yaml = CPAN::Meta::YAML->read_string( $res->{content} ) ) {
84 my $meta = $yaml->[0];
85 if ( $meta && $meta->{distfile} ) {
86 my $file = $meta->{distfile};
87 $file =~ s{^./../}{}; # strip leading
88 return {
89 package => $mod,
90 version => $meta->{version},
91 uri => "cpan:///distfile/$file",
92 };
93 }
94 }
95 }
96  
97 return;
98 }
99  
100 sub index_age { return time }; # pretend always current
101  
102 sub search_authors { return }; # not supported
103  
104 1;
105  
106 =for Pod::Coverage attributes validate_attributes search_packages search_authors BUILD
107  
108 =head1 SYNOPSIS
109  
110 use CPAN::Common::Index::MetaDB;
111  
112 $index = CPAN::Common::Index::MetaDB->new;
113  
114 $index->search_packages({ package => "Moose" });
115 $index->search_packages({ package => "Moose", version_range => ">= 2.0" });
116  
117 =head1 DESCRIPTION
118  
119 This module implements a CPAN::Common::Index that searches for packages against
120 the same CPAN MetaDB API used by L<cpanminus>.
121  
122 There is no support for advanced package queries or searching authors. It just
123 takes a package name and returns the corresponding version and distribution.
124  
125 =cut
126  
127 # vim: ts=4 sts=4 sw=4 et: