silly perfectionism. disregard.
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Thu, 19 Jan 2012 21:50:19 +0000 (16:50 -0500)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Mon, 23 Jan 2012 17:26:59 +0000 (12:26 -0500)
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm

index 441025f..21144ea 100644 (file)
@@ -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);
         }
     }