Refactoring NCIP.pm slightly, dealing with namespaces more elegantly
authorChris Cormack <chrisc@catalyst.net.nz>
Wed, 18 Sep 2013 22:34:10 +0000 (10:34 +1200)
committerChris Cormack <chrisc@catalyst.net.nz>
Wed, 18 Sep 2013 22:34:10 +0000 (10:34 +1200)
lib/NCIP.pm
t/NCIP.t
t/sample_data/LookupItem.xml

index c5101f7..cd74e5d 100644 (file)
@@ -7,6 +7,7 @@ use Try::Tiny;
 use base qw(Class::Accessor);
 
 our $VERSION = '0.01';
+our $nsURI = 'http://www.niso.org/2008/ncip';
 
 =head1 NAME
   
@@ -97,26 +98,20 @@ sub validate {
 sub parse_request {
     my $self  = shift;
     my $dom   = shift;
-    my $nodes = $dom->findnodes('/*');
-    if ( $nodes->[0]->nodeName() ne 'ns1:NCIPMessage' ) {
-
-        # we don't have a valid ncip message
-        # bail out
-        warn "bad xml";
-    }
-    else {
+    my $nodes = $dom->getElementsByTagNameNS($nsURI,'NCIPMessage');
+    if ($nodes){
         my @childnodes = $nodes->[0]->childNodes();
-
-        # the second child should be the type of request
-        if ( $childnodes[1] && $childnodes[1]->nodeName =~ /ns1\:(.*)/ ) {
-            return $1;
+        if ($childnodes[1]){
+            return $childnodes[1]->localname();
         }
         else {
-            # just while developing return not found
-            return ('Not_found');
+            return "unknown";
         }
     }
-
+    else {
+        warn "Invalid XML";
+        return 0;
+    }
     return 0;
 }
 
index d2dbe81..43a2090 100644 (file)
--- a/t/NCIP.t
+++ b/t/NCIP.t
@@ -19,7 +19,7 @@ use strict;
 use warnings;
 use File::Slurp;
 
-use Test::More tests => 7;    # last test to print
+use Test::More tests => 9;    # last test to print
 
 use lib 'lib';
 
@@ -49,5 +49,9 @@ ok( $ncip->handle_initiation($xml),     'Good XML' );
 
 my $lookupitem = read_file('t/sample_data/LookupItem.xml');
 
-ok( my $response = $ncip->process_request($lookupitem), 'Try looking up an item');
+ok( $response = $ncip->process_request($lookupitem), 'Try looking up an item');
 is ($response, 'LookupItem', 'We got lookupitem');
+
+$lookupitem = read_file('t/sample_data/LookupItemWithExampleItemIdentifierType.xml');
+ok( $response = $ncip->process_request($lookupitem), 'Try looking up an item, with agency');
+is ($response, 'LookupItem', 'We got lookupitem with agency');
index 02c94f5..5795a16 100644 (file)
@@ -1,29 +1,15 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-
 <ns1:NCIPMessage xmlns:ns1="http://www.niso.org/2008/ncip" ns1:version="http://www.niso.org/schemas/ncip/v2_0/imp1/xsd/ncip_v2_0.xsd">
-
 <ns1:LookupItem>
-
 <ns1:ItemId>
-
 <ns1:ItemIdentifierValue>3110888</ns1:ItemIdentifierValue>
-
 </ns1:ItemId>
-
 <ns1:ItemElementType ns1:Scheme="http://www.niso.org/ncip/v1_0/schemes/itemelementtype/itemelementtype.scm">Bibliographic Description</ns1:ItemElementType>
-
 <ns1:ItemElementType ns1:Scheme="http://www.niso.org/ncip/v1_0/schemes/itemelementtype/itemelementtype.scm">Circulation Status</ns1:ItemElementType>
-
 <ns1:ItemElementType ns1:Scheme="http://www.niso.org/ncip/v1_0/schemes/itemelementtype/itemelementtype.scm">Electronic Resource</ns1:ItemElementType>
-
 <ns1:ItemElementType ns1:Scheme="http://www.niso.org/ncip/v1_0/schemes/itemelementtype/itemelementtype.scm">Hold Queue Length</ns1:ItemElementType>
-
 <ns1:ItemElementType ns1:Scheme="http://www.niso.org/ncip/v1_0/schemes/itemelementtype/itemelementtype.scm">Item Description</ns1:ItemElementType>
-
 <ns1:ItemElementType ns1:Scheme="http://www.niso.org/ncip/v1_0/schemes/itemelementtype/itemelementtype.scm">Item Use Restriction Type</ns1:ItemElementType>
-
 <ns1:ItemElementType ns1:Scheme="http://www.niso.org/ncip/v1_0/schemes/itemelementtype/itemelementtype.scm">Location</ns1:ItemElementType>
-
 </ns1:LookupItem>
-
 </ns1:NCIPMessage>