File Coverage

blib/lib/WWW/Alexa/TrafficRank.pm
Criterion Covered Total %
statement 25 41 60.9
branch 5 20 25.0
condition 1 2 50.0
subroutine 6 7 85.7
pod 3 3 100.0
total 40 73 54.7


line stmt bran cond sub pod time code
1             package WWW::Alexa::TrafficRank;
2            
3 2     2   47712 use strict;
  2         5  
  2         357  
4 2     2   14 use warnings;
  2         4  
  2         86  
5 2     2   18 use vars qw($VERSION);
  2         8  
  2         181  
6 2     2   14798 use LWP::UserAgent;
  2         217996  
  2         963  
7            
8             $VERSION = '1.9';
9            
10             sub new
11             {
12 1     1 1 22 my $class = shift;
13 1         4 my %par = @_;
14 1         2 my $self;
15            
16 1 50 50     18 $self->{ua} = LWP::UserAgent->new(agent => $par{agent} || 'Opera 10.0') or return;
17 1 50       5008 $self->{ua}->proxy('http', $par{proxy}) if $par{proxy};
18 1 50       5 $self->{ua}->timeout($par{timeout}) if $par{timeout};
19            
20 1         8 bless ($self, $class);
21             }
22            
23             sub get
24             {
25 1     1 1 7 my ($self, $domain) = @_;
26            
27 1 50       4 return unless defined $domain;
28            
29 1         12 my $res = $self->{ua}->get("http://xml.alexa.com/data?cli=10&dat=nsa&url=$domain");
30 1 50       475548 return $res->status_line if !$res->is_success;
31            
32 1         33 my ($rank) = $res->content =~ /
33            
34 1         56 return $rank;
35             }
36            
37             sub get_country_rank
38             {
39 0     0 1   my $self = shift;
40 0           my $par = { @_ };
41            
42 0 0         return unless defined $par->{Domain};
43 0 0         return unless defined $par->{Country};
44            
45 0           my $res = $self->{ua}->get("http://www.alexa.com/siteinfo/$par->{Domain}");
46 0 0         return $res->status_line if !$res->is_success;
47            
48 0           my $cont = $res->content;
49            
50 0 0         return 0 unless $cont =~ /
(.+?)
51            
52 0           my $listdata = $1;
53            
54 0           while ($listdata =~ /
(.+?)<\/div>/igs) {
55 0           my $item = $1; $item =~ s/,//g;
  0            
56            
57 0 0         if ($item =~ /$par->{Country}/gs) {
58 0           $item =~ /

(\d+)/i;

59            
60 0           return $1;
61             }
62             }
63            
64 0           return 0;
65             }
66            
67             1;
68            
69             __END__