use OpenSRF::Utils::JSON;
use OpenILS::Utils::CStoreEditor qw/:funcs/;
+# BEGIN package globals
+
+# We'll probably never need this fanciness for autosuggest, but
+# you can add handlers for different requested content-types here, and
+# you can weight them to control what matches requests for things like
+# 'application/*'
+
+my $_output_handler_dispatch = {
+ "application/xml" => {
+ "prio" => 0,
+ "code" => sub {
+ my ($r, $data) = @_;
+ $r->content_type("application/xml; charset=utf-8");
+ print suggestions_to_xml($data);
+ return Apache2::Const::OK;
+ }
+ },
+ "application/json" => {
+ "prio" => 1,
+ "code" => sub {
+ my ($r, $data) = @_;
+ $r->content_type("application/json; charset=utf-8");
+ print suggestions_to_json($data);
+ return Apache2::Const::OK;
+ }
+ }
+};
+
+my @_output_handler_types = sort {
+ $_output_handler_dispatch->{$a}->{prio} <=>
+ $_output_handler_dispatch->{$b}->{prio}
+} keys %$_output_handler_dispatch;
+
+# END package globals
+
sub prepare_for_tsquery {
my ($str) = shift;
sub output_handler {
my ($r, $data) = @_;
- # We'll probably never need this fanciness for autosuggest, but
- # you can add handlers for different requested content-types here, and
- # you can weight them to control what matches requests for things like
- # 'application/*'
-
- my $dispatch = {
- "application/xml" => {
- "prio" => 0,
- "code" => sub {
- $r->content_type("application/xml; charset=utf-8");
- print suggestions_to_xml($data);
- return Apache2::Const::OK;
- }
- },
- "application/json" => {
- "prio" => 1,
- "code" => sub {
- $r->content_type("application/json; charset=utf-8");
- print suggestions_to_json($data);
- return Apache2::Const::OK;
- }
- }
- };
-
foreach my $media_range (split /,/, $r->headers_in->{Accept}) {
$media_range =~ s/;.+$//; # keep type, subtype. lose parameters.
- my ($match) = grep { Text::Glob::match_glob($media_range, $_) } (
- sort {
- $dispatch->{$a}->{prio} <=> $dispatch->{$b}->{prio}
- } keys %$dispatch
- );
+ my ($match) = grep {
+ Text::Glob::match_glob($media_range, $_)
+ } @_output_handler_types;
if ($match) {
- return $dispatch->{$match}{code}->($data);
+ return $_output_handler_dispatch->{$match}{code}->($r, $data);
}
}