Skip to content

Commit

Permalink
Refactoring NCIP.pm slightly, dealing with namespaces more elegantly
Browse files Browse the repository at this point in the history
  • Loading branch information
ranginui committed Sep 18, 2013
1 parent 8020a2a commit f438d2b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
25 changes: 10 additions & 15 deletions lib/NCIP.pm
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 6 additions & 2 deletions t/NCIP.t
Expand Up @@ -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';

Expand Down Expand Up @@ -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');
14 changes: 0 additions & 14 deletions t/sample_data/LookupItem.xml
@@ -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>

0 comments on commit f438d2b

Please sign in to comment.