line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
4
|
|
|
4
|
|
24
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
130
|
|
2
|
4
|
|
|
4
|
|
21
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
174
|
|
3
|
|
|
|
|
|
|
package Net::Disqus::UserAgent; |
4
|
|
|
|
|
|
|
BEGIN { |
5
|
4
|
|
|
4
|
|
79
|
$Net::Disqus::UserAgent::VERSION = '1.19'; |
6
|
|
|
|
|
|
|
} |
7
|
4
|
|
|
4
|
|
2096
|
use Net::Disqus::Exception; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
31
|
|
8
|
4
|
|
|
4
|
|
139
|
use Try::Tiny; |
|
4
|
|
|
|
|
17
|
|
|
4
|
|
|
|
|
4919
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
1
|
|
|
1
|
1
|
2
|
my $pkg = shift; |
12
|
1
|
|
|
|
|
5
|
my %args = ( |
13
|
|
|
|
|
|
|
pass_content_as_is => 0, |
14
|
|
|
|
|
|
|
forcelwp => 0, |
15
|
|
|
|
|
|
|
@_, |
16
|
|
|
|
|
|
|
ua_class => 'LWP::UserAgent', |
17
|
|
|
|
|
|
|
ua_key => 'lwp', |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
33
|
|
|
8
|
$args{agent} ||= "Net::Disqus/$Net::Disqus::VERSION"; |
21
|
1
|
50
|
|
|
|
4
|
if(!$args{'forcelwp'}) { |
22
|
1
|
|
|
1
|
|
81
|
eval 'use Mojo::UserAgent; use Mojo::JSON; use Mojo::URL'; |
|
1
|
|
|
|
|
434
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
23
|
1
|
50
|
|
|
|
6
|
unless($@) { |
24
|
0
|
|
|
|
|
0
|
$args{'ua_class'} = 'Mojo::UserAgent'; |
25
|
0
|
|
|
|
|
0
|
$args{'name'} = delete($args{'agent'}); |
26
|
0
|
|
|
|
|
0
|
$args{'ua_key'} = 'mojo'; |
27
|
|
|
|
|
|
|
} else { |
28
|
1
|
|
|
1
|
|
74
|
eval 'use LWP::UserAgent; use JSON::PP; use URI; use URI::Escape;'; |
|
1
|
|
|
1
|
|
1889
|
|
|
1
|
|
|
1
|
|
59389
|
|
|
1
|
|
|
1
|
|
36
|
|
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
86
|
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
29
|
1
|
50
|
|
|
|
6
|
die Net::Disqus::Exception->new({code => 500, text => 'Something really funny is going on, cannot find one of LWP::UserAgent, JSON::PP, URI, URI::Escape'}) if($@); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} else { |
32
|
0
|
|
|
|
|
0
|
eval 'use LWP::UserAgent; use JSON::PP; use URI; use URI::Escape;'; |
33
|
0
|
0
|
|
|
|
0
|
die Net::Disqus::Exception->new({code => 500, text => 'Something really funny is going on, cannot find one of LWP::UserAgent, JSON::PP, URI, URI::Escape'}) if($@); |
34
|
|
|
|
|
|
|
} |
35
|
1
|
|
|
|
|
9
|
my $self = bless({%args}, $pkg); |
36
|
1
|
|
|
|
|
7
|
delete($args{$_}) for(qw(pass_content_as_is forcelwp ua_class ua_key)); # and this is for LWP who doesn't like being passed unknown options |
37
|
1
|
|
|
|
|
10
|
$self->{'ua'} = $self->{'ua_class'}->new(%args); |
38
|
1
|
|
|
|
|
3765
|
return $self; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
0
|
0
|
0
|
sub ua { return shift->{ua} } |
42
|
0
|
|
|
0
|
0
|
0
|
sub ua_key { return shift->{ua_key} } |
43
|
0
|
|
|
0
|
0
|
0
|
sub ua_class { return shift->{ua_class} } |
44
|
0
|
|
|
0
|
0
|
0
|
sub pass_content_as_is { return shift->{pass_content_as_is} } |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub request { |
47
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
48
|
0
|
|
|
|
|
0
|
my $method = shift; |
49
|
0
|
|
|
|
|
0
|
my $f = "tx_" . $self->ua_key; |
50
|
0
|
|
|
|
|
0
|
return $self->$f($method, @_); |
51
|
|
|
|
|
|
|
}; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub tx_mojo { |
54
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
55
|
0
|
|
|
|
|
0
|
my $method = shift; |
56
|
0
|
|
|
|
|
0
|
my $url = shift; |
57
|
0
|
|
|
|
|
0
|
my %args = (@_); |
58
|
0
|
|
|
|
|
0
|
my $rate = {}; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
0
|
my $uri = Mojo::URL->new( |
61
|
|
|
|
|
|
|
($method eq 'get') |
62
|
0
|
0
|
|
|
|
0
|
? sprintf('%s?%s', $url, join('&', map { sprintf('%s=%s', $_, $args{$_}) } (keys(%args)))) |
63
|
|
|
|
|
|
|
: $url |
64
|
|
|
|
|
|
|
); |
65
|
0
|
0
|
|
|
|
0
|
my $f = ($method eq 'get') |
66
|
|
|
|
|
|
|
? 'get' |
67
|
|
|
|
|
|
|
: 'post_form'; |
68
|
0
|
|
|
|
|
0
|
my @fa = ($uri); |
69
|
0
|
0
|
|
|
|
0
|
push(@fa, { %args }) if($method eq 'post'); |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
0
|
my $res = $self->ua->$f(@fa)->res; |
72
|
0
|
0
|
0
|
|
|
0
|
die Net::Disqus::Exception->new({ code => 500, text => 'Did not receive a JSON response'}) if( |
|
|
|
0
|
|
|
|
|
73
|
|
|
|
|
|
|
($res->headers->content_type && $res->headers->content_type ne 'application/json') && |
74
|
|
|
|
|
|
|
!$self->pass_content_as_is |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
0
|
|
|
0
|
$rate->{$_} = $res->headers->to_hash->{$_} || 0 for(qw(X-Ratelimit-Remaining X-Ratelimit-Limit X-Ratelimit-Reset)); |
78
|
0
|
0
|
|
|
|
0
|
my @ret = ( |
79
|
|
|
|
|
|
|
($self->pass_content_as_is) ? $res->body : $res->json, |
80
|
|
|
|
|
|
|
$rate |
81
|
|
|
|
|
|
|
); |
82
|
0
|
|
|
|
|
0
|
return @ret; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub json_decode { |
86
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
87
|
0
|
|
|
|
|
0
|
my $str = shift; |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
0
|
return ($self->ua_key eq 'mojo') |
90
|
|
|
|
|
|
|
? Mojo::JSON->decode($str) |
91
|
|
|
|
|
|
|
: JSON::PP::decode_json($str); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub tx_lwp { |
95
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
96
|
0
|
|
|
|
|
0
|
my $method = shift; |
97
|
0
|
|
|
|
|
0
|
my $url = shift; |
98
|
0
|
|
|
|
|
0
|
my %args = (@_); |
99
|
0
|
|
|
|
|
0
|
my $rate = {}; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
0
|
my $uri = URI->new($url); |
102
|
0
|
|
|
|
|
0
|
my $query_args = join('&', map { sprintf('%s=%s', $_, uri_escape($args{$_})) } (keys(%args))); |
|
0
|
|
|
|
|
0
|
|
103
|
0
|
0
|
|
|
|
0
|
$uri->query($query_args) if($method eq 'get'); |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
0
|
my $request = HTTP::Request->new(uc($method), $uri); |
106
|
0
|
0
|
|
|
|
0
|
$request->content($query_args) if($method eq 'post'); |
107
|
0
|
|
|
|
|
0
|
my $res = $self->ua->request($request); |
108
|
0
|
0
|
0
|
|
|
0
|
die Net::Disqus::Exception->new({ code => 500, text => 'Did not receive a JSON response'}) if($res->header('Content-Type') ne 'application/json' && !$self->pass_content_as_is); |
109
|
0
|
|
|
|
|
0
|
my $json; |
110
|
0
|
0
|
|
|
|
0
|
if($self->pass_content_as_is) { |
111
|
0
|
|
|
|
|
0
|
$json = $res->content; |
112
|
|
|
|
|
|
|
} else { |
113
|
|
|
|
|
|
|
try { |
114
|
0
|
|
|
0
|
|
0
|
$json = JSON::PP::decode_json($res->content); |
115
|
|
|
|
|
|
|
} catch { |
116
|
0
|
|
|
0
|
|
0
|
die Net::Disqus::Exception->new({ code => 500, text => "Failed JSON decoding: $_"}); |
117
|
0
|
|
|
|
|
0
|
}; |
118
|
|
|
|
|
|
|
} |
119
|
0
|
|
0
|
|
|
0
|
$rate->{$_} = $res->header($_) || 0 for(qw(X-Ratelimit-Remaining X-Ratelimit-Limit X-Ratelimit-Reset)); |
120
|
0
|
|
|
|
|
0
|
return ($json, $rate); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
__END__ |