Starting work on our first handler
authorChris Cormack <chrisc@catalyst.net.nz>
Wed, 18 Sep 2013 23:42:56 +0000 (11:42 +1200)
committerChris Cormack <chrisc@catalyst.net.nz>
Wed, 18 Sep 2013 23:42:56 +0000 (11:42 +1200)
lib/NCIP.pm
lib/NCIP/Handler.pm [new file with mode: 0644]
lib/NCIP/Handler/LookupItem.pm [new file with mode: 0644]
t/NCIP.t
t/NCIP_Handler.t [new file with mode: 0644]

index cd74e5d..8f9a3e8 100644 (file)
@@ -1,13 +1,14 @@
 package NCIP;
 use NCIP::Configuration;
+use NCIP::Handler;
 use Modern::Perl;
 use XML::LibXML;
 use Try::Tiny;
 
-use base qw(Class::Accessor);
+use Object::Tiny;
 
 our $VERSION = '0.01';
-our $nsURI = 'http://www.niso.org/2008/ncip';
+our $nsURI   = 'http://www.niso.org/2008/ncip';
 
 =head1 NAME
   
@@ -48,12 +49,17 @@ sub process_request {
 
       # We have invalid xml, or we can't figure out what kind of request this is
       # Handle error here
+        return;
+
+        #bail out for now
     }
 
 #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;
+    warn $request_type;
+    my $handler = NCIP::Handler->new($request_type);
+    return $handler->handle($xml);
 }
 
 =head2 handle_initiation
@@ -73,8 +79,21 @@ sub handle_initiation {
     if ($dom) {
 
         # should check validity with validate at this point
+        #        if ( $self->validate($dom) ) {
         my $request_type = $self->parse_request($dom);
-        return $request_type;
+
+        # do whatever we should do to initiate, then hand back request_type
+        if ($request_type) {
+            return $request_type;
+        }
+
+        #       }
+        #       else {
+        #            warn "Not valid xml";
+        # not valid throw error
+        #           return;
+        #       }
+
     }
     else {
         return;
@@ -98,14 +117,14 @@ sub validate {
 sub parse_request {
     my $self  = shift;
     my $dom   = shift;
-    my $nodes = $dom->getElementsByTagNameNS($nsURI,'NCIPMessage');
-    if ($nodes){
+    my $nodes = $dom->getElementsByTagNameNS( $nsURI, 'NCIPMessage' );
+    if ($nodes) {
         my @childnodes = $nodes->[0]->childNodes();
-        if ($childnodes[1]){
+        if ( $childnodes[1] ) {
             return $childnodes[1]->localname();
         }
         else {
-            return "unknown";
+            return;
         }
     }
     else {
diff --git a/lib/NCIP/Handler.pm b/lib/NCIP/Handler.pm
new file mode 100644 (file)
index 0000000..e9577c5
--- /dev/null
@@ -0,0 +1,33 @@
+package NCIP::Handler;
+#
+#===============================================================================
+#
+#         FILE: Hander.pm
+#
+#  DESCRIPTION: 
+#
+#        FILES: ---
+#         BUGS: ---
+#        NOTES: ---
+#       AUTHOR: Chris Cormack (rangi), chrisc@catalyst.net.nz
+# ORGANIZATION: Koha Development Team
+#      VERSION: 1.0
+#      CREATED: 19/09/13 10:43:14
+#     REVISION: ---
+#===============================================================================
+
+use Modern::Perl;
+use Object::Tiny qw{ type };
+
+
+use NCIP::Handler::LookupItem;
+
+sub new {
+    my $class = shift;
+    my $type = shift;
+    my $subclass = __PACKAGE__."::".$type;
+    my $self = bless { type => $type }, $subclass;
+    return $self;
+}
+
+1;
diff --git a/lib/NCIP/Handler/LookupItem.pm b/lib/NCIP/Handler/LookupItem.pm
new file mode 100644 (file)
index 0000000..81c192e
--- /dev/null
@@ -0,0 +1,30 @@
+package NCIP::Handler::LookupItem;
+
+#
+#===============================================================================
+#
+#         FILE: LookupItem.pm
+#
+#  DESCRIPTION: 
+#
+#        FILES: ---
+#         BUGS: ---
+#        NOTES: ---
+#       AUTHOR: Chris Cormack (rangi), chrisc@catalyst.net.nz
+# ORGANIZATION: Koha Development Team
+#      VERSION: 1.0
+#      CREATED: 19/09/13 10:52:44
+#     REVISION: ---
+#===============================================================================
+
+use Modern::Perl;
+
+use NCIP::Handler;
+our @ISA = qw(NCIP::Handler);
+
+sub handle {
+    my $self = shift;
+    return $self->type;
+}
+
+1;
index 43a2090..660302e 100644 (file)
--- a/t/NCIP.t
+++ b/t/NCIP.t
@@ -19,22 +19,13 @@ use strict;
 use warnings;
 use File::Slurp;
 
-use Test::More tests => 9;    # last test to print
+use Test::More tests => 7;    # last test to print
 
 use lib 'lib';
 
 use_ok('NCIP');
 ok( my $ncip = NCIP->new('t/config_sample'), 'Create new object' );
 
-my $xml = <<'EOT';
-<?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' );
-
 my $xmlbad = <<'EOT';
 <xml>
 this is bad
@@ -45,13 +36,17 @@ EOT
 # handle_initiation is called as part of the process_request, but best to test
 # 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( $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');
+ok( my $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/NCIP_Handler.t b/t/NCIP_Handler.t
new file mode 100644 (file)
index 0000000..700906c
--- /dev/null
@@ -0,0 +1,29 @@
+#
+#===============================================================================
+#
+#         FILE: NCIP_Handler.t
+#
+#  DESCRIPTION: 
+#
+#        FILES: ---
+#         BUGS: ---
+#        NOTES: ---
+#       AUTHOR: Chris Cormack (rangi), chrisc@catalyst.net.nz
+# ORGANIZATION: Koha Development Team
+#      VERSION: 1.0
+#      CREATED: 19/09/13 11:32:01
+#     REVISION: ---
+#===============================================================================
+
+use strict;
+use warnings;
+
+use Test::More tests => 1;                      # last test to print
+use lib 'lib';
+
+use_ok('NCIP::Handler');
+
+my $type='LookupItem';
+
+ok (my $handler = NCIP::Handler->new($type), 'Create new handler');
+ok (my $response = $handler->handle());