Skip to content
This repository has been archived by the owner on Apr 12, 2020. It is now read-only.

Commit

Permalink
Clean up the API a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
dgl committed Jul 26, 2012
1 parent 764844b commit f83194a
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions lib/WWW/CPANGrep.pm
Expand Up @@ -26,13 +26,7 @@ sub dispatch_request {
$limit ||= 100;
my $r = $self->_search($q);

return [ 200, ['Content-type' => 'application/json' ],
[ to_json {
count => $r->{count},
duration => $r->{duration},
results => [grep defined, @{$r->{results}}[0 .. $limit]]
}, { pretty => 1 } ]
];
_format_api($r, $limit);
}
},
sub (!/api) {
Expand Down Expand Up @@ -205,6 +199,37 @@ sub render_response {
return $output->select('.pagination')->replace_content(\$pager->html);
}

sub _format_api {
my($r, $limit) = @_;

my @results = grep defined, @{$r->{results}}[0 .. $limit];

# Clean up the results a little
# TODO: Can we just clean up the internal data structure to just match this?
for my $result(@results) {
for my $file(@{$result->{files}}) {
for my $file_result(@{$file->{results}}) {
delete $file_result->{snippet};
delete $file_result->{zset};
# The file thing is duplication, although size could be useful so move
# up a level.
my $inner_file = delete $file_result->{file};
$file->{size} = $inner_file->{size};
}
}
}

return [200,
['Content-type' => 'application/json' ],
[ to_json {
count => $r->{count},
duration => $r->{duration},
results => \@results,
}, { pretty => 1 }
]
];
}

sub _render_snippet {
my($excerpt) = @_;
# XXX: Find some better code for this.
Expand Down

0 comments on commit f83194a

Please sign in to comment.