From b83c1638f2b009bac805c41babf7ad829c168fbb Mon Sep 17 00:00:00 2001 From: Lebbeous Fogle-Weekley Date: Thu, 19 Jan 2012 16:50:19 -0500 Subject: [PATCH] silly perfectionism. disregard. Signed-off-by: Lebbeous Fogle-Weekley --- .../src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm | 69 ++++++++++++---------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm index 441025ffca..21144eaf70 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm @@ -14,6 +14,41 @@ use CGI qw(:all -utf8); 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; @@ -103,41 +138,15 @@ sub suggestions_to_json { 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); } } -- 2.11.0