File Coverage

blib/lib/Bio/PrimerDesigner/Remote.pm
Criterion Covered Total %
statement 46 47 97.8
branch 8 14 57.1
condition 2 6 33.3
subroutine 8 8 100.0
pod 2 2 100.0
total 66 77 85.7


line stmt bran cond sub pod time code
1             package Bio::PrimerDesigner::Remote;
2              
3             # $Id: Remote.pm 9 2008-11-06 22:48:20Z kyclark $
4              
5             =head1 NAME
6              
7             Bio::PrimerDesigner::Remote - A class for remote access to Bio::PrimerDesigner
8              
9             =head1 SYNOPSIS
10              
11             use Bio::PrimerDesigner::Remote;
12              
13             =head1 DESCRIPTION
14              
15             Interface to the server-side binaries. Passes the primer design
16             paramaters to a remote CGI, which uses a server-side installation of
17             Bio::PrimerDesigner to process the request.
18              
19             =head1 METHODS
20              
21             =cut
22              
23 7     7   923 use strict;
  7         16  
  7         236  
24 7     7   36 use warnings;
  7         14  
  7         224  
25 7     7   6039 use HTTP::Request;
  7         183618  
  7         387  
26 7     7   9339 use LWP::UserAgent;
  7         201875  
  7         247  
27 7     7   6099 use Readonly;
  7         28934  
  7         699  
28              
29             Readonly our
30             $VERSION => sprintf "%s", q$Revision: 24 $ =~ /(\d+)/;
31              
32 7     7   56 use base 'Class::Base';
  7         15  
  7         6919  
33              
34             # -------------------------------------------------------------------
35             sub CGI_request {
36              
37             =pod
38              
39             =head2 CGI_request
40              
41             Passes arguments to the URL of the remote Bio::PrimerDesigner CGI and
42             returns the raw output for further processing by local design classes.
43              
44             =cut
45              
46            
47 3     3 1 2330 my $self = shift;
48 3 100       95 my $url = shift or return $self->error('No URL specified');
49 1 50       6 $url = 'http://' . $url unless $url =~ m{https?://};
50 1 50       7 my $args = shift or return $self->error('No config file');
51 1         2 my $program = $args->{'program'};
52 1         12 my $ua = LWP::UserAgent->new;
53              
54             #
55             # Is the remote server able to process our request?
56             #
57 1 50       4045 unless ( $self->check( $url, $ua, $program ) ) {
58 0         0 return $self->error("$url did not return expected result");
59             }
60              
61 1         10 my $request = HTTP::Request->new('POST', $url);
62              
63             #
64             # string-ify the config hash to pass to the CGI
65             #
66 1         167 my @content = ();
67 1         5 @content = map {"$_=" . $args->{$_}} keys %$args;
  1         8  
68 1         3 my $content = join "#", @content;
69            
70 1         9 $request->content( "config=$content" );
71 1         24 my $response = $ua->request( $request );
72 1         305635 my $output = $response->content;
73            
74 1 50 33     29 return $self->error("Some sort of HTTP error")
      33        
75             unless $ua && $request && $response;
76              
77 1         8 return map { $_ . "\n" } split "\n", $output;
  7         61  
78             }
79              
80             # -------------------------------------------------------------------
81             sub check {
82              
83             =pod
84              
85             =head2 check
86              
87             Tests the URL to make sure the host is live and the CGI returns the
88             expected results.
89              
90             =cut
91              
92 1     1 1 3 my $self = shift;
93 1         3 my ($url, $ua, $program) = @_;
94            
95 1         2 my $content = "check=" . $program;
96 1         8 my $request = HTTP::Request->new( 'POST', $url );
97 1         10280 $request->content( $content );
98 1         32 my $response = $ua->request( $request );
99              
100 1 50       453556 return $self->error("No reponse from host $url")
101             unless $response;
102              
103 1         10 my $output = $response->content;
104              
105 1 50       32 return $self->error("Incorrect response from host $url")
106             unless $output =~ /$program OK/m;
107              
108 1         27 return 1;
109             }
110              
111             1;
112              
113             # -------------------------------------------------------------------
114              
115             =pod
116              
117             =head1 AUTHOR
118              
119             Copyright (C) 2003-2009 Sheldon McKay Emckays@cshl.eduE,
120             Ken Youens-Clark Ekclark@cpan.orgE.
121              
122             =head1 LICENSE
123              
124             This program is free software; you can redistribute it and/or modify
125             it under the terms of the GNU General Public License as published by
126             the Free Software Foundation; version 3 or any later version.
127              
128             This program is distributed in the hope that it will be useful, but
129             WITHOUT ANY WARRANTY; without even the implied warranty of
130             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
131             General Public License for more details.
132              
133             You should have received a copy of the GNU General Public License
134             along with this program; if not, write to the Free Software
135             Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
136             USA.
137              
138             =head1 SEE ALSO
139              
140             Bio::PrimerDesigner, primer_designer.cgi.
141              
142             =cut