File Coverage

blib/lib/WWW/MelissaData/PhoneLocation.pm
Criterion Covered Total %
statement 12 70 17.1
branch 0 10 0.0
condition 0 5 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 18 93 19.3


line stmt bran cond sub pod time code
1             package WWW::MelissaData::PhoneLocation;
2              
3 1     1   28631 use 5.008000;
  1         3  
  1         29  
4 1     1   7 use strict;
  1         2  
  1         32  
5 1     1   4 use warnings;
  1         6  
  1         34  
6              
7 1     1   993 use LWP::UserAgent;
  1         54557  
  1         848  
8              
9             our $VERSION = '0.01';
10              
11             =head1 NAME
12              
13             WWW::MelissaData::PhoneLocation - Provides an interface to MelissaData's free
14             phone location lookup service
15              
16             =head1 SYNOPSIS
17              
18             use WWW::MelissaData::PhoneLocation;
19             my $loc = WWW::MelissaData::PhoneLocation->new;
20             my $phone = $loc->query('4079347639'); # 407-W-DISNEY (Disney Reservations)
21             use Data::Dumper;
22             print Dumper($phone);
23              
24             =head1 DESCRIPTION
25              
26             Put in a phone number and it will give you the city, state, telco, and other
27             assorted data for that number. The data comes from MelissaData.com. This only
28             provides data for NANPA phone numbers (US/Canada).
29              
30             =head2 new
31              
32             Creates WWW::MelissaData::PhoneLocation object.
33              
34             =cut
35              
36             sub new
37             {
38 0     0 1   my $self = bless({}, shift);
39 0           my %args = @_;
40 0   0       $self->{url} = $args{url} || 'http://www.melissadata.com/lookups/phonelocation.asp?number=';
41 0   0       $self->{uastring} = $args{uastring} || 'WWW::MelissaData::PhoneLocation/'.$VERSION;
42 0           $self->{ua} = $args{ua}; # pass an existing LWP::UserAgent object
43              
44 0 0         if (!$self->{ua})
45             {
46 0           $self->{ua} = LWP::UserAgent->new;
47 0           $self->{ua}->agent($self->{uastring});
48             }
49              
50 0           return $self;
51             }
52              
53             =head2 query
54              
55             Queries the site. Provide a phone number as the only argument.
56              
57             Do not include a country code. Do not provide a "1" prefix. Use only the
58             ten-digit phone number. It is okay to include hyphens/dashes/etc; non-digit
59             characters will be removed automatically.
60              
61             =cut
62              
63             sub query
64             {
65 0     0 1   my $self = shift;
66 0           my $number = shift;
67 0           $number =~ s/\D+//g; # kill any non-digit chars
68 0           my $req = HTTP::Request->new(GET => $self->{url}.$number);
69 0           my $res = $self->{ua}->request($req);
70 0 0         if ($res->is_success)
71             {
72 0           my $content = $res->content;
73 0           $content =~ s#[\r\n]# #gm;
74 0           $content =~ m#
(.*?)
#i; 75 0           my $data = $1; 76 0           $data =~ s## #gi; 77 0           $data =~ s###gi; 78 0           $data =~ s#\s+# #g; 79 0           my @data = split(//i, $data); 80 0           shift(@data); # header 81 0           shift(@data); # header 82 0           my $info = {}; 83 0           foreach my $row (@data) 84             { 85 0           my @parts = split(//i, $row); 86 0           shift(@parts); # empty 87 0           my $var = lc(shift(@parts)); 88 0           $var =~ s/\s+//g; 89 0           my $val = shift(@parts); 90 0           $val =~ s/^\s+//; 91 0           $val =~ s/\s+$//; 92 0           $val =~ s/\s+/ /g; 93 0           $info->{$var} = $val; 94             } 95               96 0           delete($info->{'name&address'}); 97 0           $info->{zip1} = $info->{'primaryzipcode'}; 98 0           delete($info->{'primaryzipcode'}); 99 0           $info->{zip2} = $info->{'secondaryzipcode'}; 100 0           delete($info->{'secondaryzipcode'}); 101 0           $info->{zip3} = $info->{'otherzipcode'}; 102 0           delete($info->{'otherzipcode'}); 103 0           $info->{type} = $info->{'typeofservice'}; 104 0           delete($info->{'typeofservice'}); 105 0           $info->{businessesinprefix} = $info->{'#ofbusinessesinprefix'}; 106 0           delete($info->{'#ofbusinessesinprefix'}); 107 0           ($info->{countyname} = $info->{'countyname(fipscode)'}) =~ s#\s*\(\s*(.*?)\s*\)\s*##g; 108 0 0         $info->{fipscode} = $1 if ($info->{'countyname(fipscode)'}); 109 0           delete($info->{'countyname(fipscode)'}); 110 0           ($info->{metroarea} = $info->{'metroarea(code)'}) =~ s#\s*\(\s*(.*?)\s*\)\s*##g; 111 0 0         $info->{metrocode} = $1 if ($info->{'metroarea(code)'}); 112 0           delete($info->{'metroarea(code)'}); 113 0           ($info->{timezone} = $info->{'timezone(localtime)'}) =~ s#\s*\(\s*(.*?)\s*\)\s*##g; 114 0 0         $info->{localtime} = $1 if ($info->{'timezone(localtime)'}); 115 0           delete($info->{'timezone(localtime)'}); 116               117 0           return $info; 118             } 119             else 120             { 121 0           return undef; 122             } 123             } 124               125             1; 126               127             =head1 COPYRIGHT AND LICENSE 128               129             Copyright (C) 2009 Dusty Wilson, Edusty@megagram.comE 130               131             This library is free software; you can redistribute it and/or modify 132             it under the same terms as Perl itself, either Perl version 5.8.0 or, 133             at your option, any later version of Perl 5 you may have available. 134               135             =cut