File Coverage

blib/lib/WWW/DomainTools.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package WWW::DomainTools;
2 4     4   32 use strict;
  4         6  
  4         178  
3 4     4   31 use warnings;
  4         8  
  4         202  
4 4     4   28 use Carp;
  4         7  
  4         379  
5 4     4   4659 use LWP::UserAgent;
  4         220395  
  4         117  
6 4     4   2905 use XML::Simple ();
  0            
  0            
7             use URI::Escape;
8             use WWW::DomainTools::SearchEngine;
9             use WWW::DomainTools::NameSpinner;
10              
11             BEGIN {
12             use Exporter ();
13             use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
14             $VERSION = "0.11";
15             @ISA = qw (Exporter);
16             @EXPORT = qw ();
17             @EXPORT_OK = qw (search_engine name_spinner);
18             %EXPORT_TAGS = ();
19             }
20              
21             use constant DEFAULT_URL => "http://engine.whoisapi.com/api.xml";
22             use constant DEFAULT_FORMAT => "hash";
23              
24             our @VALID_TLDS = qw/ com net org info biz us /;
25              
26             =head1 NAME
27              
28             WWW::DomainTools - DomainTools.com XML API interface
29              
30             =head1 SYNOPSIS
31              
32             use WWW::DomainTools qw(search_engine name_spinner);
33              
34             my $repsonse = search_engine(
35             q => 'example.com',
36             ext => 'COM|NET|ORG|INFO'
37             key => '12345',
38             partner => 'yourname',
39             customer_ip => '1.2.3.4'
40             );
41              
42             # OO
43             my $obj = WWW::DomainTools::SearchEngine->new(
44             key => '12345',
45             partner => 'yourname',
46             customer_ip => '1.2.3.4'
47             );
48             my $response = $obj->request(
49             q => 'example.com',
50             ext => 'COM|NET|ORG|INFO'
51             );
52              
53              
54             # Custom LWP user agent
55             my $ua = LWP::UserAgent->new;
56             $ua->env_proxy(true);
57              
58             my $obj = WWW::DomainTools::SearchEngine->new(
59             key => '12345',
60             partner => 'yourname',
61             customer_ip => '1.2.3.4',
62             lwp_ua => $ua
63             );
64             my $response = $obj->request(
65             q => 'example.com',
66             ext => 'COM|NET|ORG|INFO'
67             );
68              
69            
70              
71             =head1 DESCRIPTION
72              
73             This module allows you to use the name spinner and whois search available
74             on domaintools.com.
75              
76             These methods are available as both class and object methods. Nothing is
77             exported by default.
78              
79             =head1 EXPORTS
80              
81             None by default.
82              
83             Allowed:
84              
85             - search_engine
86             - name_spinner
87              
88             =head1 METHODS
89              
90             =cut
91              
92             sub new {
93             my ( $class, %parameters ) = @_;
94              
95             croak "new() can't be invoked on an object"
96             if ref($class);
97              
98             my $self = bless( {}, $class );
99              
100             $self->{_ua} = $parameters{lwp_ua} || LWP::UserAgent->new;
101             $self->{_xml} = XML::Simple->new;
102             $self->{_format} = $parameters{format} || DEFAULT_FORMAT;
103             $self->{_ua_timeout} = $parameters{timeout} || 10;
104             $self->{_url} = $parameters{url} || DEFAULT_URL;
105              
106             %{ $self->{default_params} } = ();
107              
108             # sub clases should implement this. Usually to set default_params fields
109             $self->_init();
110              
111             ## the universally recognized parameters can be passed in without using the
112             # 'params' hash as a convenience
113             foreach my $field (qw{ appname version partner key customer_ip }) {
114             if ( defined $parameters{$field} ) {
115             ${ $self->{default_params} }{$field} = $parameters{$field};
116             }
117             }
118              
119             return $self;
120              
121             }
122              
123             =over 4
124              
125             =item search_engine ( url parameters hash )
126              
127             The keys and values expected are documented on the Domain Tools website. In
128             addition the "search engine" specific parameters, you need to pass the
129             required parameters as documented in the L new()
130             method.
131              
132             If the request is successful, the return value is either a hash reference or
133             a string depending on the value of the 'format' parameter to the constructor.
134              
135             See the documentation for the new() method for more detailed information
136             about 'format' and other standard parameters.
137              
138             If the HTTP request fails, this method will die.
139              
140             =back
141              
142             =cut
143              
144             sub search_engine {
145             my $api = WWW::DomainTools::SearchEngine->new(@_);
146             return $api->request();
147             }
148              
149             =over 4
150              
151             =item name_spinner ( url parameters hash )
152              
153             The keys and values expected are documented on the Domain Tools website. In
154             addition the "name spinner" specific parameters, you need to pass the
155             required parameters as documented in the L new()
156             method.
157              
158             If the request is successful, the return value is either a hash reference or
159             a string depending on the value of the 'format' parameter to the constructor.
160              
161             See the documentation for the new() method for more detailed information
162             about 'format' and other standard parameters.
163              
164             If the HTTP request fails, this method will die.
165              
166             =back
167              
168             =cut
169              
170             sub name_spinner {
171             my $api = WWW::DomainTools::NameSpinner->new(@_);
172             return $api->request();
173             }
174              
175             sub request {
176             my ( $self, %params ) = @_;
177              
178             if ( $self->{_ua_timeout} > 0 ) {
179             $self->{_ua}->timeout( $self->{_ua_timeout} );
180             }
181              
182             my %parameters = ( %{ $self->{default_params} }, %params );
183              
184             my $req = HTTP::Request->new(
185             GET => sprintf( "%s?%s",
186             $self->{_url}, $self->_generate_query_string( \%parameters ) )
187             );
188             my $res = $self->{_ua}->request($req);
189             if ( $res->is_success ) {
190             if ( $self->{_format} eq 'hash' ) {
191             return $self->_xml_string_to_hash( \$res->content );
192             }
193             elsif ( $self->{_format} eq 'xml' ) {
194             return $res->content;
195             }
196             }
197             else {
198             die $res->status_line;
199             }
200              
201             return;
202              
203             }
204              
205             sub _generate_query_string {
206             my ( $self, $parameters ) = @_;
207              
208             return join "&",
209             map { sprintf( "%s=%s", $_, uri_escape( $parameters->{$_} ) ) }
210             keys %$parameters;
211              
212             }
213              
214             sub _init {
215             my ($self) = @_;
216              
217             #this should be implemented in the subclasses
218              
219             return;
220             }
221              
222             sub _xml_string_to_hash {
223             my ( $self, $xml_string_ref ) = @_;
224              
225             return $self->{_xml}->XMLin($$xml_string_ref);
226             }
227              
228             sub _tld_list_to_ext_param {
229             my ( $self, @tlds ) = @_;
230              
231             return join( '|', ( map { uc($_) } @tlds ) );
232             }
233              
234             sub _res_status_lookup {
235             my ( $self, $status_line, $extensions ) = @_;
236              
237             my @status_items = split //, $status_line;
238             my @extensions = map { lc($_) } split /\|/, $extensions;
239              
240             my %mapping;
241             @mapping{@extensions} = @status_items;
242              
243             return \%mapping;
244             }
245              
246             1;
247             __END__