File Coverage

blib/lib/WWW/Namecheap/DNS.pm
Criterion Covered Total %
statement 22 88 25.0
branch 2 32 6.2
condition 0 11 0.0
subroutine 7 11 63.6
pod 6 6 100.0
total 37 148 25.0


line stmt bran cond sub pod time code
1             package WWW::Namecheap::DNS;
2              
3 11     11   143 use 5.006;
  11         23  
4 11     11   36 use strict;
  11         29  
  11         180  
5 11     11   31 use warnings;
  11         9  
  11         200  
6 11     11   30 use Carp();
  11         7  
  11         8261  
7              
8             =head1 NAME
9              
10             WWW::Namecheap::DNS - Namecheap API DNS methods
11              
12             =cut
13              
14             our $VERSION = '0.06';
15              
16             =head1 SYNOPSIS
17              
18             Namecheap API DNS methods.
19              
20             See L 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             sub new {
38 2     2 1 3 my $class = shift;
39              
40 2         5 my $params = _argparse(@_);
41              
42 2         4 for (qw(API)) {
43 2 50       6 Carp::croak("${class}->new(): Mandatory parameter $_ not provided.") unless $params->{$_};
44             }
45              
46             my $self = {
47 2         3 api => $params->{'API'},
48             };
49              
50 2         8 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             sub setnameservers {
85 0     0 1 0 my $self = shift;
86              
87 0         0 my $params = _argparse(@_);
88              
89 0 0       0 return unless $params->{DomainName};
90              
91             my %request = (
92             ClientIp => $params->{'ClientIp'},
93 0         0 UserName => $params->{'UserName'},
94             );
95              
96 0 0       0 if ($params->{DefaultNS}) {
97 0         0 $request{Command} = 'namecheap.domains.dns.setDefault';
98             } else {
99 0         0 $request{Command} = 'namecheap.domains.dns.setCustom';
100 0         0 $request{Nameservers} = join(',', @{$params->{Nameservers}});
  0         0  
101             }
102              
103 0         0 my ($sld, $tld) = split(/[.]/, $params->{DomainName}, 2);
104 0         0 $request{SLD} = $sld;
105 0         0 $request{TLD} = $tld;
106              
107 0         0 my $xml = $self->api->request(%request);
108              
109 0 0       0 return unless $xml;
110              
111 0 0       0 if ($params->{DefaultNS}) {
112 0         0 return $xml->{CommandResponse}->{DomainDNSSetDefaultResult};
113             } else {
114 0         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             sub getnameservers {
135 0     0 1 0 my $self = shift;
136              
137 0         0 my $params = _argparse(@_);
138              
139 0 0       0 return unless $params->{DomainName};
140              
141             my %request = (
142             Command => 'namecheap.domains.dns.getList',
143             ClientIp => $params->{'ClientIp'},
144 0         0 UserName => $params->{'UserName'},
145             );
146              
147 0         0 my ($sld, $tld) = split(/[.]/, $params->{DomainName}, 2);
148 0         0 $request{SLD} = $sld;
149 0         0 $request{TLD} = $tld;
150              
151 0         0 my $xml = $self->api->request(%request);
152              
153 0 0       0 return unless $xml;
154              
155 0         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             sub gethosts {
184 0     0 1 0 my $self = shift;
185              
186 0         0 my $params = _argparse(@_);
187              
188 0 0       0 return unless $params->{DomainName};
189              
190             my %request = (
191             Command => 'namecheap.domains.dns.getHosts',
192             ClientIp => $params->{'ClientIp'},
193 0         0 UserName => $params->{'UserName'},
194             );
195              
196 0         0 my ($sld, $tld) = split(/[.]/, $params->{DomainName}, 2);
197 0         0 $request{SLD} = $sld;
198 0         0 $request{TLD} = $tld;
199              
200 0         0 my $xml = $self->api->request(%request);
201              
202 0 0       0 return unless $xml;
203              
204 0 0       0 unless ($xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host}) {
205 0         0 $xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host} = $xml->{CommandResponse}->{DomainDNSGetHostsResult}->{host};
206             }
207              
208 0 0 0     0 if ($xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host} &&
209             ref($xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host}) eq 'HASH') {
210 0         0 my $arrayref = [ $xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host} ];
211 0         0 $xml->{CommandResponse}->{DomainDNSGetHostsResult}->{Host} = $arrayref;
212             }
213              
214 0         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             sub sethosts {
274 0     0 1 0 my $self = shift;
275              
276 0         0 my $params = _argparse(@_);
277              
278 0 0       0 return unless $params->{DomainName};
279              
280             my %request = (
281             Command => 'namecheap.domains.dns.setHosts',
282             ClientIp => $params->{'ClientIp'},
283             UserName => $params->{'UserName'},
284 0         0 EmailType => $params->{'EmailType'},
285             );
286              
287 0         0 my ($sld, $tld) = split(/[.]/, $params->{DomainName}, 2);
288 0         0 $request{SLD} = $sld;
289 0         0 $request{TLD} = $tld;
290              
291 0         0 my $hostcount = 1;
292 0         0 foreach my $host (@{$params->{Hosts}}) {
  0         0  
293 0 0 0     0 next unless ($host->{Name} && $host->{Type} && $host->{Address});
      0        
294 0         0 $host->{Name} =~ s/[.]$params->{DomainName}\.?$//;
295 0         0 $host->{Name} =~ s/^$params->{DomainName}\.?$/\@/;
296 0         0 $request{"HostName$hostcount"} = $host->{Name};
297 0         0 $request{"RecordType$hostcount"} = $host->{Type};
298 0         0 $request{"Address$hostcount"} = $host->{Address};
299 0   0     0 $request{"MXPref$hostcount"} = $host->{MXPref} || 10;
300 0         0 $request{"TTL$hostcount"} = $host->{TTL};
301 0         0 $hostcount++;
302             }
303              
304 0         0 my $xml = $self->api->request(%request);
305              
306 0 0       0 return unless $xml;
307              
308 0         0 return $xml->{CommandResponse}->{DomainDNSSetHostsResult};
309             }
310              
311             =head2 $dns->api()
312              
313             Accessor for internal API object.
314              
315             =cut
316              
317             sub api {
318 1     1 1 223 return $_[0]->{api};
319             }
320              
321             sub _argparse {
322 2     2   1 my $hashref;
323 2 50       6 if (@_ % 2 == 0) {
    0          
324 2         5 $hashref = { @_ }
325             } elsif (ref($_[0]) eq 'HASH') {
326 0         0 $hashref = \%{$_[0]};
  0         0  
327             }
328 2         2 return $hashref;
329             }
330              
331             =head1 AUTHOR
332              
333             Tim Wilde, C<< >>
334              
335             =head1 BUGS
336              
337             Please report any bugs or feature requests to C, or through
338             the web interface at L. 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
357              
358             =item * AnnoCPAN: Annotated CPAN documentation
359              
360             L
361              
362             =item * CPAN Ratings
363              
364             L
365              
366             =item * Search CPAN
367              
368             L
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             1; # End of WWW::Namecheap::DNS