| 1 | package WWW::MelissaData::PhoneLocation; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use 5.008000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | 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 | 0 | ▶ | { | ||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 38 | 0 | my $self = bless({}, shift); | |||||||||||
| 39 | 0 | my %args = @_; | |||||||||||
| 40 | 0 | ▶ | $self->{url} = $args{url} || 'http://www.melissadata.com/lookups/phonelocation.asp?number='; | ||||||||||
|
Condition0%0/2
$args{'url'} || 'http://www.melissadata.com/lookups/phonelocation.asp?number='
MC/DC0%0/1
$args{'url'} || 'http://www.melissadata.com/lookups/phonelocation.asp?number='
$args{'url'}
Truth table0%0/2
A
A= $args{'url'}
| |||||||||||||
| 41 | 0 | ▶ | $self->{uastring} = $args{uastring} || 'WWW::MelissaData::PhoneLocation/'.$VERSION; | ||||||||||
|
Condition0%0/2
$args{'uastring'} || 'WWW::MelissaData::PhoneLocation/' . $WWW::MelissaData::PhoneLocation::VERSION
MC/DC0%0/1
$args{'uastring'} || 'WWW::MelissaData::PhoneLocation/' . $WWW::MelissaData::PhoneLocation::VERSION
$args{'uastring'}
Truth table0%0/2
A
A= $args{'uastring'}
| |||||||||||||
| 42 | 0 | $self->{ua} = $args{ua}; # pass an existing LWP::UserAgent object | |||||||||||
| 43 | |||||||||||||
| 44 | 0 | ▶ | if (!$self->{ua}) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 0 | ▶ | { | ||||||||||
|
Subroutine
query: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||
| 65 | 0 | 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 | ▶ | if ($res->is_success) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | { | ||||||||||||
| 72 | 0 | my $content = $res->content; | |||||||||||
| 73 | 0 | $content =~ s#[\r\n]# #gm; | |||||||||||
| 74 | 0 | $content =~ m#<center><table\s.*?>(.*?)</table>#i; | |||||||||||
| 75 | 0 | my $data = $1; | |||||||||||
| 76 | 0 | $data =~ s#</?(a|img|br|b).*?># #gi; | |||||||||||
| 77 | 0 | $data =~ s#</t[rd].*?>##gi; | |||||||||||
| 78 | 0 | $data =~ s#\s+# #g; | |||||||||||
| 79 | 0 | my @data = split(/<tr.*?>/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(/<td.*?>/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 | ▶ | $info->{fipscode} = $1 if ($info->{'countyname(fipscode)'}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 109 | 0 | delete($info->{'countyname(fipscode)'}); | |||||||||||
| 110 | 0 | ($info->{metroarea} = $info->{'metroarea(code)'}) =~ s#\s*\(\s*(.*?)\s*\)\s*##g; | |||||||||||
| 111 | 0 | ▶ | $info->{metrocode} = $1 if ($info->{'metroarea(code)'}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 0 | delete($info->{'metroarea(code)'}); | |||||||||||
| 113 | 0 | ($info->{timezone} = $info->{'timezone(localtime)'}) =~ s#\s*\(\s*(.*?)\s*\)\s*##g; | |||||||||||
| 114 | 0 | ▶ | $info->{localtime} = $1 if ($info->{'timezone(localtime)'}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 0 | delete($info->{'timezone(localtime)'}); | |||||||||||
| 116 | |||||||||||||
| 117 | 0 | return $info; | |||||||||||
| 118 | } | ||||||||||||
| 119 | else | ||||||||||||
| 120 | { | ||||||||||||
| 121 | 0 | return undef; | |||||||||||
| 122 | } | ||||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | 1 | 1; | |||||||||||
| 126 | |||||||||||||
| 127 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 128 | |||||||||||||
| 129 | Copyright (C) 2009 Dusty Wilson, E<lt>dusty@megagram.comE<gt> | ||||||||||||
| 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 | ||||||||||||