DNS.pm

filter: Statementstmt 26.6% 24 / 90 Branchbran 6.2% 2 / 32 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 63.6% 7 / 11 Podpod 100% 6 / 6 totaltotal 25.0% 39 / 156 CC 23 SCAR 34.5
CC
23
Cov
20%
sethosts
72.0
gethosts
42.0
setnameservers
30.0
CRAP 31.5
↑ Summary Domain.pm »
1 package WWW::Namecheap::DNS;
2  
3 12 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 WWW::Namecheap::DNS - Namecheap API DNS methods
11  
12 =cut
13  
14 12 our $VERSION = '0.06';
15  
16 =head1 SYNOPSIS
17  
18 Namecheap API DNS methods.
19  
20 See L<WWW::Namecheap::API> for main documentation.
21  
22 use WWW::Namecheap::DNS;
23  
24 my $dns = WWW::Namecheap::DNS->new(API => $api);
25 $dns->sethosts(...);
26 ...
27  
28 =head1 SUBROUTINES/METHODS
29  
30 =head2 WWW::Namecheap::Domain->new(API => $api)
31  
32 Instantiate a new DNS object for making DNS-related API calls.
33 Requires a WWW::Namecheap::API object.
34  
35 =cut
36  
37 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
38 2 my $class = shift;
39  
40 2 my $params = _argparse(@_);
41  
42 2 for (qw(API)) {
43 2 ▶ Carp::croak("${class}->new(): Mandatory parameter $_ not provided.") unless $params->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$_} 0 2 2
44 }
45  
46 my $self = {
47 2 api => $params->{'API'},
48 };
49  
50 2 return bless($self, $class);
51 }
52  
53 =head2 $dns->setnameservers(%hash)
54  
55 Set the nameservers used by a domain under your management. They may
56 be set to custom nameservers or the Namecheap default nameservers.
57  
58 my $result = $dns->setnameservers(
59 DomainName => 'example.com',
60 Nameservers => [
61 'ns1.example.com',
62 'ns2.example.com',
63 ],
64 DefaultNS => 0,
65 );
66  
67 or, for the Namecheap default:
68  
69 my $result = $dns->setnameservers(
70 DomainName => 'example.com',
71 DefaultNS => 1,
72 );
73  
74 $result is a small hashref confirming back the domain that was modified
75 and whether the operation was successful or not:
76  
77 $result = {
78 Domain => 'example.com',
79 Update => 'true',
80 };
81  
82 =cut
83  
84 0 ▶sub setnameservers {
Subroutine setnameservers: not called CC=5 SCAR=34.0
Pod documented
85 0 my $self = shift;
86  
87 0 my $params = _argparse(@_);
88  
89 0 ▶ return unless $params->{DomainName};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"DomainName"} 0 0 0
90  
91 my %request = (
92 ClientIp => $params->{'ClientIp'},
93 0 UserName => $params->{'UserName'},
94 );
95  
96 0 ▶ if ($params->{DefaultNS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'DefaultNS'}) { } 0 0 0
97 0 $request{Command} = 'namecheap.domains.dns.setDefault';
98 } else {
99 0 $request{Command} = 'namecheap.domains.dns.setCustom';
100 0 $request{Nameservers} = join(',', @{$params->{Nameservers}});
101 }
102  
103 0 my ($sld, $tld) = split(/[.]/, $params->{DomainName}, 2);
104 0 $request{SLD} = $sld;
105 0 $request{TLD} = $tld;
106  
107 0 my $xml = $self->api->request(%request);
108  
109 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
110  
111 0 ▶ if ($params->{DefaultNS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'DefaultNS'}) { } 0 0 0
112 0 return $xml->{CommandResponse}->{DomainDNSSetDefaultResult};
113 } else {
114 0 return $xml->{CommandResponse}->{DomainDNSSetCustomResult};
115 }
116 }
117  
118 =head2 $dns->getnameservers(DomainName => 'example.com')
119  
120 Get a list of nameservers currently associated with a domain under
121 your management. Returns a data structure that looks like this:
122  
123 $nameservers = {
124 Domain => 'example.com',
125 IsUsingOurDNS => 'true', # if using the "default NS" option
126 Nameserver => [
127 'ns1.example.com',
128 'ns2.example.com',
129 ],
130 };
131  
132 =cut
133  
134 0 ▶sub getnameservers {
Subroutine getnameservers: not called CC=3 SCAR=24.8
Pod documented
135 0 my $self = shift;
136  
137 0 my $params = _argparse(@_);
138  
139 0 ▶ return unless $params->{DomainName};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"DomainName"} 0 0 0
140  
141 my %request = (
142 Command => 'namecheap.domains.dns.getList',
143 ClientIp => $params->{'ClientIp'},
144 0 UserName => $params->{'UserName'},
145 );
146  
147 0 my ($sld, $tld) = split(/[.]/, $params->{DomainName}, 2);
148 0 $request{SLD} = $sld;
149 0 $request{TLD} = $tld;
150  
151 0 my $xml = $self->api->request(%request);
152  
153 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
154  
155 0 return $xml->{CommandResponse}->{DomainDNSGetListResult};
156 }
157  
158 =head2 $dns->gethosts(DomainName => 'example.com')
159  
160 Get a list of DNS hosts for a domain name under management and using the
161 Namecheap provided DNS service. Returns a data structure as follows:
162  
163 $hosts = {
164 Domain => 'example.com',
165 IsUsingOurDNS => 'true',
166 Host => [
167 {
168 HostId => '10',
169 Name => '@',
170 Type => 'A',
171 Address => '1.2.3.4',
172 MXPref => 10, # yes, even for non-MX records
173 },
174 ...
175 ],
176 };
177  
178 See the documentation for the sethosts() method for more details of the
179 possible values of each of these fields.
180  
181 =cut
182  
183 0 ▶sub gethosts {
Subroutine gethosts: not called CC=6 SCAR=37.4
Pod documented
184 0 my $self = shift;
185  
186 0 my $params = _argparse(@_);
187  
188 0 ▶ return unless $params->{DomainName};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"DomainName"} 0 0 0
189  
190 my %request = (
191 Command => 'namecheap.domains.dns.getHosts',
192 ClientIp => $params->{'ClientIp'},
193 0 UserName => $params->{'UserName'},
194 );
195  
196 0 my ($sld, $tld) = split(/[.]/, $params->{DomainName}, 2);
197 0 $request{SLD} = $sld;
198 0 $request{TLD} = $tld;
199  
200 0 my $xml = $self->api->request(%request);
201  
202 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
203  
204 0 ▶ unless ($xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"}) 0 0 0
205 0 $xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host} = $xml->{CommandResponse}->{DomainDNSGetHostsResult}->{host};
206 }
207  
208 0 ▶ if ($xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"} and ref $xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"} eq "HASH") 0 0 0
Condition0%0/3
$xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"} and ref $xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"} and ref $xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"} eq "HASH"
$xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"} ref $xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"} eq "HASH"
Truth table0%0/3
A and B
A= $xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"}B= ref $xml->{"CommandResponse"}{"DomainDNSGetHostsResult"}{"Host"} eq "HASH"
ABresult
0X0
100
111
209 ref($xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host}) eq 'HASH') {
210 0 my $arrayref = [ $xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host} ];
211 0 $xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host} = $arrayref;
212 }
213  
214 0 return $xml->{CommandResponse}->{DomainDNSGetHostsResult};
215 }
216  
217 =head2 $dns->sethosts
218  
219 Set DNS hosts for a domain.
220  
221 IMPORTANT NOTE: You must include all hosts for the domain in your sethosts
222 command, any hosts not present in the command but previously present in the
223 domain configuration will be deleted. You can simply modify the arrayref
224 you get back from gethosts and pass it back in, we'll even strip out the
225 HostIds for you! :)
226  
227 my $result = $dns->sethosts(
228 DomainName => 'example.com',
229 Hosts => [
230 {
231 Name => 'foo', # do not include domain name
232 Type => 'A',
233 Address => '1.2.3.4',
234 TTL => 1800, # optional, default 1800
235 },
236 {
237 Name => '@', # for example.com itself
238 Type => 'MX',
239 Address => 'mail.example.com',
240 MXPref => 10,
241 },
242 ],
243 EmailType => 'MX', # or 'MXE' or 'FWD'
244 );
245  
246 Results in a not very useful response:
247  
248 $result = {
249 Domain => 'example.com',
250 IsSuccess => 'true', # or false
251 };
252  
253 Further verification can be performed using a subsequent gethosts() call.
254  
255 Possible values for "Type" are listed below. All relevant data (including
256 URLs for the URL, URL301, and FRAME types) goes into the "Address" field.
257  
258 * A
259 * AAAA
260 * CNAME
261 * MX
262 * MXE
263 * TXT
264 * URL
265 * URL301
266 * FRAME
267  
268 Type=MXE expects an IP address and synthesizes appropriate MX and host
269 records.
270  
271 =cut
272  
273 0 ▶sub sethosts {
Subroutine sethosts: not called CC=8 SCAR=42.8
Pod documented
274 0 my $self = shift;
275  
276 0 my $params = _argparse(@_);
277  
278 0 ▶ return unless $params->{DomainName};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"DomainName"} 0 0 0
279  
280 my %request = (
281 Command => 'namecheap.domains.dns.setHosts',
282 ClientIp => $params->{'ClientIp'},
283 UserName => $params->{'UserName'},
284 0 EmailType => $params->{'EmailType'},
285 );
286  
287 0 my ($sld, $tld) = split(/[.]/, $params->{DomainName}, 2);
288 0 $request{SLD} = $sld;
289 0 $request{TLD} = $tld;
290  
291 0 my $hostcount = 1;
292 0 foreach my $host (@{$params->{Hosts}}) {
293 0 ▶ next unless ($host->{Name} && $host->{Type} && $host->{Address});
Branch: 1 branch
BranchTrueFalseTotal
unless $host->{"Name"} and $host->{"Type"} and $host->{"Address"} 0 0 0
Condition0%0/6
$host->{"Name"} and $host->{"Type"} and $host->{"Address"}
!ll&&!rl&&r
000
$host->{"Name"} and $host->{"Type"}
!ll&&!rl&&r
000
MC/DC0%0/3
$host->{"Name"} and $host->{"Type"} and $host->{"Address"}
$host->{"Name"} $host->{"Type"} $host->{"Address"}
Truth table0%0/4
A and B and C
A= $host->{"Name"}B= $host->{"Type"}C= $host->{"Address"}
ABCresult
0XX0
10X0
1100
1111
294 0 $host->{Name} =~ s/[.]$params->{DomainName}\.?$//;
295 0 $host->{Name} =~ s/^$params->{DomainName}\.?$/\@/;
296 0 $request{"HostName$hostcount"} = $host->{Name};
297 0 $request{"RecordType$hostcount"} = $host->{Type};
298 0 $request{"Address$hostcount"} = $host->{Address};
299 0 ▶ $request{"MXPref$hostcount"} = $host->{MXPref} || 10;
Condition0%0/2
$host->{'MXPref'} || 10
l!l
00
MC/DC0%0/1
$host->{'MXPref'} || 10
$host->{'MXPref'}
Truth table0%0/2
A
A= $host->{'MXPref'}
Aresult
11
00
300 0 $request{"TTL$hostcount"} = $host->{TTL};
301 0 $hostcount++;
302 }
303  
304 0 my $xml = $self->api->request(%request);
305  
306 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
307  
308 0 return $xml->{CommandResponse}->{DomainDNSSetHostsResult};
309 }
310  
311 =head2 $dns->api()
312  
313 Accessor for internal API object.
314  
315 =cut
316  
317 1 ▶sub api {
Subroutine api: called CC=1 SCAR=0.0
Pod documented
318 1 return $_[0]->{api};
319 }
320  
321 2 ▶sub _argparse {
Subroutine _argparse: called CC=3 SCAR=14.2
322 2 my $hashref;
323 2 ▶ if (@_ % 2 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 2 0 2
elsif (ref $_[0] eq 'HASH') { } 0 0 0
324 2 $hashref = { @_ }
325 } elsif (ref($_[0]) eq 'HASH') {
326 0 $hashref = \%{$_[0]};
327 }
328 2 return $hashref;
329 }
330  
331 =head1 AUTHOR
332  
333 Tim Wilde, C<< <twilde at cpan.org> >>
334  
335 =head1 BUGS
336  
337 Please report any bugs or feature requests to C<bug-www-namecheap-api at rt.cpan.org>, or through
338 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Namecheap-API>. I will be notified, and then you'll
339 automatically be notified of progress on your bug as I make changes.
340  
341  
342  
343 =head1 SUPPORT
344  
345 You can find documentation for this module with the perldoc command.
346  
347 perldoc WWW::Namecheap::DNS
348  
349  
350 You can also look for information at:
351  
352 =over 4
353  
354 =item * RT: CPAN's request tracker (report bugs here)
355  
356 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Namecheap-API>
357  
358 =item * AnnoCPAN: Annotated CPAN documentation
359  
360 L<http://annocpan.org/dist/WWW-Namecheap-API>
361  
362 =item * CPAN Ratings
363  
364 L<http://cpanratings.perl.org/d/WWW-Namecheap-API>
365  
366 =item * Search CPAN
367  
368 L<http://search.cpan.org/dist/WWW-Namecheap-API/>
369  
370 =back
371  
372  
373 =head1 ACKNOWLEDGEMENTS
374  
375  
376 =head1 LICENSE AND COPYRIGHT
377  
378 Copyright 2011 Tim Wilde.
379  
380 This program is free software; you can redistribute it and/or modify it
381 under the terms of either: the GNU General Public License as published
382 by the Free Software Foundation; or the Artistic License.
383  
384 See http://dev.perl.org/licenses/ for more information.
385  
386  
387 =cut
388  
389 12 1; # End of WWW::Namecheap::DNS
↑ Summary Domain.pm »