blib/lib/Froody/Response/Terse.pm

filter: Statementstmt 88.8% 80 / 90 Branchbran 66.6% 12 / 18 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 90.0% 18 / 20 Podpod 100% 6 / 6 totaltotal 85.4% 123 / 144 CC 12 SCAR 8.4
CC
12
Cov
85%
_transform
5.2
content
3.0
_validate_content
3.0
CRAP 2.3
« blib/lib/Froody/Method.pm ↑ Summary blib/lib/Froody/Base.pm »
1 package Froody::Response::Terse;
2 33 ▶use base qw(Froody::Response::Content);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 33 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Params::Validate qw(SCALAR ARRAYREF HASHREF);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 33 ▶use Froody::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 33 ▶use Froody::Walker;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 33 my $logger = get_logger("froody.response.terse");
14  
15 33 ▶use Froody::Response::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Froody::Response::Terse - create a response from a Terse data structure
20  
21 =head1 SYNOPSIS
22  
23 my $rsp = Froody::Response::Terse->new();
24 $rsp->structure($froody_method);
25 $rsp->content({
26 group => "imperialframeworks"
27 person => [
28 { nick => "Trelane", number => "243", name => "Mark Fowler" }
29 { nick => "jerakeen", number => "235", name => "Tom Insam" }
30 ],
31 -text => "Some of Frameworks went to Imperial Collage"
32 }
33 print $rsp->render();
34
35 # prints out (more or less)
36 <?xml version="1.0" encoding="utf-8" ?>
37 <people group="imperialframeworks">
38 <person nick="Trelane" number="234"><name>Mark Fowler</name></person>
39 <person nick="jerakeen" number="235"><name>Tom Insam</name></person>
40 Some of Frameworks went to Imperial Collage
41 </people>
42  
43 =head1 DESCRIPTION
44  
45 The Terse response class allows you to construct Responses from a very small
46 data structure that is the most logical form for the data to take. It is able
47 to convert the data to XML by virtue of the specification stored in the
48 Froody::Method alone.
49  
50 The data structure is exactly the same that's returned from a method
51 implementation that's defined in a Froody::Implementation subclass (If you
52 don't know what that is, I'd go read that module's documentation now if I were
53 you.)
54  
55 =head2 Methods
56  
57 =over 4
58  
59 =item create_envelope( XML::Document, XML::Node (content) )
60  
61 You're given one last shot to change the overall format of the response.
62  
63 The default behavior injects an <rsp stat='status'> wrapper around the C<content>
64 node.
65  
66 =back
67  
68 =head2 Attributes
69  
70 In addition to the attributes inherited from Froody::Response, this class has
71 the following get/set methods:
72  
73 =over 4
74  
75 =item status
76  
77 The status of the response, should be 'ok' or 'fail' only.
78  
79 =item content
80  
81 The contents of the data structure. Setting this accessor causes a deep
82 clone of the data structure to happen, meaning that you can add similar
83 content to multiple Response objects by setting on one object, altering, then
84 setting another object.
85  
86 my $data = { keywords => [qw( fotango office daytime )] };
87 $picture_of_office->content($data);
88 push @{ $data->{keywords} } = "Trelane";
89 $picture_of_me_in_office->content($data);
90
91 Note however, this is not true for the returned value from content. Altering
92 that data structure really does alter the data in the response (this is
93 considered a feature)
94  
95 # add my name as a keyword as well
96 push @{ $picture_of_me_in_office->content->{keywords} }, "Mark";
97  
98 =back
99  
100 =cut
101  
102 # yep, this is all we need to do, everything else is defined in the
103 # superclass
104 sub _to_xml
105 8 ▶{
Subroutine _to_xml: called CC=1 SCAR=0.0
106 8 my $self = shift;
107 8 my $content = shift;
108
109 # okay, we have an error. This means we should have a data
110 # structure that looks like this:
111 # { code => 123, message => "" }
112 # and we need to construct XML from it
113
114 # okay, we've got a sucessful response. Create the XML
115 8 my $document = XML::LibXML::Document->new("1.0", "utf-8");
116 8 my $child = $self->_transform('Terse','XML',$self->content, $document);
117 8 my $rsp = $self->create_envelope($document, $child);
118 8 $document->setDocumentElement($rsp);
119  
120 8 return $document;
121 }
122  
123 8 ▶sub create_envelope {
Subroutine create_envelope: called CC=2 SCAR=7.0
Pod documented
124 8 my ($self, $document, $content) = @_;
125 8 my $rsp = $document->createElement("rsp");
126 8 $rsp->setAttribute("stat" => $self->status );
127 8 ▶ $rsp->addChild( $content ) if $content;
Branch: 1 branch
BranchTrueFalseTotal
if $content 8 0 8
128 8 return $rsp;
129 }
130  
131 64 ▶sub _transform {
Subroutine _transform: called CC=5 SCAR=16.4
132 64 my $self = shift;
133 64 my $from = shift; # the walker class defines what we're transforming to what
134 64 my $to = shift;
135
136 64 my @args = @_;
137  
138 64 ▶ my $method = $self->structure
Branch: 1 branch
BranchTrueFalseTotal
my $method = $self->structure or "Froody::Error"->throw("froody.convert.nomethod", "Response has no associated Froody Structure!") 64 0 64
139 or Froody::Error->throw("froody.convert.nomethod", "Response has no associated Froody Structure!");
140  
141 # FIXME: Shouldn't a lack of structure indicate that we should be using the default
142 # structure???
143 64 ▶ my $spec = $method->structure
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $method->structure or "Froody::Error"->throw("froody.convert.nostructure", q[Associated method '] . $method->full_name . q[' doesn't have a specification structure!]) 64 0 64
144 or Froody::Error->throw("froody.convert.nostructure",
145 "Associated method '".$method->full_name."' doesn't have a specification structure!");
146
147 64 my $walker = Froody::Walker->new({
148 'spec' => $spec,
149 'method' => $method->full_name
150 });
151
152 # create a new instance of the walker that processes our data
153 64 $from = "Froody::Walker::".$from;
154
155 64 ▶ $from->require or Froody::Error->throw("perl.use",
Branch: 1 branch
BranchTrueFalseTotal
$from->require or "Froody::Error"->throw("perl.use", "couldn't load class $from") 64 0 64
156 "couldn't load class $from");
157
158 64 $walker->from($from->new);
159
160 # create a new instance of the walker that processes our data
161 64 $to = "Froody::Walker::".$to;
162
163 64 ▶ $to->require or Froody::Error->throw("perl.use",
Branch: 1 branch
BranchTrueFalseTotal
$to->require or "Froody::Error"->throw("perl.use", "couldn't load class $from") 64 0 64
164 "couldn't load class $from");
165
166 64 $walker->to($to->new);
167  
168  
169 # and walk with it
170 64 return $walker->walk(@args);
171 }
172  
173 =head2 Converting other Responses to Froody::Response::Terse objects
174  
175 Once you've loaded this class you can automatically convert other
176 Froody::Response class instances to Froody::Response::Terse objects with
177 the C<as_terse> method.
178  
179 use Froody::Response::Terse;
180 my $terse = Froody::Response::XML
181 ->new()
182 ->structure($froody_method)
183 ->content({ name => "foo", text => "bar" })
184 ->as_terse;
185 print ref($terse); # prints "Froody::Response::Terse"
186  
187 =cut
188  
189 # as_terse is documented
190 18 ▶sub as_terse { $_[0] }
Subroutine as_terse: called CC=1 SCAR=0.0
Pod documented
191  
192 sub Froody::Response::as_terse
193 17 ▶{
Subroutine as_terse: called CC=2 SCAR=7.0
Pod documented
194 17 my $self = shift;
195
196 # Er...I have no idea how to do this. quick, let's turn
197 # whatever we are into xml first!
198 17 ▶ unless ($self->can('as_xml')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can("as_xml")) 0 17 17
199 33 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
200 0 Carp::confess;
201 }
202 17 my $xml = $self->as_xml;
203  
204 # create a new terse
205 16 my $terse = Froody::Response::Terse->new();
206 16 $terse->structure($xml->structure);
207  
208 # walk the xml and set it as the content
209 15 my ($node) = $xml->xml->findnodes("/rsp/*");
210 15 $terse->content($terse->_transform("XML","Terse", $node));
211 15 return $terse;
212 }
213  
214 sub content
215 188 ▶{
Subroutine content: called CC=3 SCAR=11.0
Pod documented
216 188 my $self = shift;
217 188 my $ret = $self->SUPER::content(@_);
218 188 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 45 143 188
219 45 ▶ $ret = $self->_validate_content || $ret;
Condition66%2/3
$self->_validate_content || $ret
l!l&&r!l&&!r
17280
MC/DC0%0/2
$self->_validate_content || $ret
$self->_validate_content $ret
Truth table66%2/3
A || B
A= $self->_validate_contentB= $ret
ABresult
1X1
011
000
220 }
221 188 return $ret;
222 }
223  
224 # Use this only when you're really sure that you have
225 # valid terse structure and data (ie, don't.)
226 0 ▶sub _valid_content {
Subroutine _valid_content: not called CC=1 SCAR=6.9
227 0 my $self = shift;
228 0 $self->SUPER::structure(shift);
229 0 $self->SUPER::content(shift);
230 }
231  
232 sub _validate_content
233 89 ▶{
Subroutine _validate_content: called CC=3 SCAR=11.0
234 89 my $self = shift;
235 89 ▶ if ($self->structure && $self->content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->structure and $self->content) 41 48 89
Condition100%3/3
$self->structure and $self->content
!ll&&!rl&&r
282041
MC/DC100%2/2
$self->structure and $self->content
$self->structure $self->content
Truth table100%3/3
A and B
A= $self->structureB= $self->content
ABresult
0X0
100
111
236 41 return $self->SUPER::content($self->_transform("Terse","Terse",$self->content));
237 }
238 }
239  
240 sub structure
241 206 ▶{
Subroutine structure: called CC=2 SCAR=6.9
Pod documented
242 206 my $self = shift;
243 206 my $ret = $self->SUPER::structure(@_);
244 205 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 44 161 205
245 44 $self->_validate_content;
246 }
247 205 return $ret;
248 }
249  
250 sub as_error
251 0 ▶{
Subroutine as_error: not called CC=1 SCAR=6.9
Pod documented
252 0 my $self = shift;
253 0 my $data = $self->content;
254 0 my $code = delete $data->{code};
255 0 my $msg = delete $data->{msg};
256
257 0 my $error = Froody::Error->new(
258 $code,
259 $msg,
260 $data
261 );
262
263 0 return Froody::Response::Error->new()
264 ->structure($self->structure)
265 ->set_error($error);
266 }
267  
268 =head1 BUGS
269  
270 None known.
271  
272 Please report any bugs you find via the CPAN RT system.
273 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody>
274  
275 =head1 AUTHOR
276  
277 Copyright Fotango 2005. All rights reserved.
278  
279 Please see the main L<Froody> documentation for details of who has worked
280 on this project.
281  
282 This module is free software; you can redistribute it and/or modify it under
283 the same terms as Perl itself.
284  
285 =head1 SEE ALSO
286  
287 L<Froody>, L<Froody::Response>
288  
289 =cut
290  
291 33 1;
« blib/lib/Froody/Method.pm ↑ Summary blib/lib/Froody/Base.pm »