The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
use 5.008001;
use strict;
use warnings;

package CPAN::Common::Index::MetaDB;
# ABSTRACT: Search index via CPAN MetaDB
our $VERSION = '0.003'; # VERSION

use parent 'CPAN::Common::Index';

use Carp;
use CPAN::Meta::YAML;
use HTTP::Tiny;


sub attributes {
    return { uri => "http://cpanmetadb.plackperl.org/v1.0/" };
}

sub validate_attributes {
    my ($self) = @_;

    # ensure URI ends in '/'
    my $uri = $self->uri;
    $uri =~ s{/?$}{/};
    $self->uri($uri);

    return 1;
}

sub search_packages {
    my ( $self, $args ) = @_;
    Carp::croak("Argument to search_packages must be hash reference")
      unless ref $args eq 'HASH';

    # only support direct package query
    return
      unless keys %$args == 1 && exists $args->{package} && ref $args->{package} eq '';

    my $mod = $args->{package};
    my $res = HTTP::Tiny->new->get( $self->uri . "package/$mod" );
    return unless $res->{success};

    if ( my $yaml = CPAN::Meta::YAML->read_string( $res->{content} ) ) {
        my $meta = $yaml->[0];
        if ( $meta && $meta->{distfile} ) {
            my $file = $meta->{distfile};
            $file =~ s{^./../}{}; # strip leading
            return {
                package => $mod,
                version => $meta->{version},
                uri     => "cpan:///distfile/$file",
            };
        }
    }

    return;
}

sub index_age { return time };    # pretend always current

sub search_authors { return };    # not supported

__PACKAGE__->_build_accessors;


# vim: ts=4 sts=4 sw=4 et:

__END__

=pod

=encoding utf-8

=head1 NAME

CPAN::Common::Index::MetaDB - Search index via CPAN MetaDB

=head1 VERSION

version 0.003

=head1 SYNOPSIS

  use CPAN::Common::Index::MetaDB;

  $index = CPAN::Common::Index::MetaDB->new;

=head1 DESCRIPTION

This module implements a CPAN::Common::Index that searches for packages against
the same CPAN MetaDB API used by L<cpanminus>.

There is no support for advanced package queries or searching authors.  It just
takes a package name and returns the corresponding version and distribution.

=head1 ATTRIBUTES

=head2 uri

A URI for the endpoint of a CPAN MetaDB server. The
default is L<http://cpanmetadb.plackperl.org/v1.0/>.

=for Pod::Coverage attributes validate_attributes search_packages search_authors

=head1 AUTHOR

David Golden <dagolden@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2013 by David Golden.

This is free software, licensed under:

  The Apache License, Version 2.0, January 2004

=cut