From f438d2b36253e97f1f6704d75caf3ef0b53c985b Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Thu, 19 Sep 2013 10:34:10 +1200 Subject: [PATCH] Refactoring NCIP.pm slightly, dealing with namespaces more elegantly --- lib/NCIP.pm | 25 ++++++++++--------------- t/NCIP.t | 8 ++++++-- t/sample_data/LookupItem.xml | 14 -------------- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/lib/NCIP.pm b/lib/NCIP.pm index c5101f7..cd74e5d 100644 --- a/lib/NCIP.pm +++ b/lib/NCIP.pm @@ -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; } diff --git a/t/NCIP.t b/t/NCIP.t index d2dbe81..43a2090 100644 --- 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'); diff --git a/t/sample_data/LookupItem.xml b/t/sample_data/LookupItem.xml index 02c94f5..5795a16 100644 --- a/t/sample_data/LookupItem.xml +++ b/t/sample_data/LookupItem.xml @@ -1,29 +1,15 @@ - - - - 3110888 - - Bibliographic Description - Circulation Status - Electronic Resource - Hold Queue Length - Item Description - Item Use Restriction Type - Location - - -- 2.43.2