File Coverage

blib/lib/Net/Dynect/REST/Request.pm
Criterion Covered Total %
statement 52 72 72.2
branch 22 46 47.8
condition 2 5 40.0
subroutine 11 11 100.0
pod 6 6 100.0
total 93 140 66.4


line stmt bran cond sub pod time code
1             package Net::Dynect::REST::Request;
2             # $Id: Request.pm 149 2010-09-26 01:33:15Z james $
3 1     1   9 use strict;
  1         1  
  1         28  
4 1     1   4 use warnings;
  1         1  
  1         19  
5 1     1   5 use Carp;
  1         1  
  1         100  
6 1     1   1739 use overload '""' => \&_as_string;
  1         1191  
  1         18  
7             our $VERSION = do { my @r = (q$Revision: 149 $ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r };
8              
9             =head1 NAME
10              
11             Net::Dynect::REST::Request - A request object to supply to Dynect
12              
13             =head1 SYNOPSIS
14              
15             use Net::Dynect::REST::Request;
16             $request = Net::Dynect::REST::Request->new(operation => 'read', service => 'Zone', params => {zone => 'example.com'});
17              
18             =head1 DESCRIPTION
19              
20             The Request object in the REST interface will form the basis of the underlying HTTP request that is made to the REST server. It will format the optional parameters in one of the supported formats.
21              
22             =head1 METHODS
23              
24             =head2 Creating
25              
26             =over 4
27              
28             =item new
29              
30             This creator method will return a new Net::Dynect::REST::Request object. You may use the following arguments:
31              
32             =over 4
33              
34             =item * operation => $value
35              
36             The operation is either 'create', 'read', 'update', or 'delete' (CRUD).
37              
38             =item * service => $service
39              
40             The service is the end of the URI that will handle the request. The base of the URI, including hte protocol, server name and port, and a base path, is already known in the session object - your session should already be established to pass this request to be executed. Hence the I value is one of a list as documented in the manual (eg, I). Note this is B.
41              
42             =item * params => {list => $value1, of => $value2, parameters => $values3);
43              
44             A reference to a hash with the set of parameters being passed to the service. The exact list of valid parameters depends upon the service being accessed; it may be a zone name, a record name, etc.
45              
46             =back
47              
48             =back
49              
50             =cut
51              
52             sub new {
53 2     2 1 17 my $proto = shift;
54 2   33     14 my $self = bless {}, ref($proto) || $proto;
55 2         11 my %args = @_;
56              
57 2 50       12 $self->operation( $args{operation} ) if defined $args{operation};
58              
59 2 50       11 $self->service( $args{service} ) if defined $args{service};
60 2 100       9 $self->params( $args{params} ) if defined $args{params};
61              
62 2         13 return $self;
63             }
64              
65             =head2 Attributes
66              
67             =over 4
68              
69             =item operation
70              
71             This is the operation to perform upon the service. It is one of:
72              
73             =over 4
74              
75             =item * create
76              
77             =item * read
78              
79             =item * update
80              
81             =item * delete
82              
83             =back
84              
85             =cut
86              
87             sub operation {
88 8     8 1 13 my $self = shift;
89 8 100       20 if (@_) {
90 2         4 my $new = shift;
91 2 50       11 if ( $new !~ /^create|read|update|delete$/ ) {
92 0         0 carp
93             "Invalid operation: $new. Must be one of create, read, update, delete (CRUD)";
94 0         0 return;
95             }
96 2         43 $self->{operation} = $new;
97             }
98 8         51 return $self->{operation};
99             }
100              
101             =item service
102              
103             This is the end of the URI that will handle the REST request. There is a long list of the implemented services in the Dynect REST API manual.
104              
105             =cut
106              
107             sub service {
108 7     7 1 422 my $self = shift;
109 7 100       21 if (@_) {
110 2         5 my $new = shift;
111 2 50       9 if ( $new !~
112             /^Session|QPSReport|ACL|Contact|Password|User|AAAARecord|ANYRecord|ARecord|CNAMERecord|DNSKEYRecord|DSRecord|KEYRecord|LOCRecord|MXRecord|NSRecord|PTRRecord|RPRecord|SOARecord|SRVRecord|TXTRecord|Job|Node|NodeList|Secondary|Zone|ZoneChanges(\/.*)?$/
113             )
114             {
115 0         0 carp "Invalid service. See manual for list of valid services.";
116 0         0 return;
117             }
118 2         6 $self->{service} = $new;
119             }
120 7         33 return $self->{service};
121             }
122              
123             =item format
124              
125             This is the format that we will send our request in, and hope to recieve our response in. It is one of:
126              
127             =over 4
128              
129             =item * JSON
130              
131             =item * XML
132              
133             =item * YAML
134              
135             =item * HTML
136              
137             =back
138              
139             =cut
140              
141             sub format {
142 11     11 1 286 my $self = shift;
143 11 50       31 if (@_) {
144 0         0 my $new = shift;
145 0 0       0 if ( $new !~ /^JSON|XML|YAML|HTML$/ ) {
146 0         0 carp "Invalid format. Must be one of JSON, XML, YAML, or HTML.";
147 0         0 return;
148             }
149 0         0 $self->{format} = $new;
150             }
151 11   50     103 return $self->{format} || "JSON";
152             }
153              
154             =item mime_type
155              
156             This returns the mime type for the L already selected.
157              
158             =cut
159              
160             sub mime_type {
161 2     2 1 4 my $self = shift;
162              
163 2 50       5 if ( not defined $self->format ) {
164 0         0 carp "format() needs to be set";
165 0         0 return;
166             }
167 2 50       6 return "application/json" if $self->format eq "JSON";
168 0 0       0 return "text/xml" if $self->format eq "XML";
169 0 0       0 return "application/yaml" if $self->format eq "YAML";
170 0 0       0 return "text/html" if $self->format eq "HTML";
171             }
172              
173             =item params
174              
175             This is a hash reference of the parameters to be supplied with the request, if any. The valid parameters depend upon the service being accessed and the operation being performed.
176              
177             =cut
178              
179             sub params {
180 4     4 1 7 my $self = shift;
181 4 100       14 if (@_) {
182 1         3 $self->{params} = shift;
183             }
184              
185 4 50       13 if ( $self->format eq "JSON" ) {
186              
187             # If we have no params, just return.
188 4 100       19 return unless defined $self->{params};
189 2         19 require JSON;
190 2         13 JSON->import('encode_json');
191 2         269 return encode_json( $self->{params} );
192             }
193 0 0       0 if ( $self->format eq "YAML" ) {
194              
195             # If we have no params, just return.
196 0 0       0 return unless defined $self->{params};
197 0         0 require YAML;
198 0         0 YAML->import('Dump');
199 0         0 return Dump( $self->{params} );
200             }
201 0         0 return $self->{params};
202             }
203              
204             sub _as_string {
205 1     1   2 my $self = shift;
206 1         3 my @texts;
207 1 50       6 push @texts, sprintf "Operation: '%s'", $self->operation
208             if defined $self->operation;
209 1 50       5 push @texts, sprintf "Service: '%s'", $self->service
210             if defined $self->service;
211 1 50       5 push @texts, sprintf "Format: '%s'", $self->format
212             if defined $self->operation;
213 1 50       7 push @texts, sprintf "Params: '%s'", $self->params if defined $self->params;
214 1         9 return join( "\n", @texts );
215             }
216              
217             =back
218              
219             =head1 SEE ALSO
220              
221             L, L.
222              
223             =head1 AUTHOR
224              
225             James bromberger, james@rcpt.to
226              
227             =head1 COPYRIGHT AND LICENSE
228              
229             Copyright (C) 2010 by James Bromberger
230              
231             This library is free software; you can redistribute it and/or modify
232             it under the same terms as Perl itself, either Perl version 5.10.1 or,
233             at your option, any later version of Perl 5 you may have available.
234              
235             =cut
236              
237             1;