]> git.evergreen-ils.org Git - working/NCIPServer.git/blob - lib/NCIP.pm
Refactoring NCIP.pm slightly, dealing with namespaces more elegantly
[working/NCIPServer.git] / lib / NCIP.pm
1 package NCIP;
2 use NCIP::Configuration;
3 use Modern::Perl;
4 use XML::LibXML;
5 use Try::Tiny;
6
7 use base qw(Class::Accessor);
8
9 our $VERSION = '0.01';
10 our $nsURI = 'http://www.niso.org/2008/ncip';
11
12 =head1 NAME
13   
14     NCIP
15
16 =head1 SYNOPSIS
17
18     use NCIP;
19     my $nicp = NCIP->new($config_dir);
20
21 =head1 FUNCTIONS
22
23 =cut
24
25 sub new {
26     my $proto      = shift;
27     my $class      = ref $proto || $proto;
28     my $config_dir = shift;
29     my $self       = {};
30     my $config     = NCIP::Configuration->new($config_dir);
31     $self->{config} = $config;
32     return bless $self, $class;
33
34 }
35
36 =head2 process_request()
37
38  my $response = $ncip->process_request($xml);
39
40 =cut
41
42 sub process_request {
43     my $self = shift;
44     my $xml  = shift;
45
46     my $request_type = $self->handle_initiation($xml);
47     unless ($request_type) {
48
49       # We have invalid xml, or we can't figure out what kind of request this is
50       # Handle error here
51     }
52
53 #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>";
54
55     #return $response;
56     return $request_type;
57 }
58
59 =head2 handle_initiation
60
61 =cut
62
63 sub handle_initiation {
64     my $self = shift;
65     my $xml  = shift;
66     my $dom;
67     try {
68         $dom = XML::LibXML->load_xml( string => $xml );
69     }
70     catch {
71         warn "Invalid xml, caught error: $_";
72     };
73     if ($dom) {
74
75         # should check validity with validate at this point
76         my $request_type = $self->parse_request($dom);
77         return $request_type;
78     }
79     else {
80         return;
81     }
82 }
83
84 sub validate {
85
86     # this should perhaps be in it's own module
87     my $self     = shift;
88     my $dom      = shift;
89     my $validity = $dom->is_valid();
90
91     # we could validate against the dtd here, might be good?
92     # my $dtd = XML::LibXML::Dtd->parse_string($dtd_str);
93     # my $validity = $dom->is_valid($dtd);
94     # perhaps we could check the ncip version and validate that too
95     return $validity;
96 }
97
98 sub parse_request {
99     my $self  = shift;
100     my $dom   = shift;
101     my $nodes = $dom->getElementsByTagNameNS($nsURI,'NCIPMessage');
102     if ($nodes){
103         my @childnodes = $nodes->[0]->childNodes();
104         if ($childnodes[1]){
105             return $childnodes[1]->localname();
106         }
107         else {
108             return "unknown";
109         }
110     }
111     else {
112         warn "Invalid XML";
113         return 0;
114     }
115     return 0;
116 }
117
118 1;