line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Business::YQL; |
2
|
1
|
|
|
1
|
|
115676
|
use Moo; |
|
1
|
|
|
|
|
18014
|
|
|
1
|
|
|
|
|
6
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.0004'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3152
|
use HTTP::Request::Common qw(GET POST); |
|
1
|
|
|
|
|
29485
|
|
|
1
|
|
|
|
|
94
|
|
7
|
1
|
|
|
1
|
|
9
|
use URI; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
24
|
|
8
|
1
|
|
|
1
|
|
910
|
use URI::QueryParam; |
|
1
|
|
|
|
|
777
|
|
|
1
|
|
|
|
|
26
|
|
9
|
1
|
|
|
1
|
|
1120
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
26078
|
|
|
1
|
|
|
|
|
42
|
|
10
|
1
|
|
|
1
|
|
1151
|
use JSON qw(to_json from_json); |
|
1
|
|
|
|
|
14430
|
|
|
1
|
|
|
|
|
8
|
|
11
|
1
|
|
|
1
|
|
185
|
use Carp qw(croak); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
65
|
|
12
|
1
|
|
|
1
|
|
988
|
use Log::Any qw($log); |
|
1
|
|
|
|
|
7985
|
|
|
1
|
|
|
|
|
6
|
|
13
|
1
|
|
|
1
|
|
1036
|
use Try::Tiny; |
|
1
|
|
|
|
|
1528
|
|
|
1
|
|
|
|
|
795
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has scheme => (is => 'ro', default => 'http' ); |
16
|
|
|
|
|
|
|
has domain => (is => 'ro', default => 'query.yahooapis.com' ); |
17
|
|
|
|
|
|
|
has version => (is => 'ro', default => 'v1' ); |
18
|
|
|
|
|
|
|
has timeout => (is => 'ro', default => 10 ); |
19
|
|
|
|
|
|
|
has retries => (is => 'ro', default => 3 ); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has uri => ( |
22
|
|
|
|
|
|
|
is => 'ro', |
23
|
|
|
|
|
|
|
lazy => 1, |
24
|
|
|
|
|
|
|
default => sub { |
25
|
|
|
|
|
|
|
my $self = shift; |
26
|
|
|
|
|
|
|
return sprintf '%s://%s/%s/public/yql', |
27
|
|
|
|
|
|
|
$self->scheme, $self->domain, $self->version; |
28
|
|
|
|
|
|
|
}, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has ua => ( |
32
|
|
|
|
|
|
|
is => 'ro', |
33
|
|
|
|
|
|
|
lazy => 1, |
34
|
|
|
|
|
|
|
default => sub { |
35
|
|
|
|
|
|
|
my $self = shift; |
36
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
37
|
|
|
|
|
|
|
$ua->timeout($self->timeout); |
38
|
|
|
|
|
|
|
return $ua |
39
|
|
|
|
|
|
|
}, |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub q { |
43
|
4
|
|
|
4
|
1
|
18656
|
my ($self, $query) = @_; |
44
|
4
|
50
|
|
|
|
20
|
croak "Invalid query string" |
45
|
|
|
|
|
|
|
unless $query; |
46
|
|
|
|
|
|
|
|
47
|
4
|
|
|
|
|
28
|
my %params = ( |
48
|
|
|
|
|
|
|
q => $query, |
49
|
|
|
|
|
|
|
format => 'json', |
50
|
|
|
|
|
|
|
env => 'store://datatables.org/alltableswithkeys', |
51
|
|
|
|
|
|
|
jsonCompat => 'new', |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
4
|
|
|
|
|
144
|
my $uri = URI->new($self->uri); |
55
|
4
|
|
|
|
|
10968
|
$uri->query_form(%params); |
56
|
|
|
|
|
|
|
|
57
|
4
|
100
|
|
|
|
1031
|
if ($query =~ m/^insert/i) { |
58
|
1
|
|
|
|
|
7
|
return $self->_req(POST $uri); |
59
|
|
|
|
|
|
|
} else { |
60
|
3
|
|
|
|
|
18
|
return $self->_req(GET $uri); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _req { |
65
|
4
|
|
|
4
|
|
587
|
my ($self, $req) = @_; |
66
|
4
|
|
|
|
|
18
|
$self->_log_request($req); |
67
|
4
|
|
|
|
|
100
|
my $res = $self->ua->request($req); |
68
|
4
|
|
|
|
|
1830507
|
$self->_log_response($res); |
69
|
4
|
|
|
|
|
68
|
my $retries = $self->retries; |
70
|
4
|
|
33
|
|
|
18
|
while ($res->code =~ /^5/x and $retries--) { |
71
|
0
|
|
|
|
|
0
|
sleep 1; |
72
|
0
|
|
|
|
|
0
|
$res = $self->ua->request($req); |
73
|
|
|
|
|
|
|
} |
74
|
4
|
100
|
|
|
|
57
|
return from_json $res->content |
75
|
|
|
|
|
|
|
if $res->code =~ /^4/x; |
76
|
3
|
50
|
|
|
|
37
|
return $res->content ? from_json($res->content)->{query}{results} : 1; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _log_content { |
80
|
8
|
|
|
8
|
|
518
|
my ($self, $content) = @_; |
81
|
8
|
50
|
33
|
|
|
45
|
if ($content && length $content) { |
82
|
|
|
|
|
|
|
try { |
83
|
0
|
|
|
0
|
|
0
|
$content = to_json from_json $content; |
84
|
0
|
|
|
|
|
0
|
$log->trace($content); |
85
|
|
|
|
|
|
|
} catch { |
86
|
0
|
|
|
0
|
|
0
|
$log->error('Invalid JSON: ' . $content); |
87
|
0
|
|
|
|
|
0
|
}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _log_request { |
92
|
4
|
|
|
4
|
|
10
|
my ($self, $req) = @_; |
93
|
4
|
|
|
|
|
15
|
$log->trace($req->method . ' => ' . $req->uri); |
94
|
4
|
|
|
|
|
124
|
_log_content $req->content; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _log_response { |
98
|
4
|
|
|
4
|
|
10
|
my ($self, $res) = @_; |
99
|
4
|
|
|
|
|
92
|
$log->trace($res->status_line); |
100
|
4
|
|
|
|
|
84
|
_log_content $res->content; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# ABSTRACT: YQL Perl interface for the Y! Query API |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |