line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::TDWTF; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
194580
|
use 5.014000; |
|
2
|
|
|
|
|
8
|
|
4
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
44
|
|
5
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
62
|
|
6
|
2
|
|
|
2
|
|
2587
|
use parent qw/Exporter/; |
|
2
|
|
|
|
|
603
|
|
|
2
|
|
|
|
|
12
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
98
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
135
|
|
9
|
2
|
|
|
2
|
|
12557
|
use HTTP::Tiny; |
|
2
|
|
|
|
|
103393
|
|
|
2
|
|
|
|
|
86
|
|
10
|
2
|
|
|
2
|
|
1589
|
use JSON::MaybeXS qw/decode_json/; |
|
2
|
|
|
|
|
27085
|
|
|
2
|
|
|
|
|
131
|
|
11
|
2
|
|
|
2
|
|
14
|
use Scalar::Util qw/looks_like_number/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
172
|
|
12
|
2
|
|
|
2
|
|
1143
|
use WebService::TDWTF::Article; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
12
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my @subs = qw/article list_recent list_series list_author/; |
15
|
|
|
|
|
|
|
our @EXPORT = map { "tdwtf_$_" } @subs; |
16
|
|
|
|
|
|
|
our @EXPORT_OK = (@EXPORT, @subs); |
17
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
18
|
|
|
|
|
|
|
our $VERSION = '0.002'; |
19
|
|
|
|
|
|
|
our $AGENT = "WebService-TDWTF/$VERSION"; |
20
|
|
|
|
|
|
|
our $BASE_URL = 'http://thedailywtf.com/api'; |
21
|
|
|
|
|
|
|
|
22
|
12
|
|
|
12
|
|
119
|
sub _ht { HTTP::Tiny->new(agent => $AGENT) } |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _query { |
25
|
12
|
|
|
12
|
|
26
|
my ($url) = @_; |
26
|
|
|
|
|
|
|
|
27
|
12
|
|
|
|
|
39
|
my $ht = _ht; |
28
|
12
|
|
|
|
|
1545
|
my $response = $ht->get($url); |
29
|
12
|
50
|
|
|
|
11922313
|
croak $response->{reason} unless $response->{success}; |
30
|
12
|
|
|
|
|
3010
|
$response = decode_json $response->{content}; |
31
|
12
|
50
|
66
|
|
|
239
|
croak $response->{Status} if ref $response eq 'HASH' && !exists $response->{BodyHtml}; |
32
|
|
|
|
|
|
|
|
33
|
12
|
|
|
|
|
1944
|
$response |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _objectify { |
37
|
59
|
|
|
59
|
|
122
|
my ($response) = @_; |
38
|
|
|
|
|
|
|
|
39
|
59
|
100
|
|
|
|
247
|
return map { _objectify($_) } @$response if ref $response eq 'ARRAY'; |
|
47
|
|
|
|
|
1559
|
|
40
|
53
|
|
|
|
|
363
|
WebService::TDWTF::Article->new($response) |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub article { |
44
|
6
|
|
|
6
|
1
|
1847
|
my ($id_or_slug, $only_body_and_html) = @_; |
45
|
6
|
|
|
|
|
81
|
my $url = "$BASE_URL/articles/"; |
46
|
6
|
100
|
|
|
|
59
|
$url .= @_ == 0 ? 'random' : looks_like_number $id_or_slug ? "/id/$id_or_slug" : "/slug/$id_or_slug"; |
|
|
100
|
|
|
|
|
|
47
|
6
|
100
|
|
|
|
21
|
$url .= '/true' if $only_body_and_html; |
48
|
6
|
|
|
|
|
23
|
_objectify _query $url |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _list { |
52
|
6
|
|
|
6
|
|
38
|
my $url = join '/', $BASE_URL, @_; |
53
|
6
|
|
|
|
|
25
|
_objectify _query $url |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
4
|
100
|
|
4
|
1
|
4476
|
sub list_recent { my $url = @_ == 2 ? 'articles' : 'articles/recent'; _list $url, @_ } |
|
4
|
|
|
|
|
17
|
|
57
|
1
|
|
|
1
|
1
|
993
|
sub list_series { _list 'series', @_ } |
58
|
1
|
|
|
1
|
1
|
1715
|
sub list_author { _list 'author', @_ } |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
BEGIN { |
61
|
2
|
|
|
2
|
|
1172
|
*tdwtf_article = \&article; |
62
|
2
|
|
|
|
|
4
|
*tdwtf_list_recent = \&list_recent; |
63
|
2
|
|
|
|
|
4
|
*tdwtf_list_series = \&list_series; |
64
|
2
|
|
|
|
|
59
|
*tdwtf_list_author = \&list_author; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
__END__ |