From: Chris Cormack Date: Wed, 18 Sep 2013 23:42:56 +0000 (+1200) Subject: Starting work on our first handler X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=8c1ac90edbf66634ef033989c8d0d6e92fd6627f;p=working%2FNCIPServer.git Starting work on our first handler --- diff --git a/lib/NCIP.pm b/lib/NCIP.pm index cd74e5d..8f9a3e8 100644 --- a/lib/NCIP.pm +++ b/lib/NCIP.pm @@ -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 = " Hello There

Hello You Big JERK!

Who would take this book seriously if the first eaxample didn't say \"hello world\"? "; #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 index 0000000..e9577c5 --- /dev/null +++ b/lib/NCIP/Handler.pm @@ -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 index 0000000..81c192e --- /dev/null +++ b/lib/NCIP/Handler/LookupItem.pm @@ -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; diff --git a/t/NCIP.t b/t/NCIP.t index 43a2090..660302e 100644 --- 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'; - - - -EOT - -ok( my $response = $ncip->process_request($xml), 'Process a request' ); - my $xmlbad = <<'EOT'; 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 index 0000000..700906c --- /dev/null +++ b/t/NCIP_Handler.t @@ -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());