From f280c144508943ee7fb94a8d452ad65730ec3f36 Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Mon, 18 Nov 2013 15:30:16 +1300 Subject: [PATCH] Rejigging the creation of a handler a bit, so we now pass our ILS object This should allow our handlers to behave agnostically to the underlying ILS And we should only have to create the object once, at start up, rather than per message --- lib/NCIP.pm | 10 ++++++++-- lib/NCIP/Handler.pm | 15 +++++++++------ lib/NCIP/Handler/LookupItem.pm | 2 +- t/NCIP_Handler.t | 8 ++++++-- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/NCIP.pm b/lib/NCIP.pm index b962b1e..239cf1a 100644 --- a/lib/NCIP.pm +++ b/lib/NCIP.pm @@ -36,7 +36,7 @@ sub new { # load the ILS dependent module my $module = 'NCIP::ILS::' . $config->('NCIP.ils.value'); load $module || die "Can not load ILS module $module"; - my $ils = $module->new( name => $config->('NCIP.ils.value') ); + my $ils = $module->new( name => $config->('NCIP.ils.value') ); $self->{'ils'} = $ils; return bless $self, $class; @@ -61,7 +61,13 @@ sub process_request { #bail out for now } - my $handler = NCIP::Handler->new( $self->namespace(), $request_type ); + my $handler = NCIP::Handler->new( + { + namespace => $self->namespace(), + type => $request_type, + ils => $self->ils + } + ); return $handler->handle( $self->xmldoc ); } diff --git a/lib/NCIP/Handler.pm b/lib/NCIP/Handler.pm index 32367f1..a9fab25 100644 --- a/lib/NCIP/Handler.pm +++ b/lib/NCIP/Handler.pm @@ -17,16 +17,19 @@ package NCIP::Handler; #=============================================================================== use Modern::Perl; -use Object::Tiny qw{ type namespace }; +use Object::Tiny qw{ type namespace ils }; use NCIP::Handler::LookupItem; sub new { - my $class = shift; - my $namespace = shift; - my $type = shift; - my $subclass = __PACKAGE__ . "::" . $type; - my $self = bless { type => $type, namespace => $namespace }, $subclass; + my $class = shift; + my $params = shift; + my $subclass = __PACKAGE__ . "::" . $params->{type}; + my $self = bless { + type => $params->{type}, + namespace => $params->{namespace}, + ils => $params->{ils} + }, $subclass; return $self; } diff --git a/lib/NCIP/Handler/LookupItem.pm b/lib/NCIP/Handler/LookupItem.pm index e931bec..c101fa2 100644 --- a/lib/NCIP/Handler/LookupItem.pm +++ b/lib/NCIP/Handler/LookupItem.pm @@ -32,7 +32,7 @@ sub handle { my ($item_id) = $xmldoc->getElementsByTagNameNS( $self->namespace(), 'ItemIdentifierValue' ); - my $item = NCIP::Item->new( { itemid => $item_id->textContent() } ); + my $item = NCIP::Item->new( { itemid => $item_id->textContent(), ils => $self->ils} ); warn $item->itemid(); } return $self->type; diff --git a/t/NCIP_Handler.t b/t/NCIP_Handler.t index 1b97c2f..4e8b90d 100644 --- a/t/NCIP_Handler.t +++ b/t/NCIP_Handler.t @@ -22,9 +22,13 @@ use Test::More tests => 3; # last test to print use lib 'lib'; use_ok('NCIP::Handler'); -my $namespace='http://test'; +my $namespace = 'http://test'; my $type = 'LookupItem'; -ok( my $handler = NCIP::Handler->new($namespace, $type), 'Create new handler' ); +ok( + my $handler = + NCIP::Handler->new( { namespace => $namespace, type => $type } ), + 'Create new handler' +); ok( my $response = $handler->handle() ); -- 2.43.2