File Coverage

blib/lib/REST/Request.pm
Criterion Covered Total %
statement 24 26 92.3
branch 7 8 87.5
condition 3 3 100.0
subroutine 6 7 85.7
pod 4 4 100.0
total 44 48 91.6


line stmt bran cond sub pod time code
1             #----------------------------------------------------------------------
2             =pod
3              
4             =head1 NAME
5              
6             REST::Request
7              
8             =head1 SYNOPSIS
9              
10             use REST::Resource;
11              
12             sub main
13             {
14             my( $restul ) = new REST::Resource( request_interface => new REST::Request() );
15             ...
16             }
17              
18             =head1 DESCRIPTION
19              
20             This class provides a standardized interface shim that users can
21             implement in order to wrap around their favorite CGI interface module
22             so that it can be registered and used by REST::Resource.
23              
24             If you prefer some module other than CGI.pm to access server-side CGI
25             behavior, then create a module that mimics this interface and register
26             it with REST::Resource as shown in the synopsis.
27              
28             =head1 INTERFACE v. ABSTRACT BASE CLASS
29              
30             In this case, I prefer Java's interface-style to an abstract base
31             class that someone must override. Since this class derives from
32             CGI.pm for its implementation, you may not want that baggage in your
33             interface implementation. Therefore, all you need to do is register a
34             class that provides the functionality specified by this module.
35              
36             Since there isn't really a great Perl-based interface specification,
37             REST::Resource will interrogate your registered request_interface to
38             ensure that the class provides the minimum / required methods:
39              
40             new()
41             http()
42             param()
43             header()
44              
45             If you chose to provide an alternate interface implementation, these
46             are the methods that must exist before REST::Resource will accept your
47             interface.
48              
49             =head1 AUTHOR
50              
51             John "Frotz" Fa'atuai
52             frotz@acm.org
53              
54             =head1 INTERFACE METHODS
55              
56             =cut
57              
58             package REST::Request;
59              
60 4     4   23 use strict;
  4         9  
  4         156  
61 4     4   23 use warnings;
  4         16  
  4         139  
62 4     4   21 use base "CGI";
  4         9  
  4         2259  
63              
64             our( $VERSION ) = '0.5.2.4'; ## MODULE-VERSION-NUMBER
65              
66              
67              
68              
69              
70             #----------------------------------------------------------------------
71             =pod
72              
73             =head2 new()
74              
75             USAGE:
76              
77             my( $restful ) = new REST::Resource( request_interface => new REST::Request() );
78             my( $request ) = new REST::Request();
79              
80             DESCRIPTION:
81              
82             This method constructs a new instance of the request object. The
83             first usage shows how users should pass this into REST::Resource. The
84             second usage shows how you might use this in your unit tests.
85              
86             WARNING:
87              
88             This constructor plays REST games with CGI.pm by detecting PUT or
89             DELETE and transforming the request (temporarily) to POST, then
90             reverting back to the original value before returning an instance.
91             This allows us to use all of the nice POST processing provided by
92             CGI.pm, but for PUT, and DELETE, not just POST.
93              
94             =cut
95              
96             sub new
97             {
98 15     15 1 7604 my( $class ) = shift;
99 15 50       48 $class = ref( $class ) if (ref( $class ));
100              
101 15         42 my( $orig ) = $ENV{REQUEST_METHOD};
102 15 100       125 $ENV{REQUEST_METHOD} = "POST" if ($orig =~ /PUT|DELETE/i);
103              
104 15         85 my( $this ) = $class->SUPER::new( @_ );
105              
106 15 100       896 $ENV{REQUEST_METHOD} = $orig if ($orig =~ /PUT|DELETE/i);
107 15         84 return( $this );
108             }
109              
110              
111              
112              
113             #----------------------------------------------------------------------
114             =pod
115              
116             =head2 http()
117              
118             USAGE:
119              
120             my( $value ) = $request->http( $variable );
121              
122             DESCRIPTION:
123              
124             This method extracts the given CGI $variable from the underlying
125             $request and returns its $value.
126              
127             =cut
128              
129             sub http
130             {
131 45     45 1 143 my( $this ) = shift;
132 45         64 my( $var ) = shift;
133              
134 45         1118 my( $retval ) = $this->SUPER::http( $var ); ## Try to get it first.
135 45 100       2934 unless( $retval ) ## Failing that...
136             {
137 23   100     138 $retval = ($ENV{$var} || ## Exact name match.
138             $ENV{ uc( $var ) } || ## Uppercase match.
139             $ENV{ lc( $var ) } ## Lowercase match.
140             );
141             }
142 45         193 return( $retval );
143             }
144              
145              
146              
147              
148             #----------------------------------------------------------------------
149             =pod
150              
151             =head2 header()
152              
153             USAGE:
154              
155             $request->header( %args );
156              
157             DESCRIPTION:
158              
159             This interface method provides access to the CGI-response header
160             functionality. This method will be called when you have the
161             collection of response headers that you want to pass down to your base
162             class.
163              
164             =cut
165              
166             sub header()
167             {
168 0     0 1 0 my( $this ) = shift;
169 0         0 return( $this->SUPER::header( @_ ) );
170             }
171              
172              
173              
174              
175              
176             #----------------------------------------------------------------------
177             =pod
178              
179             =head2 param()
180              
181             USAGE:
182              
183             my( $value ) = $request->param( $variable );
184              
185             DESCRIPTION:
186              
187             This method returns the $value of the CGI request parameter $variable.
188              
189             =cut
190              
191             sub param
192             {
193 89     89 1 21239 my( $this ) = shift;
194 89         411 return( $this->SUPER::param( @_ ) );
195             }
196              
197              
198              
199             #----------------------------------------------------------------------
200             =pod
201              
202             =head1 SEE ALSO
203              
204             CGI
205             REST::Resource
206              
207             =cut
208              
209             1;