Navigation Menu

Skip to content

Commit

Permalink
add support for raw output mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyn committed May 12, 2014
1 parent 637c3fa commit 30c4165
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 9 additions & 0 deletions examples/raw_output.pl
@@ -0,0 +1,9 @@
use strict;
use warnings;
use Data::Printer;
use MetaCPAN::Client;

my $author =
MetaCPAN::Client->new(raw_output=>1)->author('XSAWYERX');

p $author;
21 changes: 18 additions & 3 deletions lib/MetaCPAN/Client.pm
Expand Up @@ -21,6 +21,11 @@ has request => (
handles => [qw<ua fetch post ssearch>],
);

has raw_output => (
is => 'ro',
required => 0,
);

my @supported_searches = qw<
author distribution favorite module rating release
>;
Expand Down Expand Up @@ -143,6 +148,8 @@ sub _get {
ref $response eq 'HASH'
or croak sprintf( 'Failed to fetch %s (%s)', ucfirst($type), $arg );

$self->raw_output and return $response;

my $class = 'MetaCPAN::Client::' . ucfirst($type);
return $class->new_from_request($response);
}
Expand All @@ -166,6 +173,8 @@ sub _search {

my $scroller = $self->ssearch($type, $args, $params);

$self->raw_output and return $scroller;

return MetaCPAN::Client::ResultSet->new(
scroller => $scroller,
type => $type,
Expand All @@ -191,10 +200,10 @@ sub _reverse_deps {
my $self = shift;
my $dist = shift;

my $res;
my $response;

eval {
$res = $self->fetch(
my $res = $self->fetch(
'/search/reverse_dependencies/'.$dist,
{
query => { match_all => {} },
Expand All @@ -203,14 +212,20 @@ sub _reverse_deps {
}
);

$response = $res->{'hits'}{'hits'};

1;

} or do {
warn $@;
return [];
};

$self->raw_output and return $response;

return +[
map { MetaCPAN::Client::Release->new_from_request($_->{'_source'}) }
@{ $res->{'hits'}{'hits'} }
@{ $response }
];
}

Expand Down

0 comments on commit 30c4165

Please sign in to comment.