File Coverage

blib/lib/VoiceXML/Client/UserAgent.pm
Criterion Covered Total %
statement 21 96 21.8
branch 0 26 0.0
condition 0 24 0.0
subroutine 7 14 50.0
pod 0 7 0.0
total 28 167 16.7


line stmt bran cond sub pod time code
1             package VoiceXML::Client::UserAgent;
2              
3 3     3   1336 use VoiceXML::Client::Parser;
  3         9  
  3         107  
4              
5 3     3   21 use Hash::Util qw(lock_hash);
  3         7  
  3         32  
6 3     3   23956 use LWP::UserAgent;
  3         250880  
  3         107  
7 3     3   3386 use HTTP::Cookies;
  3         26862  
  3         106  
8 3     3   2226 use VoiceXML::Client::Engine::Component::Interpreter::Perl;
  3         15  
  3         127  
9              
10              
11             =head1 COPYRIGHT AND LICENSE
12              
13            
14             Copyright (C) 2007,2008 by Pat Deegan.
15             All rights reserved
16             http://voicexml.psychogenic.com
17              
18             This library is released under the terms of the GNU GPL version 3, making it available only for
19             free programs ("free" here being used in the sense of the GPL, see http://www.gnu.org for more details).
20             Anyone wishing to use this library within a proprietary or otherwise non-GPLed program MUST contact psychogenic.com to
21             acquire a distinct license for their application. This approach encourages the use of free software
22             while allowing for proprietary solutions that support further development.
23              
24              
25             This file is part of VoiceXML::Client.
26              
27            
28            
29             VoiceXML::Client is free software: you can redistribute it and/or modify
30             it under the terms of the GNU General Public License as published by
31             the Free Software Foundation, either version 3 of the License, or
32             (at your option) any later version.
33              
34             VoiceXML::Client is distributed in the hope that it will be useful,
35             but WITHOUT ANY WARRANTY; without even the implied warranty of
36             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37             GNU General Public License for more details.
38              
39             You should have received a copy of the GNU General Public License
40             along with VoiceXML::Client. If not, see .
41              
42              
43             =cut
44              
45              
46 3     3   17 use strict;
  3         8  
  3         96  
47              
48              
49              
50 3         3591 use vars qw{
51             $VERSION
52             %Directive
53 3     3   16 };
  3         8  
54              
55             $VERSION = '1.0.0';
56              
57             %Directive = (
58              
59             );
60             Hash::Util::lock_hash(%Directive);
61              
62             sub new {
63 0     0 0   my $class = shift;
64 0   0       my $domain = shift || 'localhost';
65 0   0       my $basepath = shift || '/';
66 0   0       my $params = shift || {};
67            
68 0           my $self = {};
69            
70 0   0       bless $self, ref $class || $class;
71            
72 0           $self->{'domain'} = $domain;
73 0           $self->{'basepath'} = $basepath;
74            
75 0 0         if (exists $params->{'agent'})
76             {
77 0           $self->{'agent'} = $params->{'agent'};
78             } else {
79 0           $self->{'agent'} = "VoiceXML::Client VOCP VoiceBrowser/$VERSION";
80             }
81            
82 0 0         if (exists $params->{'protocol'})
83             {
84 0           $self->{'protocol'} = $params->{'protocol'};
85             } else {
86 0           $self->{'protocol'} = 'http';
87             }
88            
89 0 0         if (exists $params->{'runtime'})
90             {
91 0           $self->{'runtime'} = $params->{'runtime'};
92             } else {
93 0           $self->{'runtime'} = VoiceXML::Client::Engine::Component::Interpreter::Perl->runtime();
94             }
95            
96 0 0         if (exists $params->{'errormsgfile'})
97             {
98 0           $self->{'errormsgfile'} = $params->{'errormsgfile'};
99             } else {
100 0           $self->{'errormsgfile'} = '';
101             }
102            
103 0           $self->initUA($params);
104            
105 0   0       $self->{'vxmlparser'} = new VoiceXML::Client::Parser || die "Could not create parser??";
106            
107 0           return $self;
108             }
109              
110             sub initUA {
111 0     0 0   my $self = shift;
112 0           my $params = shift;
113            
114            
115 0 0         unless (exists $self->{'cookiejar'})
116             {
117            
118 0           my %cookieParams = (
119             );
120            
121 0 0         if ($params->{'cookiefile'})
122             {
123 0           $cookieParams{'file'} = $params->{'cookiefile'};
124             }
125            
126 0           $self->{'cookiejar'} = HTTP::Cookies->new( %cookieParams );
127             }
128            
129 0           $self->{'ua'} = LWP::UserAgent->new(
130             'agent' => $self->{'agent'},
131             'cookie_jar' => $self->{'cookiejar'},
132             );
133            
134 0           return 1;
135             }
136             sub constructFullURL {
137 0     0 0   my $self = shift;
138 0   0       my $url = shift || '' ;
139            
140 0 0         if ($url !~ m|^\w+://|)
141             {
142             # not full url...
143 0           my $fullURL = $self->{'protocol'} . '://' . $self->{'domain'};
144            
145 0 0         if ($url !~ m|^/|)
146             {
147             # not full path...
148 0           $fullURL .= $self->{'basepath'};
149             }
150            
151 0           $fullURL .= $url;
152            
153 0           VoiceXML::Client::Util::log_msg("constructFullURL return $fullURL");
154 0           return $fullURL;
155             }
156            
157             # it is full already...
158 0           return $url;
159             }
160              
161             sub get {
162 0     0 0   my $self = shift;
163 0           my $url = shift;
164            
165 0           return $self->{'ua'}->get($self->constructFullURL($url));
166             }
167              
168             sub post {
169 0     0 0   my $self = shift;
170 0           my $url = shift ;
171 0   0       my $data = shift || {};
172            
173 0           return $self->{'ua'}->get($self->constructFullURL($url), $data);
174             }
175              
176             sub runApplication {
177 0     0 0   my $self = shift;
178 0   0       my $url = shift || die "VoiceXML::Client::UserAgent::runApplication MUST pass start URL";
179 0   0       my $deviceHandle = shift || die "VoiceXML::Client::UserAgent::runApplication MUST pass device handle";
180            
181            
182 0           my $itemExecParams = {
183             'errormsgfile' => $self->errorMessageFile()
184             };
185            
186 0           my $retVal;
187 0           my $docCount = 0;
188 0           do {
189            
190 0           my $response = $self->get($url);
191            
192 0 0         unless ($response->is_success)
193             {
194 0           warn "Could not fetch document $url";
195 0           die $response->status_line;
196             }
197            
198 0           $docCount++;
199            
200 0           my $vxmlFileContents = $response->content;
201 0 0         print STDERR "$vxmlFileContents" if ($VoiceXML::Client::Debug > 2);
202 0   0       my $vxmlDoc = $self->{'vxmlparser'}->parse($vxmlFileContents, $self->{'runtime'}) || die "Could not parse response from $url";
203            
204            
205 0           $retVal = $vxmlDoc->execute($deviceHandle, $itemExecParams);
206            
207 0 0         if ($retVal == $VoiceXML::Client::Flow::Directive{'NEXTDOC'})
208             {
209 0           $url = $vxmlDoc->nextDocument();
210 0 0         die "Told to go to next doc, but nextDocument not set" unless ($url);
211             }
212            
213             } while ($retVal == $VoiceXML::Client::Flow::Directive{'NEXTDOC'});
214            
215 0           VoiceXML::Client::Util::log_msg("Application completed ($retVal) after $docCount fetches");
216            
217 0           return ;
218             }
219              
220              
221             sub errorMessageFile {
222 0     0 0   my $self = shift;
223 0           my $setTo = shift;
224            
225 0 0 0       if (defined $setTo && -r $setTo)
226             {
227 0           $self->{'errormsgfile'} = $setTo;
228             }
229            
230 0           return $self->{'errormsgfile'};
231            
232             }
233            
234              
235              
236            
237             1;