File Coverage

blib/lib/SRU/Request.pm
Criterion Covered Total %
statement 63 82 76.8
branch 13 24 54.1
condition 5 9 55.5
subroutine 13 14 92.8
pod 4 4 100.0
total 98 133 73.6


line stmt bran cond sub pod time code
1             package SRU::Request;
2             {
3             $SRU::Request::VERSION = '1.01';
4             }
5             #ABSTRACT: Factories for creating SRU request objects.
6              
7 2     2   33349 use strict;
  2         5  
  2         86  
8 2     2   10 use warnings;
  2         5  
  2         57  
9 2     2   4143 use URI;
  2         13744  
  2         65  
10 2     2   1457 use SRU::Request::Explain;
  2         7  
  2         14  
11 2     2   1365 use SRU::Request::SearchRetrieve;
  2         7  
  2         16  
12 2     2   1548 use SRU::Request::Scan;
  2         5  
  2         13  
13 2     2   63 use SRU::Utils qw( error );
  2         5  
  2         94  
14 2     2   1125 use SRU::Utils::XML qw( escape );
  2         5  
  2         141  
15 2     2   12 use Scalar::Util qw(reftype);
  2         3  
  2         1841  
16              
17             our %PARAMETERS = (
18             'explain' =>
19             [qw(version recordPacking stylesheet extraRequestData)],
20             'scan' =>
21             [qw(version scanClause responsePosition maximumTerms stylesheet
22             extraRequestData)],
23             'searchRetrieve' =>
24             [qw(version query startRecord maximumRecords recordPacking recordSchema
25             recordXPath resultSetTTL sortKeys stylesheet extraRequestData)]
26             );
27              
28              
29             sub new {
30 4     4 1 4091 my $class = shift;
31              
32 4         10 my %query;
33              
34 4 50       20 if ( @_ % 2 ) {
35 4         9 my $q = shift;
36              
37 4 100 50     95 if ( UNIVERSAL::isa( $q, 'CGI' ) ) {
    50          
38             ## we must have ampersands between query string params, but lets
39             ## make sure we don't screw anybody else up
40 1         3 my $saved = $CGI::USE_PARAM_SEMICOLONS;
41 1         2 $CGI::USE_PARAM_SEMICOLONS = 0;
42 1         16 $q = $q->self_url;
43 1         5361 $CGI::USE_PARAM_SEMICOLONS = $saved;
44             } elsif ( (reftype $q // '') eq 'HASH' ) {
45 3         14 $q = "http://example.org/?" . $q->{QUERY_STRING};
46             }
47            
48 4 50       38 if ( ! UNIVERSAL::isa( $q, 'URI' ) ) {
49 4         28 $q = URI->new($q);
50             }
51 4 50       28244 if ( UNIVERSAL::isa( $q, 'URI' ) ) {
52 4         158 %query = $q->query_form;
53             } else {
54 0         0 return error( "invalid uri: $q" )
55             }
56             } else {
57 0         0 %query = @_;
58             }
59              
60 4   50     864 my $operation = $query{operation} || 'explain';
61              
62 4         6 my $request;
63 4 100       20 if ( $operation eq 'scan' ) {
    100          
    50          
64 2         30 $request = SRU::Request::Scan->new( %query );
65             } elsif ( $operation eq 'searchRetrieve' ) {
66 1         13 $request = SRU::Request::SearchRetrieve->new( %query );
67             } elsif ( $operation eq 'explain' ) {
68 1         11 $request = SRU::Request::Explain->new( %query );
69             } else {
70 0         0 $request = SRU::Request::Explain->new( %query );
71 0         0 $request->missingOperator(1);
72             }
73              
74 4         80 return $request;
75              
76             }
77              
78              
79             *newFromURI = *new;
80             *newFromCGI = *new;
81              
82              
83             sub asXML {
84 0     0 1 0 my $self = shift;
85              
86             ## extract the type of request from the type of object
87 0         0 my ($type) = ref($self) =~ /^SRU::Request::(.*)$/;
88 0         0 $type = "echoed${type}Request";
89              
90             ## build the xml
91 0         0 my $xml = "<$type>";
92              
93             ## add xml for each param if it is available
94 0         0 foreach my $param ( $self->validParams() ) {
95 0 0       0 $xml .= "<$param>" . escape($self->$param) . ""
96             if $self->$param;
97             }
98             ## add XCQL if appropriate
99 0 0       0 if ( $self->can( 'cql' ) ) {
100 0         0 my $cql = $self->cql();
101 0 0       0 if ( $cql ) {
102 0         0 my $xcql = $cql->toXCQL(0);
103 0         0 chomp( $xcql );
104 0         0 $xcql =~ s/>\n *
105 0         0 $xml .= "$xcql";
106             }
107             }
108              
109 0         0 $xml .= "";
110 0         0 return $xml;
111             }
112              
113              
114             sub asURI {
115 6     6 1 252 my ($self, $base) = @_;
116              
117 6   100     41 my $uri = URI->new($base // "http://localhost/");
118 6         329 my %query = $uri->query_form;
119              
120 6         104 $query{operation} = $self->type;
121            
122 2     2   56 no strict 'refs';
  2         6  
  2         387  
123 6         11 foreach (@{ $PARAMETERS{ $self->type } }) {
  6         15  
124 42 100       776 $query{$_} = $self->$_ if defined $self->$_;
125             }
126              
127 6         93 $uri->query_form( \%query );
128 6         1161 return $uri;
129             }
130              
131              
132              
133             sub type {
134 12     12 1 16 my $self = shift;
135 12   33     34 my $class = ref $self || $self;
136 12         59 return lcfirst( ( split( '::', $class ) )[ -1 ] );
137             }
138              
139             1;
140              
141             __END__