Menlo/Index/MetaCPAN.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 109 CC 10 SCAR 35.3
CC
10
Cov
0%
search_packages
90.0
BUILD
6.0
_parse_date
2.0
CRAP 34.0
1 use 5.008001;
2 use strict;
3 use warnings;
4  
5 package Menlo::Index::MetaCPAN;
6 # ABSTRACT: Search index via MetaCPAN
7 # VERSION
8  
9 use parent 'CPAN::Common::Index';
10  
11 use Class::Tiny qw/uri include_dev/;
12  
13 use Carp;
14 use HTTP::Tinyish;
15 use JSON::PP ();
16 use Time::Local ();
17  
18 sub BUILD {
19 my $self = shift;
20 my $uri = $self->uri;
21 $uri = "https://fastapi.metacpan.org/v1/download_url/"
22 unless defined $uri;
23 # ensure URI ends in '/'
24 $uri =~ s{/?$}{/};
25 $self->uri($uri);
26 return;
27 }
28  
29 sub search_packages {
30 my ( $self, $args ) = @_;
31 Carp::croak("Argument to search_packages must be hash reference")
32 unless ref $args eq 'HASH';
33  
34 my $range;
35 if ( $args->{version} ) {
36 $range = "== $args->{version}";
37 } elsif ( $args->{version_range} ) {
38 $range = $args->{version_range};
39 }
40 my %query = (
41 ($self->include_dev ? (dev => 1) : ()),
42 ($range ? (version => $range) : ()),
43 );
44 my $query = join "&", map { "$_=" . $self->_uri_escape($query{$_}) } sort keys %query;
45  
46 my $uri = $self->uri . $args->{package} . ($query ? "?$query" : "");
47 my $res = HTTP::Tinyish->new->get($uri);
48 return unless $res->{success};
49  
50 my $dist_meta = eval { JSON::PP::decode_json($res->{content}) };
51 if ($dist_meta && $dist_meta->{download_url}) {
52 (my $distfile = $dist_meta->{download_url}) =~ s!.+/authors/id/\w/\w\w/!!;
53  
54 return {
55 package => $args->{package},
56 version => $dist_meta->{version},
57 uri => "cpan:///distfile/$distfile",
58 download_uri => $self->_download_uri("http://cpan.metacpan.org", $distfile),
59 };
60 }
61  
62 return;
63 }
64  
65 sub _parse_date {
66 my($self, $date) = @_;
67 my @date = $date =~ /^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)$/;
68 Time::Local::timegm($date[5], $date[4], $date[3], $date[2], $date[1] - 1, $date[0] - 1900);
69 }
70  
71 sub _uri_escape {
72 my($self, $string) = @_;
73 $string =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
74 $string;
75 }
76  
77 sub _download_uri {
78 my($self, $base, $distfile) = @_;
79 join "/", $base, "authors/id", substr($distfile, 0, 1), substr($distfile, 0, 2), $distfile;
80 }
81  
82 sub index_age { return time } # pretend always current
83  
84 sub search_authors { return } # not supported
85  
86 1;
87  
88 =for Pod::Coverage attributes validate_attributes search_packages search_authors BUILD
89  
90 =head1 SYNOPSIS
91  
92 use CPAN::Common::Index::MetaCPAN;
93  
94 $index = CPAN::Common::Index::MetaCPAN->new({ include_dev => 1 });
95 $index->search_packages({ package => "Moose", version => "1.1" });
96 $index->search_packages({ package => "Moose", version_range => ">= 1.1, < 2" });
97  
98 =head1 DESCRIPTION
99  
100 This module implements a CPAN::Common::Index that searches for packages against
101 the MetaCPAN API.
102  
103 This backend supports searching modules with a version range (as
104 specified in L<CPAN::Meta::Spec>) which is translated into MetaCPAN
105 search query.
106  
107 There is also a support for I<dev> release search, by passing
108 C<include_dev> parameter to the index object.
109  
110 The result may include an optional field C<download_uri> which
111 suggests a specific mirror URL to download from, which can be
112 C<backpan.org> if the archive was deleted, or C<cpan.metacpan.org> if
113 the release date is within 1 day (because some mirrors might not have
114 synced it yet).
115  
116 There is no support for searching packages with a regular expression, nor searching authors.
117  
118 =cut
119  
120 # vim: ts=4 sts=4 sw=4 et: