blib/lib/Net/Whois/RegistryFusion.pm

filter: Statementstmt 38.2% 44 / 115 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 45.1% 14 / 31 Podpod 87.5% 7 / 8 totaltotal 33.6% 65 / 193 CC 19 SCAR 21.6
CC
19
Cov
29%
new
30.0
_login
20.0
whois
12.0
CRAP 8.7
↑ Summary
1 package Net::Whois::RegistryFusion;
2  
3 =head1 NAME
4  
5 Net::Whois::RegistryFusion - perform cacheable whois lookups using RegistryFusion XML API
6  
7 =head1 SYNOPSIS
8  
9 $rf = Net::Whois::RegistryFusion->new();
10 # OR:
11 $rf = Net::Whois::RegistryFusion->new({ refreshCache=>1,
12 AUTH=>'http://hexillion.com/rf/xml/1.0/auth/',
13 WHOIS=>'http://hexillion.com/rf/xml/1.0/whois/'
14 });
15 $rf->isCached('domain.com')
16 &&
17 $xml = $rf->whois('domain.com');
18  
19 =head1 DESCRIPTION
20  
21 This class does not do any XML parsing. You must create a subclass inheriting and extending the whois method
22 where you can code the XML parsing using eg. XML::Simple.
23 You must also implement the _getUsername, _getPassword, _getXmlpath methods.
24  
25 The class does some basic on-disk caching of the raw xml retrieved from RegistryFusion.
26 The path to the cache is specified using _getXmlpath method.
27  
28 =head1 REQUIRED MODULES
29  
30 Date::Format (any)
31 Error (any)
32 File::Slurp (any)
33 File::stat (any)
34 IO::LockedFile (any)
35 LWP::Simple (any)
36 Module::Signature (any)
37 Set::Array (any)
38 Test::More (any)
39 Test::Signature (any)
40  
41 =head1 ABSTRACT METHODS
42  
43 =head2 _getUsername
44  
45 Must be implemented to return a RegistryFusion username
46  
47 =head2 _getPassword
48  
49 Must be implemented to return a RegistryFusion password
50  
51 =head2 _getXmlpath
52  
53 Must be implemented to return a path where XML cache files will be stored
54  
55 =head1 PUBLIC METHODS
56  
57 =head2 new
58  
59 This is the constructor. It takes as argument a hashref of options.
60  
61 One option is 'refreshCache'. Setting 'refreshCache' to a value of 1 will alter the behaviour of the whois method for the lifetime of the object; foregoing and deleting the cached domain (if any) and retrieving directly from RegistryFusion.
62  
63 You can also pass the auth url in the 'AUTH' key and the whois url in 'WHOIS' key, overriding the default values of:
64 AUTH => 'http://whois.RegistryFusion.com/rf/xml/1.0/auth/';
65 WHOIS => 'http://whois.RegistryFusion.com/rf/xml/1.0/whois/';
66  
67 =head2 whois ($domain)
68  
69 Returns (scalar) whois info in xml format for given $domain. Checks the cache first. If not found in the cache, retrieves from RegistryFusion. The whois xml info is cached in a file under the path as returned by _getXmlpath method. So, if the XMLPATH is '/registryfusion' and the $domain is 'example.com', the file will be stored as '/registryfusion/e/example.com.xml'
70  
71 =head2 getFetchedDomains
72  
73 Fetched domains are those domains that had the whois info fetched from RegistryFusion and not the cache.
74 Accessor returns array in list context or a Set::Array object in scalar context.
75  
76 =head2 isCached ($domain)
77  
78 Returns TRUE if given domain is cached. FALSE otherwise.
79  
80 =head2 deleteFromCache ($domain)
81  
82 Deletes the given $domain from the cache.
83  
84 =head2 logout
85  
86 Logs out of RegistryFusion, expiring the session. This method is called by the destructor, so you don't need to explicitly call it.
87  
88 =head2 getSessionKey
89  
90 returns session key.
91  
92 =head1 PRIVATE METHODS
93  
94 =head2 _login
95  
96 Login to RegistryFusion and return a session key. this method is called by the constructor.
97  
98 =head1 TODO
99  
100 Need generic test suite. eg. use Class::Generate to generate a subclass. Get the username, password, xmlpath values from user during make test.
101  
102 I've started doing this but encountered a problem with the fact that the username, password and xmlpath variables are static class variables and Class::Generate doesn't take care of this. Maybe we can improve Class::Generate.
103  
104 My current progress can be seen embedded in the pod (Test::Inline style).
105  
106  
107 =begin testing
108  
109 use Class::Generate;
110  
111 subclass Net::Whois::RegistryFusion::Test => [
112 '&_getUsername' => q{ return $username },
113 '&_getPassword' => q{ return $password },
114 '&_getXmlpath' => q{ return $xmlpath },
115  
116 ], -parent => 'Net::Whois::RegistryFusion';
117  
118 my $rf = new Net::Whois::RegistryFusion::Test;
119 can_ok('Net::Whois::RegistryFusion::Test', ('isCached', 'whois'));
120 ok(defined $rf->isCached('lobsanov.com'), "verify isCached");
121 ok(my @info = $rf->whois('lobsanov.com'), "get whois");
122 is($info[0], 'LOBSANOV.COM', 'verify whois content');
123  
124  
125 =end testing
126  
127 =cut
128  
129 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 1 ▶use File::Slurp (); # don't import File::Slurp symbols to avoid collisions and reduce overhead
Subroutine BEGIN: called CC=1 SCAR=0.0
133 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
134 1 ▶use IO::LockedFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 1 ▶use Date::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 1 ▶use Set::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 1 ▶use Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 1 ▶use constant AUTH => 'http://whois.RegistryFusion.com/rf/xml/1.0/auth/';
Subroutine BEGIN: called CC=1 SCAR=0.0
140 1 ▶use constant WHOIS => 'http://whois.RegistryFusion.com/rf/xml/1.0/whois/';
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 1 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 1 ▶use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
146 1 $VERSION = '0.05';
147  
148  
149 1 ▶use fields qw(sessionKey fetchedDomains refreshCache AUTH WHOIS);
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
152 0 my ($self, $opts) = @_;
153  
154 0 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
155  
156 0 $self->{'fetchedDomains'} = new Set::Array;
157 0 ▶ $self->{'refreshCache'} = $opts->{'refreshCache'} || FALSE;
Condition0%0/2
$opts->{'refreshCache'} || 0
l!l
00
MC/DC0%0/1
$opts->{'refreshCache'} || 0
$opts->{'refreshCache'}
Truth table0%0/2
A
A= $opts->{'refreshCache'}
Aresult
11
00
158 0 ▶ $self->{'AUTH'} = $opts->{'AUTH'} || AUTH;
Condition0%0/2
$opts->{'AUTH'} || 'http://whois.RegistryFusion.com/rf/xml/1.0/auth/'
l!l
00
MC/DC0%0/1
$opts->{'AUTH'} || 'http://whois.RegistryFusion.com/rf/xml/1.0/auth/'
$opts->{'AUTH'}
Truth table0%0/2
A
A= $opts->{'AUTH'}
Aresult
11
00
159 0 ▶ $self->{'WHOIS'} = $opts->{'WHOIS'} || WHOIS;
Condition0%0/2
$opts->{'WHOIS'} || 'http://whois.RegistryFusion.com/rf/xml/1.0/whois/'
l!l
00
MC/DC0%0/1
$opts->{'WHOIS'} || 'http://whois.RegistryFusion.com/rf/xml/1.0/whois/'
$opts->{'WHOIS'}
Truth table0%0/2
A
A= $opts->{'WHOIS'}
Aresult
11
00
160 0 $self->{'sessionKey'} = $self->_login();
161 0 return $self;
162 }
163  
164 0 ▶sub _login {
Subroutine _login: not called CC=4 SCAR=30.0
165 0 my ($self) = @_;
166  
167 0 ▶ if ( defined $self->{'sessionKey'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"sessionKey"}) 0 0 0
168 # already logged in
169 0 return;
170 }
171 0 my $url = $self->{AUTH} . "?username=" . $self->_getUsername() . "&password=" . $self->_getPassword();
172 0 ▶ my $xml = get($url)
Branch: 1 branch
BranchTrueFalseTotal
my $xml = &get($url) or "Error::Simple"->throw("failed to get $url") 0 0 0
173 or throw Error::Simple("failed to get $url");
174 0 ▶ $xml =~ m#<SessionKey>(.*)</SessionKey>#
Branch: 1 branch
BranchTrueFalseTotal
$xml =~ m[<SessionKey>(.*)</SessionKey>] or "Error::Simple"->throw("Couldn't open session") 0 0 0
175 or throw Error::Simple("Couldn't open session");
176 0 return $1;
177 }
178  
179 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
180 0 my ($self) = @_;
181  
182 0 my $url = $self->{AUTH} . "?sessionkey=" . $self->getSessionKey();
183 0 get($url);
184 }
185  
186 0 ▶sub _getUsername {
Subroutine _getUsername: not called CC=1 SCAR=6.9
187 0 throw Error::Simple("this is an abstract method and must be implemented by subclass");
188 }
189  
190 0 ▶sub _getPassword {
Subroutine _getPassword: not called CC=1 SCAR=6.9
191 0 throw Error::Simple("this is an abstract method and must be implemented by subclass");
192 }
193  
194 0 ▶sub _getXmlpath {
Subroutine _getXmlpath: not called CC=1 SCAR=6.9
195 0 throw Error::Simple("this is an abstract method and must be implemented by subclass");
196 }
197  
198 0 ▶sub getSessionKey {
Subroutine getSessionKey: not called CC=1 SCAR=6.9
Pod documented
199 0 my ($self) = @_;
200 0 return $self->{'sessionKey'};
201 }
202  
203 0 ▶sub whois {
Subroutine whois: not called CC=3 SCAR=24.8
Pod documented
204 0 my ($self, $domain) = @_;
205  
206 0 my $xml;
207 0 ▶ if ( $self->isCached($domain) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isCached($domain)) { } 0 0 0
208 0 ▶ if ( $self->{refreshCache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'refreshCache'}) { } 0 0 0
209 0 $self->deleteFromCache($domain);
210 0 $xml = $self->_whois($domain);
211 }
212 else {
213 0 $xml = $self->_getCached($domain);
214 }
215 }
216 else {
217 0 $xml = $self->_whois($domain);
218 }
219 0 return $xml;
220 }
221  
222 0 ▶sub _whois {
Subroutine _whois: not called CC=2 SCAR=17.9
223 # here we do the actual whois lookup
224 0 my ($self, $domain) = @_;
225  
226 0 my $url = $self->{WHOIS} . "?sessionkey=". $self->getSessionKey() . "&query=$domain";
227 0 ▶ my $xml = get($url)
Branch: 1 branch
BranchTrueFalseTotal
my $xml = &get($url) or "Error::Simple"->throw("get $url failed") 0 0 0
228 or throw Error::Simple("get $url failed");
229  
230 # record the domain as fetched for reporting purposes
231 0 $self->{fetchedDomains}->push($domain);
232 # cache the xml
233 0 $self->_cache($xml, $domain);
234  
235 0 return $xml;
236 }
237  
238 0 ▶sub getFetchedDomains {
Subroutine getFetchedDomains: not called CC=2 SCAR=17.9
Pod documented
239 0 my ($self) = @_;
240  
241 0 ▶ return wantarray ? @{ $self->{fetchedDomains} } : $self->{fetchedDomains};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
242 }
243  
244 0 ▶sub _getFilename {
Subroutine _getFilename: not called CC=3 SCAR=24.8
245 0 my ($self, $domain) = @_;
246  
247 0 ▶ unless ( $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($domain) 0 0 0
248 0 throw Error::Simple("_getFilename called without domain argument");
249 }
250  
251 0 my $subdir = lc(substr($domain, 0, 1));
252 0 my $filename = $self->_getXmlpath() . "/$subdir/$domain.xml";
253  
254 0 ▶ return wantarray ? ($filename, $subdir) : $filename;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
255 }
256  
257 0 ▶sub getCacheDate {
Subroutine getCacheDate: not called CC=1 SCAR=6.9
Pod undocumented
258 0 my ($self, $domain) = @_;
259  
260 0 my $stat = stat($self->_getFilename($domain));
261 0 return time2str("%x", $stat->mtime());
262 }
263  
264 0 ▶sub isCached {
Subroutine isCached: not called CC=2 SCAR=17.9
Pod documented
265 0 my ($self, $domain) = @_;
266
267 0 ▶ if ( -e $self->_getFilename($domain) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->_getFilename($domain)) { } 0 0 0
268 0 return TRUE;
269 }
270 else {
271 0 return FALSE;
272 }
273 }
274  
275 0 ▶sub _getCached {
Subroutine _getCached: not called CC=2 SCAR=17.9
276 0 my ($self, $domain) = @_;
277  
278 0 my $filename = $self->_getFilename($domain);
279 0 my $file = new IO::LockedFile $filename;
280 0 ▶ if ( my $xml = File::Slurp::read_file($file) ) { #YYY: Perl6::Slurp is nicer but requires perl5.8+
Branch: 1 branch
BranchTrueFalseTotal
if (my $xml = File::Slurp::read_file($file)) 0 0 0
281 0 return $xml;
282 }
283 }
284  
285 0 ▶sub _cache {
Subroutine _cache: not called CC=2 SCAR=17.9
286 0 my ($self, $xml, $domain) = @_;
287
288 0 my ($filename, $subdir) = $self->_getFilename($domain);
289 0 $subdir = $self->_getXmlpath() . "/$subdir";
290 0 ▶ mkdir $subdir if ! -d $subdir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $subdir 0 0 0
291 0 my $file = new IO::LockedFile ">$filename";
292 0 File::Slurp::write_file($file, $xml);
293 }
294  
295 0 ▶sub deleteFromCache {
Subroutine deleteFromCache: not called CC=3 SCAR=24.8
Pod documented
296 0 my ($self, $domain) = @_;
297  
298 0 my $filename = $self->_getFilename($domain);
299 0 ▶ if ( -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) 0 0 0
300 0 ▶ unlink $filename
Branch: 1 branch
BranchTrueFalseTotal
unlink $filename or "Error::Simple"->throw("Failed to unlink $filename. $!") 0 0 0
301 or throw Error::Simple("Failed to unlink $filename. $!");
302 }
303 }
304  
305 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
306 0 my ($self) = @_;
307 0 $self->logout();
308 }
309  
310 1 1;
311  
312 __END__
↑ Summary