Continuing work on NCIP.pm
authorChris Cormack <chrisc@catalyst.net.nz>
Wed, 18 Sep 2013 03:07:09 +0000 (15:07 +1200)
committerChris Cormack <chrisc@catalyst.net.nz>
Wed, 18 Sep 2013 03:07:09 +0000 (15:07 +1200)
We can now recognised (in theory) the type of request we are getting.

Have a basic test of a LookupItem message

Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
lib/NCIP.pm
t/NCIP.t

index b8bc2ce..c5101f7 100644 (file)
@@ -48,10 +48,11 @@ sub process_request {
       # We have invalid xml, or we can't figure out what kind of request this is
       # Handle error here
     }
-    my $response =
-"<HTML> <HEAD> <TITLE>Hello There</TITLE> </HEAD> <BODY> <H1>Hello You Big JERK!</H1> Who would take this book seriously if the first eaxample didn't say \"hello world\"?  </BODY> </HTML>";
 
-    return $response;
+#my $response = "<HTML> <HEAD> <TITLE>Hello There</TITLE> </HEAD> <BODY> <H1>Hello You Big JERK!</H1> Who would take this book seriously if the first eaxample didn't say \"hello world\"?  </BODY> </HTML>";
+
+    #return $response;
+    return $request_type;
 }
 
 =head2 handle_initiation
@@ -69,11 +70,54 @@ sub handle_initiation {
         warn "Invalid xml, caught error: $_";
     };
     if ($dom) {
-        return ('lookup_item');
+
+        # should check validity with validate at this point
+        my $request_type = $self->parse_request($dom);
+        return $request_type;
     }
     else {
         return;
     }
 }
 
+sub validate {
+
+    # this should perhaps be in it's own module
+    my $self     = shift;
+    my $dom      = shift;
+    my $validity = $dom->is_valid();
+
+    # we could validate against the dtd here, might be good?
+    # my $dtd = XML::LibXML::Dtd->parse_string($dtd_str);
+    # my $validity = $dom->is_valid($dtd);
+    # perhaps we could check the ncip version and validate that too
+    return $validity;
+}
+
+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 @childnodes = $nodes->[0]->childNodes();
+
+        # the second child should be the type of request
+        if ( $childnodes[1] && $childnodes[1]->nodeName =~ /ns1\:(.*)/ ) {
+            return $1;
+        }
+        else {
+            # just while developing return not found
+            return ('Not_found');
+        }
+    }
+
+    return 0;
+}
+
 1;
index 74422cb..d2dbe81 100644 (file)
--- a/t/NCIP.t
+++ b/t/NCIP.t
@@ -17,8 +17,9 @@
 
 use strict;
 use warnings;
+use File::Slurp;
 
-use Test::More tests => 5;    # last test to print
+use Test::More tests => 7;    # last test to print
 
 use lib 'lib';
 
@@ -26,8 +27,10 @@ use_ok('NCIP');
 ok( my $ncip = NCIP->new('t/config_sample'), 'Create new object' );
 
 my $xml = <<'EOT';
-<xml>
-</xml>
+<?xml version="1.0" encoding="UTF-8"?>
+<ns1:NCIPMessage
+  ns1:version="http://www.niso.org/schemas/ncip/v2_0/imp1/xsd/ncip_v2_0.xsd" xmlns:ns1="http://www.niso.org/2008/ncip">
+</ns1:NCIPMessage>
 EOT
 
 ok( my $response = $ncip->process_request($xml), 'Process a request' );
@@ -43,3 +46,8 @@ EOT
 # anyway
 ok( !$ncip->handle_initiation($xmlbad), 'Bad xml' );
 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');
+is ($response, 'LookupItem', 'We got lookupitem');