WebSource/Query.pm

filter: Statementstmt 16.1% 10 / 62 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 37.5% 3 / 8 Podpod 25.0% 1 / 4 totaltotal 13.8% 14 / 101 CC 14 SCAR 40.2
CC
14
Cov
11%
build_query
110.0
make_request
12.0
_init_
6.0
CRAP 55.5
« WebSource/Filter/tests.pm ↑ Summary WebSource/Module.pm »
1 package WebSource::Query;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 use WebSource::Module
7  
8 1 ▶our @ISA = ('WebSource::Module');
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 WebSource::Query - Build a query (HTTP request) given a hash
13  
14 =head1 DESCRIPTION
15  
16 A Query operator builds an HTTP query for each input.
17 The declaration of a query operator is a DOM Node with the following format :
18  
19 <ws:query name="opname" method="POST|GET" forward-to="ops">
20 <base>http://somewhere/query.cgi</base>
21 <parameters>
22 <param name="p1" default="v1" />
23 ...
24 <param name="pn" default="vn" />
25 </parameters>
26 </ws:query>
27  
28 =head1 METHODS
29  
30 See WebSource::Module
31  
32 =cut
33  
34 0 ▶sub _init_ {
Subroutine _init_: not called CC=2 SCAR=17.9
35 0 my $self = shift;
36 0 $self->SUPER::_init_;
37 0 ▶ my $wsd = $self->{wsdnode} or croak("No description node given\n");
Branch: 1 branch
BranchTrueFalseTotal
my $wsd = $self->{"wsdnode"} or croak("No description node given\n") 0 0 0
38 0 $self->{base_uri} = $wsd->findvalue('base');
39 0 $self->{method} = $wsd->getAttribute('method');
40 }
41  
42 0 ▶sub handle {
Subroutine handle: not called CC=2 SCAR=17.9
Pod documented
43 0 my $self = shift;
44 0 my $env = shift;
45  
46 0 my ($query, $missing) = $self->build_query($env);
47 0 ▶ if(@$missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$missing) 0 0 0
48 0 croak "Missing query parameters :\n\t- '" . join("'\n\t- '",@$missing) . "'\n";
49 }
50  
51 0 my $req = $self->make_request($query,$env);
52 0 return WebSource::Envelope->new(
53 type => "object/http-request",
54 data => $req);
55 }
56  
57 0 ▶sub build_query {
Subroutine build_query: not called CC=10 SCAR=47.0
Pod undocumented
58 0 my ($self,$env) = @_;
59 # $env->type eq "hash" or croak("Envelope didn't contain hash\n");
60 # my $qref = $env->data;
61 0 my $qref = $env;
62 0 my @params = $self->{wsdnode}->findnodes('parameters/param');
63 0 my @missing;
64  
65 my %query;
66 0 foreach $_ (@params) {
67 0 my $name = $_->getAttribute("name");
68 0 my $value;
69  
70 0 ▶ $_->hasAttribute("rename") and
Branch: 1 branch
BranchTrueFalseTotal
$_->hasAttribute("rename") and croak("Deprecated use of attribute rename in query parameters") 0 0 0
71 croak("Deprecated use of attribute rename in query parameters");
72  
73 0 ▶ if($_->hasAttribute("value-of")){
Branch: 1 branch
BranchTrueFalseTotal
if ($_->hasAttribute("value-of")) 0 0 0
74 0 my $key = $_->getAttribute("value-of");
75 0 ▶ if (exists($qref->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $qref->{$key}) { } 0 0 0
76 0 ▶ if($qref->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qref->{$key}) 0 0 0
77 $value = ($key eq 'data') ?
78 $env->dataString :
79 0 ▶ $qref->{$key};
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'data' ? : 0 0 0
80 }
81 } else {
82 0 $value = "";
83 }
84 }
85 0 ▶ if(!defined($value) && $_->hasAttribute("default")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $_->hasAttribute("default")) 0 0 0
Condition0%0/3
not defined $value and $_->hasAttribute("default")
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $value and $_->hasAttribute("default")
not defined $value $_->hasAttribute("default")
Truth table0%0/3
not(A) and B
A= not defined $valueB= $_->hasAttribute("default")
ABresult
0X0
100
111
86 0 $value = $_->getAttribute("default");
87 }
88  
89 0 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
90 0 $query{$name} = $value;
91 } else {
92 0 push(@missing, ($name));
93 }
94 };
95  
96 0 return (\%query,\@missing);
97 }
98  
99 0 ▶sub make_request {
Subroutine make_request: not called CC=3 SCAR=24.8
Pod undocumented
100 0 my ($self,$query,$env) = @_;
101 0 ▶ my $uri = URI->new($self->{base_uri} ? $self->{base_uri} : $env->dataAsURI);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'base_uri'} ? : 0 0 0
102 0 my $qstr;
103 map {
104 0 $qstr .= "\t". $_ . " => " . $query->{$_} ."\n";
105 } keys(%$query);
106 0 $self->log(5,"Built query : \n",$qstr);
107 0 $uri->query_form(%$query);
108 0 my $method = $self->{method};
109 0 ▶ if($method ne "GET") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method ne 'GET') { } 0 0 0
110 0 my $query = $uri->query;
111 0 $uri->query(undef);
112 0 my $req = HTTP::Request->new('POST',$uri);
113 0 $req->content_type("application/x-www-form-urlencoded");
114 0 $req->content_length(length($query));
115 0 $req->content($query);
116 0 return $req;
117 } else {
118 0 return HTTP::Request->new('GET',$uri);
119 }
120 }
121  
122 0 ▶sub parameters {
Subroutine parameters: not called CC=1 SCAR=6.9
Pod undocumented
123 0 my ($self) = @_;
124 return map {
125 0 $_->getAttribute("name") => $_->getAttribute("default")
126 0 } $self->{wsdnode}->findnodes('parameters/param');
127 }
128  
129 =head1 SEE ALSO
130  
131 WebSource
132  
133 =cut
134  
135 1 1;
« WebSource/Filter/tests.pm ↑ Summary WebSource/Module.pm »