Shifting the finding user elements in the XML to its own subroutine
authorChris Cormack <chrisc@catalyst.net.nz>
Tue, 25 Feb 2014 21:09:16 +0000 (10:09 +1300)
committerChris Cormack <chrisc@catalyst.net.nz>
Tue, 25 Feb 2014 21:09:16 +0000 (10:09 +1300)
lib/NCIP/Handler.pm
lib/NCIP/Handler/LookupUser.pm

index 713967c..58b1d1d 100644 (file)
@@ -16,11 +16,51 @@ package NCIP::Handler;
 #     REVISION: ---
 #===============================================================================
 
+# Copyright 2014 Catalyst IT <chrisc@catalyst.net.nz>
+
+# This file is part of NCIPServer
+#
+# NCIPServer is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# NCIPServer is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with NCIPServer; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+=head1 NAME
+
+    NCIP::Handler
+
+=head1 SYNOPSIS
+
+    use NCIP::Handler;
+    my $handler = NCIP::Handler->new( { namespace    => $namespace,
+                                        type         => $request_type,
+                                        ils          => $ils,
+                                        template_dir => $templates
+                                       } );
+
+=head1 FUNCTIONS
+=cut
+
 use Modern::Perl;
 use Object::Tiny qw{ type namespace ils templates };
 use Module::Load;
 use Template;
 
+=head2 new()
+
+    Set up a new handler object, this will actually create one of the request type
+    eg NCIP::Handler::LookupUser
+
+=cut
+
 sub new {
     my $class    = shift;
     my $params   = shift;
@@ -35,6 +75,27 @@ sub new {
     return $self;
 }
 
+=head2 get_user_elements($xml)
+
+    When passed an xml dom, this will find the user elements and pass convert them into an arrayref
+
+=cut
+
+sub get_user_elements {
+    my $self   = shift;
+    my $xmldoc = shift;
+    my $xpc    = XML::LibXML::XPathContext->new;
+    $xpc->registerNs( 'ns', $self->namespace() );
+
+    my $root = $xmldoc->documentElement();
+    my @elements =
+      $xpc->findnodes( 'ns:LookupUser/UserElementType/Value', $root );
+    unless ( $elements[0] ) {
+        @elements = $xpc->findnodes( 'ns:LookupUser/UserElementType', $root );
+    }
+    return \@elements;
+}
+
 sub render_output {
     my $self         = shift;
     my $templatename = shift;
index bfa3993..24f44cd 100644 (file)
@@ -72,15 +72,12 @@ sub handle {
             my $output = $self->render_output( 'problem.tt', $vars );
             return $output;
         }
-
-        my $root = $xmldoc->documentElement();
-        my @elements =
-          $xpc->findnodes( 'ns:LookupUser/UserElementType/Value', $root );
+        my $elements = $self->get_user_elements($xmldoc);
 
         #set up the variables for our template
         my $vars;
         $vars->{'messagetype'} = 'LookupUserResponse';
-        $vars->{'elements'}    = \@elements;
+        $vars->{'elements'}    = $elements;
         $vars->{'user'}        = $user;
         my $output = $self->render_output( 'response.tt', $vars );
         return $output;