File Coverage

blib/lib/Net/Dynect/REST/Response.pm
Criterion Covered Total %
statement 81 96 84.3
branch 28 48 58.3
condition 1 3 33.3
subroutine 14 14 100.0
pod 7 7 100.0
total 131 168 77.9


line stmt bran cond sub pod time code
1             package Net::Dynect::REST::Response;
2             # $Id: Response.pm 177 2010-09-28 00:50:02Z james $
3 1     1   4 use strict;
  1         1  
  1         33  
4 1     1   5 use warnings;
  1         2  
  1         38  
5 1     1   4 use overload '""' => \&_as_string;
  1         2  
  1         9  
6 1     1   573 use Net::Dynect::REST::Response::Data;
  1         3  
  1         34  
7 1     1   589 use Net::Dynect::REST::Response::Msg;
  1         3  
  1         39  
8 1     1   7 use Carp;
  1         3  
  1         1318  
9             our $VERSION = do { my @r = (q$Revision: 177 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r };
10              
11             =head1 NAME
12              
13             Net::Dynect::REST::Response - A response object from a request to Dynect
14              
15             =head1 SYNOPSIS
16              
17             use Net::Dynect::REST;
18             my $dynect = Net::Dynect::REST->new(user_name => $user, customer_name => $customer, password => $password);
19             use Net::Dynect::REST::Request;
20             my $request = Net::Dynect::REST::Request->new(operation => 'read', service => 'Zone');
21             $response = $dynect->execute($request);
22             print $response . "\n";
23             print $response->status . "\n";
24              
25             =head1 METHODS
26              
27             =head2 Creating
28              
29             =over 4
30              
31             =item new
32              
33             This creates a new Response object. It can optionally take the arguments (as a hash ref) of:
34              
35             =over 4
36              
37             =item * format => $format
38              
39             The valid format of the mssage, eithe JSON, YAML, XML, or HTML.
40              
41             =item * content => $content
42              
43             The decoded content from the HTTP response.
44              
45             =item * request_duration => $duration
46              
47             The time (in seconds, as a float) between the request being sent, and this response being returned.
48              
49             =item * request_time => $time
50              
51             The time the request was submitted to dynect (ie, this response was recieved as request_time + request_duration).
52              
53             =back
54              
55             =cut
56              
57             sub new {
58 1     1 1 3 my $proto = shift;
59 1   33     9 my $self = bless {}, ref($proto) || $proto;
60 1         9 my %args = @_;
61              
62 1 50       5 return unless defined $args{format};
63 1 50       6 return unless defined $args{content};
64              
65 1 50       649 $self->request_duration( $args{request_duration} )
66             if defined $args{request_duration};
67 1 50       9 $self->request_time( $args{request_time} ) if defined $args{request_time};
68              
69 1 50       5 if ( $args{format} eq "JSON" ) {
70 1         14 require JSON;
71 1         13 JSON->import('decode_json');
72 1         145 my $hash = decode_json( $args{content} );
73 1         6 $self->job_id( $hash->{job_id} );
74 1         5 $self->status( $hash->{status} );
75              
76 1         3 foreach ( @{ $hash->{msgs} } ) {
  1         4  
77 3         6 push @{ $self->{msgs} }, Net::Dynect::REST::Response::Msg->new($_);
  3         34  
78             }
79              
80 1 50       7 if ( ref( $hash->{data} ) eq "ARRAY" ) {
81 0         0 foreach ( @{ $hash->{data} } ) {
  0         0  
82 0         0 push @{ $self->{data} },
  0         0  
83             Net::Dynect::REST::Response::Data->new(
84             data => { value => $_ } );
85             }
86             }
87             else {
88 1         12 $self->data(
89             Net::Dynect::REST::Response::Data->new( data => $hash->{data} )
90             );
91             }
92             }
93              
94 1         5 return $self;
95             }
96              
97             =back
98              
99             =head2 Attributes
100              
101             =over 4
102              
103             =item job_id
104              
105             This is the job_id for a request. It may be that, if a request takes longer thana short period to process, a follow up request shoul dbe sent, with his job id, to get the eventual results.
106              
107             =cut
108              
109             sub job_id {
110 2     2 1 4 my $self = shift;
111 2 100       7 if (@_) {
112 1         3 my $new = shift;
113 1 50       4 return unless defined $new;
114 0 0       0 if ( $new !~ /^\d+$/ ) {
115 0         0 carp "Invalid job id: $new";
116 0         0 return;
117             }
118 0         0 $self->{job_id} = $new;
119             }
120 1         5 return $self->{job_id};
121             }
122              
123             =item status
124              
125             This is one of 'success', 'failure' or 'incomplete'.
126              
127             =cut
128              
129             sub status {
130 5     5 1 806 my $self = shift;
131 5 100       16 if (@_) {
132 1         3 my $new = shift;
133 1 50       9 if ( $new !~ /^success|failure|incomplete$/ ) {
134 0         0 carp "Invalid status: $new";
135 0         0 return;
136             }
137 1         35 $self->{status} = $new;
138             }
139 5         30 return $self->{status};
140             }
141              
142             =item msgs
143              
144             This is an array of zero or more messages that were returned. See L for details of what eachof these look like.
145              
146             =cut
147              
148             sub msgs {
149 4     4 1 7 my $self = shift;
150 4 50       9 if (@_) {
151 0         0 my $new = shift;
152 0         0 $self->{msgs} = $new;
153             }
154 4         18 return $self->{msgs};
155             }
156              
157             =item data
158              
159             This is the data part of the message that was returned.
160              
161             =cut
162              
163             sub data {
164 4     4 1 7 my $self = shift;
165 4 100       8 if (@_) {
166 1         1 my $new = shift;
167 1         4 $self->{data} = $new;
168             }
169 4         54 return $self->{data};
170             }
171              
172             =item request_duration
173              
174             This is th elengh of time, in seconds as a float, between the request being submitted, and this reponse being received.
175              
176             =cut
177              
178             sub request_duration {
179 6     6 1 13 my $self = shift;
180 6 100       17 if (@_) {
181 1         3 my $new_time = shift;
182 1 50       24 return unless $new_time =~ /^\d+(\.\d+)?$/;
183 1         75 $self->{request_duration} = $new_time;
184             }
185 6         40 return $self->{request_duration};
186             }
187              
188             =item request_time
189              
190             This was the time that the corresponding request that this response was built for, was submitted to Dynect.
191              
192             =cut
193              
194             sub request_time {
195 3     3 1 4 my $self = shift;
196 3 100       20 if (@_) {
197 1         4 my $new_time = shift;
198 1 50       16 return unless $new_time =~ /^\d+$/;
199 1         4 $self->{request_time} = $new_time;
200             }
201 3         21 return $self->{request_time};
202             }
203              
204             sub _as_string {
205 1     1   2 my $self = shift;
206 1         1 my @texts;
207 1 50       5 push @texts, sprintf "Job '%s'", $self->job_id if defined $self->job_id;
208 1 50       4 push @texts, sprintf "Status '%s'", $self->status if defined $self->status;
209 1 50       4 push @texts, sprintf "Requested %s GMT", scalar gmtime $self->request_time
210             if defined $self->request_time;
211 1 50       5 push @texts, sprintf "took %s secs", $self->request_duration
212             if defined $self->request_duration;
213 1         5 my $text = join( ', ', @texts ) . "\n";
214 1 50       4 $text .= "-- Msgs: " . join( ', ', @{ $self->msgs } ) . "\n"
  1         3  
215             if defined $self->msgs;
216              
217 1 50       6 if ( ref $self->data eq "ARRAY" ) {
218 0         0 foreach ( @{ $self->data } ) {
  0         0  
219 0         0 $text .= "-- Data: " . $_;
220             }
221             }
222             else {
223 1 50       3 $text .= "-- Data: " . $self->data if defined $self->data;
224             }
225 1         125 return $text;
226             }
227              
228             =back
229              
230             =head1 SEE ALSO
231              
232             L, L, L, L, L.
233              
234             =head1 AUTHOR
235              
236             James Bromberger, james@rcpt.to
237              
238             =head1 COPYRIGHT AND LICENSE
239              
240             Copyright (C) 2010 by James Bromberger
241              
242             This library is free software; you can redistribute it and/or modify
243             it under the same terms as Perl itself, either Perl version 5.10.1 or,
244             at your option, any later version of Perl 5 you may have available.
245              
246              
247             =cut
248              
249             1;