blib/lib/WWW/Namecheap/Domain.pm

filter: Statementstmt 19.3% 24 / 124 Branchbran 5.0% 2 / 40 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 43.7% 7 / 16 Podpod 100% 11 / 11 totaltotal 20.7% 44 / 212 SCAR 63.8
CC
30
Cov
15%
create
42.0
check
30.0
list
30.0
CRAP 588.8
↑ Summary blib/lib/WWW/Namecheap/DNS.pm »
1 package WWW::Namecheap::Domain;
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::Domain - Namecheap API domain methods
11  
12 =cut
13  
14 12 our $VERSION = '0.05';
15  
16 =head1 SYNOPSIS
17  
18 Namecheap API domain methods.
19  
20 See L<WWW::Namecheap::API> for main documentation.
21  
22 use WWW::Namecheap::Domain;
23  
24 my $domain = WWW::Namecheap::Domain->new(API => $api);
25 $domain->check(...);
26 $domain->create(...);
27 ...
28  
29 =head1 SUBROUTINES/METHODS
30  
31 =head2 WWW::Namecheap::Domain->new(API => $api)
32  
33 Instantiate a new Domain object for making domain-related API calls.
34 Requires a WWW::Namecheap::API object.
35  
36 =cut
37  
38 sub new {
39 2 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
40
41 2 my $params = _argparse(@_);
42
43 2 for (qw(API)) {
44 2 ▶ Carp::croak("${class}->new(): Mandatory parameter $_ not provided.") unless $params->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{$_} 0 2 2
45 }
46
47 my $self = {
48 2 api => $params->{'API'},
49 };
50
51 2 return bless($self, $class);
52 }
53  
54 =head2 $domain->check(Domains => ['example.com'])
55  
56 Check a list of domains. Returns a hashref of availablity status with
57 domain names as the keys and 0/1 as the values for not available/available.
58  
59 my $result = $domain->check(Domains => [qw(
60 example.com
61 example2.com
62 foobar.com
63 )]);
64
65 Will give a $result something like:
66  
67 $result = {
68 'example.com' => 0, # example.com is taken
69 'example2.com' => 1, # example2.com is available
70 'foobar.com' => 0, # damn, foobar.com is taken
71 };
72  
73 =cut
74  
75 sub check {
76 0 ▶ my $self = shift;
Subroutine check: not called CC=5 SCAR=34.0
Pod documented
77
78 0 my $params = _argparse(@_);
79
80 0 my %domains = map { $_ => -1 } @{$params->{'Domains'}};
81 0 my $DomainList = join(',', keys %domains);
82 my $xml = $self->api->request(
83 Command => 'namecheap.domains.check',
84 ClientIp => $params->{'ClientIp'},
85 0 UserName => $params->{'UserName'},
86 DomainList => $DomainList,
87 );
88
89 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
90
91 0 foreach my $entry (@{$xml->{CommandResponse}->{DomainCheckResult}}) {
92 0 ▶ unless ($domains{$entry->{Domain}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($domains{$entry->{"Domain"}}) 0 0 0
93 0 Carp::carp("Unexpected domain found: $entry->{Domain}");
94 0 next;
95 }
96 0 ▶ if ($entry->{Available} eq 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{'Available'} eq 'true') { } 0 0 0
97 0 $domains{$entry->{Domain}} = 1;
98 } else {
99 0 $domains{$entry->{Domain}} = 0;
100 }
101 }
102
103 0 return \%domains;
104 }
105  
106 =head2 $domain->create(%hash)
107  
108 Register a new domain name.
109  
110 Example:
111  
112 my $result = $domain->create(
113 UserName => 'username', # optional if DefaultUser specified in $api
114 ClientIp => '1.2.3.4', # optional if DefaultIp specified in $api
115 DomainName => 'example.com',
116 Years => 1,
117 Registrant => {
118 OrganizationName => 'Example Dot Com', # optional
119 FirstName => 'Domain',
120 LastName => 'Manager',
121 Address1 => '123 Fake Street',
122 Address2 => 'Suite 555', # optional
123 City => 'Univille',
124 StateProvince => 'SD',
125 StateProvinceChoice => 'S', # for 'State' or 'P' for 'Province'
126 PostalCode => '12345',
127 Country => 'US',
128 Phone => '+1.2025551212',
129 Fax => '+1.2025551212', # optional
130 EmailAddress => 'foo@example.com',
131 },
132 Tech => {
133 # same fields as Registrant
134 },
135 Admin => {
136 # same fields as Registrant
137 },
138 AuxBilling => {
139 # same fields as Registrant
140 },
141 Nameservers => 'ns1.foo.com,ns2.bar.com', # optional
142 AddFreeWhoisguard => 'yes', # or 'no', default 'yes'
143 WGEnabled => 'yes', # or 'no', default 'yes'
144 );
145  
146 Unspecified contacts will be automatically copied from the registrant, which
147 must be provided.
148  
149 Returns:
150  
151 $result = {
152 Domain => 'example.com',
153 DomainID => '12345',
154 Registered => 'true',
155 OrderID => '12345',
156 TransactionID => '12345',
157 ChargedAmount => '10.45', # dollars and cents
158 };
159  
160 =cut
161  
162 sub create {
163 0 ▶ my $self = shift;
Subroutine create: not called CC=6 SCAR=37.4
Pod documented
164
165 0 my $params = _argparse(@_);
166
167 my %request = (
168 Command => 'namecheap.domains.create',
169 ClientIp => $params->{'ClientIp'},
170 UserName => $params->{'UserName'},
171 DomainName => $params->{'DomainName'},
172 Years => $params->{Years},
173 Nameservers => $params->{'Nameservers'},
174 AddFreeWhoisguard => $params->{'AddFreeWhoisguard'} || 'yes',
175 0 ▶ WGEnabled => $params->{'WGEnabled'} || 'yes',
Condition0%0/4
$params->{'AddFreeWhoisguard'} || 'yes'
l!l
00
$params->{'WGEnabled'} || 'yes'
l!l
00
MC/DC0%0/1
$params->{'AddFreeWhoisguard'} || 'yes'
$params->{'AddFreeWhoisguard'}
MC/DC0%0/1
$params->{'WGEnabled'} || 'yes'
$params->{'WGEnabled'}
Truth table0%0/2
A
A= $params->{'AddFreeWhoisguard'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'WGEnabled'}
Aresult
11
00
176 );
177
178 0 foreach my $contact (qw(Registrant Tech Admin AuxBilling)) {
179 0 ▶ $params->{$contact} ||= $params->{Registrant};
Condition0%0/3
$params->{$contact} ||= $params->{"Registrant"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{$contact} ||= $params->{"Registrant"}
$params->{$contact} $params->{"Registrant"}
Truth table0%0/3
A ||= B
A= $params->{$contact}B= $params->{"Registrant"}
ABresult
1X1
011
000
180 0 map { $request{"$contact$_"} = $params->{$contact}{$_} } keys %{$params->{$contact}};
181 }
182
183 0 my $xml = $self->api->request(%request);
184
185 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
186
187 0 return $xml->{CommandResponse}->{DomainCreateResult};
188 }
189  
190 =head2 $domain->list(%hash)
191  
192 Get a list of domains in your account. Automatically handles the Namecheap
193 "paging" to get a full list. May be optionally restricted:
194  
195 my $domains = $domain->list(
196 ListType => 'ALL', # or EXPIRING or EXPIRED
197 SearchTerm => 'foo', # keyword search
198 SortBy => 'NAME', # or EXPIREDATE, CREATEDATE, or *_DESC
199 );
200  
201 Returns an arrayref of hashrefs:
202  
203 $domains = [
204 {
205 ID => '123',
206 Name => 'example.com',
207 User => 'owner',
208 Created => 'MM/DD/YYYY',
209 Expires => 'MM/DD/YYYY',
210 IsExpired => 'false',
211 IsLocked => 'true',
212 AutoRenew => 'false',
213 WhoisGuard => 'ENABLED',
214 },
215 ...
216 ];
217  
218 =cut
219  
220 sub list {
221 0 ▶ my $self = shift;
Subroutine list: not called CC=5 SCAR=34.0
Pod documented
222
223 0 my $params = _argparse(@_);
224
225 my %request = (
226 Command => 'namecheap.domains.getList',
227 ClientIp => $params->{'ClientIp'},
228 UserName => $params->{'UserName'},
229 PageSize => 100,
230 Page => 1,
231 ListType => $params->{'ListType'},
232 0 SearchTerm => $params->{'SearchTerm'},
233 );
234
235 0 my @domains;
236
237 0 my $break = 0;
238 0 while (1) {
239 0 my $xml = $self->api->request(%request);
240
241 0 ▶ last unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
242
243 0 ▶ if (ref($xml->{CommandResponse}->{DomainGetListResult}->{Domain}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $xml->{'CommandResponse'}{'DomainGetListResult'}{'Domain'} eq 'ARRAY') { } 0 0 0
elsif (ref $xml->{'CommandResponse'}{'DomainGetListResult'}{'Domain'} eq 'HASH') { } 0 0 0
244 0 push(@domains, @{$xml->{CommandResponse}->{DomainGetListResult}->{Domain}});
245 } elsif (ref($xml->{CommandResponse}->{DomainGetListResult}->{Domain}) eq 'HASH') {
246 0 push(@domains, $xml->{CommandResponse}->{DomainGetListResult}->{Domain});
247 } else {
248 0 Carp::carp('Unexpected XML in CommandResponse->DomainGetListResult->Domain!');
249 }
250 0 ▶ if ($xml->{CommandResponse}->{Paging}->{TotalItems} <= ($request{Page} * $request{PageSize})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->{'CommandResponse'}{'Paging'}{'TotalItems'} <= $request{'Page'} * $request{'PageSize'}) { } 0 0 0
251 0 last;
252 } else {
253 0 $request{Page}++;
254 }
255 }
256
257 0 return \@domains;
258 }
259  
260 =head2 $domain->getcontacts(DomainName => 'example.com')
261  
262 Get the contacts on file for the provided DomainName. Returns a big
263 ol' data structure:
264  
265 $contacts = {
266 Domain => 'example.com',
267 domainnameid => '12345',
268 Registrant => {
269 ReadOnly => 'false',
270 ... all contact fields from create ...
271 },
272 Tech => {
273 ... ditto ...
274 },
275 Admin => {
276 ... ditto ...
277 },
278 AuxBilling => {
279 ... ditto ...
280 },
281 WhoisGuardContact => {
282 ... same contacts as outside, except the actual published
283 WhoisGuard info, ReadOnly => 'true' ...
284 },
285 };
286  
287 =cut
288  
289 sub getcontacts {
290 0 ▶ my $self = shift;
Subroutine getcontacts: not called CC=3 SCAR=24.8
Pod documented
291
292 0 my $params = _argparse(@_);
293
294 0 ▶ return unless $params->{'DomainName'};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"DomainName"} 0 0 0
295
296 0 my %request = (
297 Command => 'namecheap.domains.getContacts',
298 %$params,
299 );
300
301 0 my $xml = $self->api->request(%request);
302
303 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
304
305 0 return $xml->{CommandResponse}->{DomainContactsResult};
306 }
307  
308 =head2 $domain->setcontacts(%hash)
309  
310 Set contacts for a domain name.
311  
312 Example:
313  
314 my $result = $domain->create(
315 UserName => 'username', # optional if DefaultUser specified in $api
316 ClientIp => '1.2.3.4', # optional if DefaultIp specified in $api
317 DomainName => 'example.com',
318 Registrant => {
319 OrganizationName => 'Example Dot Com', # optional
320 FirstName => 'Domain',
321 LastName => 'Manager',
322 Address1 => '123 Fake Street',
323 Address2 => 'Suite 555', # optional
324 City => 'Univille',
325 StateProvince => 'SD',
326 StateProvinceChoice => 'S', # for 'State' or 'P' for 'Province'
327 PostalCode => '12345',
328 Country => 'USA',
329 Phone => '+1.2025551212',
330 Fax => '+1.2025551212', # optional
331 EmailAddress => 'foo@example.com',
332 },
333 Tech => {
334 # same fields as Registrant
335 },
336 Admin => {
337 # same fields as Registrant
338 },
339 AuxBilling => {
340 # same fields as Registrant
341 },
342 );
343  
344 Unspecified contacts will be automatically copied from the registrant, which
345 must be provided.
346  
347 $result is a small hashref confirming back the domain that was modified
348 and whether the operation was successful or not:
349  
350 $result = {
351 Domain => 'example.com',
352 IsSuccess => 'true',
353 };
354  
355 =cut
356  
357 sub setcontacts {
358 0 ▶ my $self = shift;
Subroutine setcontacts: not called CC=5 SCAR=34.0
Pod documented
359
360 0 my $params = _argparse(@_);
361
362 0 ▶ return unless $params->{'DomainName'};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"DomainName"} 0 0 0
363
364 my %request = (
365 Command => 'namecheap.domains.setContacts',
366 ClientIp => $params->{'ClientIp'},
367 UserName => $params->{'UserName'},
368 0 DomainName => $params->{'DomainName'},
369 );
370
371 0 foreach my $contact (qw(Registrant Tech Admin AuxBilling)) {
372 0 ▶ $params->{$contact} ||= $params->{Registrant};
Condition0%0/3
$params->{$contact} ||= $params->{"Registrant"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{$contact} ||= $params->{"Registrant"}
$params->{$contact} $params->{"Registrant"}
Truth table0%0/3
A ||= B
A= $params->{$contact}B= $params->{"Registrant"}
ABresult
1X1
011
000
373 0 map { $request{"$contact$_"} = $params->{$contact}{$_} } keys %{$params->{$contact}};
374 }
375
376 0 my $xml = $self->api->request(%request);
377
378 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
379
380 0 return $xml->{CommandResponse}->{DomainSetContactResult};
381 }
382  
383 =head2 $domain->gettldlist()
384  
385 Get a list of all TLDs available for registration, along with various
386 attributes for each TLD. Results are automatically cached for one
387 hour to avoid excessive API load.
388  
389 =cut
390  
391 sub gettldlist {
392 0 ▶ my $self = shift;
Subroutine gettldlist: not called CC=3 SCAR=24.8
Pod documented
393
394 0 my $params = _argparse(@_);
395
396 0 my %request = (
397 Command => 'namecheap.domains.getTldList',
398 %$params,
399 );
400
401 0 ▶ if (!$self->{_tldlist_cachetime} || time() - $self->{_tldlist_cachetime} > 3600) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_tldlist_cachetime"} or time - $self->{"_tldlist_cachetime"} > 3600) 0 0 0
Condition0%0/3
not $self->{"_tldlist_cachetime"} or time - $self->{"_tldlist_cachetime"} > 3600
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"_tldlist_cachetime"} or time - $self->{"_tldlist_cachetime"} > 3600
not $self->{"_tldlist_cachetime"} time - $self->{"_tldlist_cachetime"} > 3600
Truth table0%0/3
not(A) or B
A= not $self->{"_tldlist_cachetime"}B= time - $self->{"_tldlist_cachetime"} > 3600
ABresult
1X1
011
000
402 0 my $xml = $self->api->request(%request);
403 0 $self->{_tldlist_cache} = $xml->{CommandResponse}->{Tlds}->{Tld};
404 0 $self->{_tldlist_cachetime} = time();
405 }
406
407 0 return $self->{_tldlist_cache};
408 }
409  
410 =head2 $domain->transfer(%hash)
411  
412 Initiate a transfer in request to Namecheap from another registrar.
413 Request should look something like:
414  
415 my $transfer = $domain->transfer(
416 DomainName => 'example.com',
417 Years => 1,
418 EPPCode => 'foobarbaz',
419 );
420  
421 The response will be a hashref:
422  
423 $transfer = {
424 Transfer => 'true',
425 TransferID => '15',
426 StatusID => '-1',
427 OrderID => '1234',
428 TransactionID => '1234',
429 ChargedAmount => '10.10',
430 };
431  
432 For transfer status code details, see the Namecheap API documentation:
433  
434 L<https://www.namecheap.com/support/api/domains-transfer/transfer-statuses.aspx>
435  
436 =cut
437  
438 sub transfer {
439 0 ▶ my $self = shift;
Subroutine transfer: not called CC=2 SCAR=17.9
Pod documented
440
441 0 my $params = _argparse(@_);
442
443 0 my %request = (
444 Command => 'namecheap.domains.transfer.create',
445 %$params,
446 );
447
448 0 my $xml = $self->api->request(%request);
449
450 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
451
452 0 return $xml->{CommandResponse}->{DomainTransferCreateResult};
453 }
454  
455 =head2 $domain->transferstatus(TransferID => '1234')
456  
457 Check the current status of a particular transfer. The TransferID
458 is the TransferID returned by the transfer() call, or included in
459 the transferlist(). Returns a hashref:
460  
461 $result = {
462 TransferID => '1234',
463 Status => 'String',
464 StatusID => '-1',
465 };
466  
467 =cut
468  
469 sub transferstatus {
470 0 ▶ my $self = shift;
Subroutine transferstatus: not called CC=2 SCAR=17.9
Pod documented
471
472 0 my $params = _argparse(@_);
473
474 0 my %request = (
475 Command => 'namecheap.domains.transfer.getStatus',
476 %$params,
477 );
478
479 0 my $xml = $self->api->request(%request);
480
481 0 ▶ return unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
482
483 0 return $xml->{CommandResponse}->{DomainTransferGetStatusResult};
484 }
485  
486 =head2 $domain->transferlist()
487  
488 Retrieve a list of transfers associated with the connected API account.
489 Automatically handles the Namecheap "paging" to get a full list. May
490 be optionally restricted:
491  
492 my $transfers = $domain->transferlist(
493 ListType => 'ALL', # or INPROGRESS, CANCELLED, COMPLETED
494 SearchTerm => 'foo', # keyword search
495 SortBy => 'DOMAINNAME', # or TRANSFERDATE, STATUSDATE, *_DESC
496 );
497  
498 Returns an arrayref of hashrefs:
499  
500 $domains = [
501 {
502 ID => '123',
503 DomainName => 'example.com',
504 User => 'apiuser',
505 TransferDate => 'MM/DD/YYYY',
506 OrderID => 12345,
507 StatusID => 20
508 Status => 'Cancelled',
509 StatusDate => 'MM/DD/YYYY',
510 StatusDescription => 'String',
511 }
512 ...
513 ];
514  
515 =cut
516  
517 sub transferlist {
518 0 ▶ my $self = shift;
Subroutine transferlist: not called CC=3 SCAR=24.8
Pod documented
519
520 0 my $params = _argparse(@_);
521
522 my %request = (
523 Command => 'namecheap.domains.transfer.getList',
524 ClientIp => $params->{'ClientIp'},
525 UserName => $params->{'UserName'},
526 PageSize => 100,
527 Page => 1,
528 ListType => $params->{'ListType'},
529 0 SearchTerm => $params->{'SearchTerm'},
530 );
531
532 0 my @transfers;
533
534 0 my $break = 0;
535 0 while (1) {
536 0 my $xml = $self->api->request(%request);
537
538 0 ▶ last unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
539
540 0 push(@transfers, @{$xml->{CommandResponse}->{TransferGetListResult}->{Transfer}});
541 0 ▶ if ($xml->{CommandResponse}->{Paging}->{TotalItems} <= ($request{Page} * $request{PageSize})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->{'CommandResponse'}{'Paging'}{'TotalItems'} <= $request{'Page'} * $request{'PageSize'}) { } 0 0 0
542 0 last;
543 } else {
544 0 $request{Page}++;
545 }
546 }
547
548 0 return \@transfers;
549 }
550  
551 =head2 $domain->api()
552  
553 Accessor for internal API object.
554  
555 =cut
556  
557 sub api {
558 1 ▶ return $_[0]->{api};
Subroutine api: called CC=1 SCAR=0.0
Pod documented
559 }
560  
561 sub _argparse {
562 2 ▶ my $hashref;
Subroutine _argparse: called CC=3 SCAR=14.2
563 2 ▶ if (@_ % 2 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 2 0 2
elsif (ref $_[0] eq 'HASH') { } 0 0 0
564 2 $hashref = { @_ }
565 } elsif (ref($_[0]) eq 'HASH') {
566 0 $hashref = \%{$_[0]};
567 }
568 2 return $hashref;
569 }
570  
571 =head1 AUTHOR
572  
573 Tim Wilde, C<< <twilde at cpan.org> >>
574  
575 =head1 BUGS
576  
577 Please report any bugs or feature requests to C<bug-www-namecheap-api at rt.cpan.org>, or through
578 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Namecheap-API>. I will be notified, and then you'll
579 automatically be notified of progress on your bug as I make changes.
580  
581  
582  
583 =head1 SUPPORT
584  
585 You can find documentation for this module with the perldoc command.
586  
587 perldoc WWW::Namecheap::Domain
588  
589  
590 You can also look for information at:
591  
592 =over 4
593  
594 =item * RT: CPAN's request tracker (report bugs here)
595  
596 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Namecheap-API>
597  
598 =item * AnnoCPAN: Annotated CPAN documentation
599  
600 L<http://annocpan.org/dist/WWW-Namecheap-API>
601  
602 =item * CPAN Ratings
603  
604 L<http://cpanratings.perl.org/d/WWW-Namecheap-API>
605  
606 =item * Search CPAN
607  
608 L<http://search.cpan.org/dist/WWW-Namecheap-API/>
609  
610 =back
611  
612  
613 =head1 ACKNOWLEDGEMENTS
614  
615  
616 =head1 LICENSE AND COPYRIGHT
617  
618 Copyright 2011 Tim Wilde.
619  
620 This program is free software; you can redistribute it and/or modify it
621 under the terms of either: the GNU General Public License as published
622 by the Free Software Foundation; or the Artistic License.
623  
624 See http://dev.perl.org/licenses/ for more information.
625  
626  
627 =cut
628  
629 12 1; # End of WWW::Namecheap::Domain
↑ Summary blib/lib/WWW/Namecheap/DNS.pm »