]> git.evergreen-ils.org Git - working/NCIPServer.git/blob - lib/NCIP/Handler.pm
Response headers behaving better
[working/NCIPServer.git] / lib / NCIP / Handler.pm
1 package NCIP::Handler;
2 #
3 #===============================================================================
4 #
5 #         FILE: Hander.pm
6 #
7 #  DESCRIPTION:
8 #
9 #        FILES: ---
10 #         BUGS: ---
11 #        NOTES: ---
12 #       AUTHOR: Chris Cormack (rangi), chrisc@catalyst.net.nz
13 # ORGANIZATION: Koha Development Team
14 #      VERSION: 1.0
15 #      CREATED: 19/09/13 10:43:14
16 #     REVISION: ---
17 #===============================================================================
18
19 # Copyright 2014 Catalyst IT <chrisc@catalyst.net.nz>
20
21 # This file is part of NCIPServer
22 #
23 # NCIPServer is free software; you can redistribute it and/or modify it under the
24 # terms of the GNU General Public License as published by the Free Software
25 # Foundation; either version 2 of the License, or (at your option) any later
26 # version.
27 #
28 # NCIPServer is distributed in the hope that it will be useful, but WITHOUT ANY
29 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
30 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
31 #
32 # You should have received a copy of the GNU General Public License along
33 # with NCIPServer; if not, write to the Free Software Foundation, Inc.,
34 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35
36 =head1 NAME
37
38     NCIP::Handler
39
40 =head1 SYNOPSIS
41
42     use NCIP::Handler;
43     my $handler = NCIP::Handler->new( { namespace    => $namespace,
44                                         type         => $request_type,
45                                         ils          => $ils,
46                                         template_dir => $templates
47                                        } );
48
49 =head1 FUNCTIONS
50 =cut
51
52 use Modern::Perl;
53 use Object::Tiny qw{ type namespace ils templates };
54 use Module::Load;
55 use Template;
56
57 =head2 new()
58
59     Set up a new handler object, this will actually create one of the request type
60     eg NCIP::Handler::LookupUser
61
62 =cut
63
64 sub new {
65     my $class    = shift;
66     my $params   = shift;
67     my $subclass = __PACKAGE__ . "::" . $params->{type};
68     load $subclass || die "Can't load module $subclass";
69     my $self = bless {
70         type      => $params->{type},
71         namespace => $params->{namespace},
72         ils       => $params->{ils},
73         templates => $params->{template_dir}
74     }, $subclass;
75     return $self;
76 }
77
78 =head2 get_user_elements($xml)
79
80     When passed an xml dom, this will find the user elements and pass convert them into an arrayref
81
82 =cut
83
84 sub get_user_elements {
85     my $self   = shift;
86     my $xmldoc = shift;
87     my $xpc    = XML::LibXML::XPathContext->new;
88     $xpc->registerNs( 'ns', $self->namespace() );
89
90     my $root = $xmldoc->documentElement();
91     my @elements =
92       $xpc->findnodes( '//ns:LookupUser/UserElementType/Value', $root );
93     unless ( $elements[0] ) {
94         @elements = $xpc->findnodes( '//ns:UserElementType', $root );
95     }
96     return \@elements;
97 }
98
99 sub get_agencies {
100     my $self   = shift;
101     my $xmldoc = shift;
102     my $xpc    = XML::LibXML::XPathContext->new;
103     $xpc->registerNs( 'ns', $self->namespace() );
104
105     my $root = $xmldoc->documentElement();
106
107     my $from =
108       $xpc->find( '//ns:FromAgencyId', $root );
109     my $to =
110       $xpc->find( '//ns:ToAgencyId', $root );
111     return ( $from, $to );
112 }
113
114 sub render_output {
115     my $self         = shift;
116     my $templatename = shift;
117
118     my $vars     = shift;
119     my $template = Template->new(
120         {
121             INCLUDE_PATH => $self->templates,
122             POST_CHOMP   => 1
123         }
124     );
125     my $output;
126     $template->process( $templatename, $vars, \$output );
127     return $output;
128 }
129 1;