File Coverage

blib/lib/WWW/Yahoo/InboundLinks.pm
Criterion Covered Total %
statement 47 62 75.8
branch 2 10 20.0
condition 2 6 33.3
subroutine 10 11 90.9
pod 3 5 60.0
total 64 94 68.0


line stmt bran cond sub pod time code
1             package WWW::Yahoo::InboundLinks;
2              
3 2     2   27069 use strict;
  2         4  
  2         75  
4 2     2   11 use warnings;
  2         4  
  2         69  
5              
6 2     2   10 use vars qw($VERSION);
  2         10  
  2         127  
7              
8 2     2   3804 use LWP::UserAgent ();
  2         118969  
  2         50  
9 2     2   27 use HTTP::Headers ();
  2         5  
  2         28  
10              
11 2     2   2473 use JSON ();
  2         33788  
  2         1199  
12              
13             $VERSION = '0.07';
14              
15             sub new {
16 1     1 1 9 my $class = shift;
17 1         2 my $appid = shift;
18            
19 1         3 my %options = @_;
20            
21 1         2 my $self = {};
22            
23             # config overrided by parameters
24 1         8 $self->{ua} = LWP::UserAgent->new;
25 1         3178 $self->{appid} = $appid;
26 1         3 $self->{format} = 'json';
27            
28 1         6 foreach (keys %options) {
29 0         0 $self->{$_} = $options{$_};
30             }
31            
32 1         8 bless($self, $class);
33             }
34              
35             sub user_agent {
36 1     1 1 421 shift->{ua};
37             }
38              
39             sub request_uri {
40 3     3 0 10 my ($self, $query, %params) = @_;
41            
42 3         26 my %opt_params = (
43             results => 2,
44             start => undef,
45             entire_site => undef,
46             omit_inlinks => undef,
47             callback => undef,
48             output => $self->{format}
49             );
50            
51 3   66     12 my %allowed_params = (map {$_ => $params{$_} || $opt_params{$_}} keys %opt_params);
  18         86  
52 3         12 $allowed_params{appid} = $self->{appid};
53 3         8 $allowed_params{query} = $query;
54            
55 12         44 my $params_string = join '&',
56 24         41 map {"$_=$allowed_params{$_}"}
57 3         11 grep {defined $allowed_params{$_}}
58             keys %allowed_params;
59            
60 3         10 my $url = 'http://search.yahooapis.com/SiteExplorerService/V1/inlinkData?'
61             . $params_string;
62            
63 3         16 return $url;
64            
65             }
66              
67             sub get {
68 3     3 1 1213 my ($self, $url, %params) = @_;
69              
70 3         7 my @result = ();
71            
72 3         14 my $query = $self->request_uri ($url, %params);
73            
74 3         212 my $resp = $self->{ua}->get ($query);
75 3         113915 $result[1] = $resp;
76            
77 3 50       14 if ($resp->is_success) {
78 0         0 my $parser = "$self->{format}_parser";
79 0         0 $self->$parser ($resp, \@result);
80             }
81            
82 3 50       38 if (wantarray) {
83 3         17 return @result;
84             } else {
85 0           return $result[0];
86             }
87             }
88              
89             sub json_parser {
90 0     0 0   my $self = shift;
91 0           my $resp = shift;
92 0           my $result = shift;
93            
94 0           my $content = $resp->content;
95            
96             # contents example:
97             #HTTP/1.1 999 Rate Limit Exceeded
98             #Date: Sun, 01 Mar 2009 11:26:23 GMT
99             #P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
100             #Connection: close
101             #Transfer-Encoding: chunked
102             #Content-Type: text/javascript; charset=utf-8
103 0 0         if ($content =~ /rate limit exceeded/si) {
104             # JSON cannot parse xml
105 0           return;
106             }
107            
108 0           my $struct = JSON::from_json ($content, {utf8 => 1});
109            
110 0 0 0       if (defined $struct and ref $struct eq 'HASH') {
111            
112 0           $result->[2] = $struct;
113            
114 0 0         if (exists $struct->{ResultSet}) {
115 0           $result->[0] = $struct->{ResultSet}->{totalResultsAvailable};
116             }
117             }
118             }
119              
120             1;
121              
122             __END__