Skip to content

Commit

Permalink
Added a controller for File::download_url and exposed the matching mo…
Browse files Browse the repository at this point in the history
…dule

version.  Tests are currently skipped
  • Loading branch information
clintongormley committed Apr 18, 2015
1 parent f66ae99 commit 2f240c9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/MetaCPAN/Document/File.pm
Expand Up @@ -973,8 +973,9 @@ sub find_download_url {
# filters to be applied to the nested modules
my $module_f = {
nested => {
path => 'module',
filter => {
path => 'module',
inner_hits => { _source => "version" },
filter => {
bool => {
must => [
{ term => { "module.authorized" => \1 } },
Expand All @@ -999,7 +1000,7 @@ sub find_download_url {
"module.version_numified" => {
mode => 'max',
order => 'desc',
nested_filter => $module_f
nested_filter => $module_f->{nested}{filter}
}
},
{ date => { order => 'desc' } }
Expand Down Expand Up @@ -1033,7 +1034,7 @@ sub find_download_url {
}

return $self->size(1)->query($query)
->source( 'download_url', 'date', 'status' )->sort( \@sort );
->source( [ 'download_url', 'date', 'status' ] )->sort( \@sort );

}

Expand Down
30 changes: 30 additions & 0 deletions lib/MetaCPAN/Server/Controller/Search/DownloadURL.pm
@@ -0,0 +1,30 @@
package MetaCPAN::Server::Controller::Search::DownloadURL;

use strict;
use warnings;

use Moose;

BEGIN { extends 'MetaCPAN::Server::Controller' }

with 'MetaCPAN::Server::Role::JSONP';

has '+type' => ( default => 'file' );

sub get : Local : Path('/download_url') : Args(1) {
my ( $self, $c, $module ) = @_;
my $args = $c->req->params;

my $model = $self->model($c);
my $res = $model->find_download_url( $module, $args )->raw->all;
my $hit = $res->{hits}{hits}[0]
or return $c->detach( '/not_found', [] );

$c->stash(
{ %{ $hit->{_source} },
%{ $hit->{inner_hits}{module}{hits}{hits}[0]{_source} }
}
);
}

1;
49 changes: 49 additions & 0 deletions t/server/controller/search/download_url.t
@@ -0,0 +1,49 @@
use strict;
use warnings;

use lib 't/lib';
use MetaCPAN::Server::Test;
use MetaCPAN::TestHelpers;
use Test::More skip_all =>
"Need to add CPAN::Test::Dummy::Perl5::VersionBump to CPAN::Faker and write tests";

use Log::Any::Adapter ( 'File', 'out' );

test_psgi app, sub {
my $cb = shift;

# test ES script using doc['blah'] value
{
ok( my $res = $cb->(
GET
'/download_url/CPAN::Test::Dummy::Perl5::VersionBump::Decrease'
),
'GET'
);
my $json = decode_json_ok($res);

use Data::Dump qw(pp);
print STDERR ( pp( scalar $json ), "\n" );

# my $got
# = [ map { $_->{_source}{documentation} }
# @{ $json->{hits}{hits} } ];
#
# is_deeply $got, [
# qw(
# Multiple::Modules
# Multiple::Modules::A
# Multiple::Modules::B
# Multiple::Modules::RDeps
# Multiple::Modules::Tester
# Multiple::Modules::RDeps::A
# Multiple::Modules::RDeps::Deprecated
# )
# ],
# 'results are sorted by module name length'
# or diag( Test::More::explain($got) );
# }
};
};

done_testing;

0 comments on commit 2f240c9

Please sign in to comment.