blib/lib/WebService/Vichan.pm

filter: Statementstmt 94.7% 90 / 95 Branchbran 57.8% 22 / 38 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 18 / 18 Podpod 77.7% 7 / 9 totaltotal 83.3% 140 / 168 CC 22 SCAR 13.4
CC
22
Cov
83%
do_request
8.2
thread
6.3
boards
4.0
CRAP 3.8
↑ Summary
1 package WebService::Vichan;
2  
3 1 ▶use 5.014000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Hash::Inflator;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Time::HiRes qw/time sleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '0.001001';
14  
15 1 our %cache;
16 1 our $last_request = 0;
17 1 our $ht = HTTP::Tiny->new(
18 agent => 'WebService-Vichan/'.$VERSION,
19 verify_SSL => 1
20 );
21  
22 1 ▶use constant +{
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 API_4CHAN => 'https://a.4cdn.org',
24 API_8CHAN => 'https://8ch.net',
25 1 };
26  
27 1 our @EXPORT_OK = qw/API_4CHAN API_8CHAN/;
28 1 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
29  
30 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
31 2 my ($class, $url) = @_;
32 2 bless { url => $url }, $class
33 }
34  
35 8 ▶sub do_request {
Subroutine do_request: called CC=6 SCAR=21.0
Pod undocumented
36 8 my ($url, $cached_result, $cached_timestamp) = @_;
37 8 my %options;
38 8 ▶ if ($cached_timestamp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_timestamp) 0 8 8
39 0 $options{headers}{'If-Modified-Since'} = $cached_timestamp
40 }
41 8 my $time_since_last_request = time - $last_request;
42 8 ▶ sleep 1 - $time_since_last_request if $time_since_last_request < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $time_since_last_request < 1 7 1 8
43 8 my $result = $ht->get($url, \%options);
44 8 $last_request = time;
45 8 ▶ if ($result->{status} == 304) {
Branch: 2 branches
BranchTrueFalseTotal
if ($result->{'status'} == 304) { } 0 8 8
elsif (not $result->{'success'}) { } 0 8 8
46 0 [$cached_result, $cached_timestamp]
47 } elsif (!$result->{success}) {
48 0 my $diestr = sprintf "Error requesting %s: %s\n", $url, $result->{reason};
49 0 ▶ die $diestr unless $result->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $result->{"success"} 0 0 0
50 } else {
51 8 [$result->{content}, $last_request]
52 }
53 }
54  
55 12 ▶sub requestf {
Subroutine requestf: called CC=3 SCAR=11.1
Pod undocumented
56 12 my ($self, $format, @args) = @_;
57 12 my $what = sprintf $format, @args;
58 12 my $url = $self->{url} . '/' . $what;
59 12 my $result = $cache{$url};
60 12 ▶ if (!defined $result) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $result) { } 8 4 12
elsif (&WebService::Vichan::time() - $result->[1] > 10) { } 0 4 4
61 8 $cache{$url} = do_request $url
62 } elsif (time - $result->[1] > 10) {
63 0 $cache{$url} = do_request $url, @$result
64 }
65 12 decode_json $cache{$url}->[0]
66 }
67  
68 2 ▶sub boards {
Subroutine boards: called CC=4 SCAR=14.0
Pod documented
69 2 my ($self) = @_;
70 2 my $result = $self->requestf('boards.json');
71 2 ▶ $result = $result->{boards} if ref $result eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "HASH" 1 1 2
72 my @results = map {
73 2 ▶ $_->{board} //= $_->{uri};
Condition66%2/3
$_->{"board"} //= $_->{"uri"}
l!l&&r!l&&!r
773540
MC/DC0%0/2
$_->{"board"} //= $_->{"uri"}
$_->{"board"} $_->{"uri"}
Truth table66%2/3
A //= B
A= $_->{"board"}B= $_->{"uri"}
ABresult
1X1
011
000
74 431 Hash::Inflator->new($_)
75 } @$result;
76 2 ▶ wantarray ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
77 }
78  
79 4 ▶sub threads {
Subroutine threads: called CC=3 SCAR=11.2
Pod documented
80 4 my ($self, $board) = @_;
81 4 ▶ $board = $board->{board} if ref $board;
Branch: 1 branch
BranchTrueFalseTotal
if ref $board 4 0 4
82 4 my $result = $self->requestf('%s/threads.json', $board);
83 4 my @pages = map { Hash::Inflator->new($_) } @$result;
84 4 ▶ wantarray ? @pages : \@pages
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
85 }
86  
87 2 ▶sub threads_flat {
Subroutine threads_flat: called CC=2 SCAR=7.0
Pod documented
88 2 my @pages = shift->threads(@_);
89 2 my @flat = map { @{$_->{threads}} } @pages;
90 2 ▶ wantarray ? @flat : \@flat
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
91 }
92  
93 4 ▶sub catalog {
Subroutine catalog: called CC=3 SCAR=11.2
Pod documented
94 4 my ($self, $board) = @_;
95 4 ▶ $board = $board->{board} if ref $board;
Branch: 1 branch
BranchTrueFalseTotal
if ref $board 4 0 4
96 4 my $result = $self->requestf('%s/catalog.json', $board);
97 4 my @pages = map { Hash::Inflator->new($_) } @$result;
98 4 ▶ wantarray ? @pages : \@pages
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
99 }
100  
101 2 ▶sub catalog_flat {
Subroutine catalog_flat: called CC=2 SCAR=7.0
Pod documented
102 2 my @pages = shift->catalog(@_);
103 2 my @flat = map { @{$_->{threads}} } @pages;
104 2 ▶ wantarray ? @flat : \@flat
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
105 }
106  
107 2 ▶sub thread {
Subroutine thread: called CC=6 SCAR=18.4
Pod documented
108 2 my ($self, $board, $threadno, $is_4chan) = @_;
109 2 ▶ $board = $board->{board} if ref $board;
Branch: 1 branch
BranchTrueFalseTotal
if ref $board 2 0 2
110 2 ▶ $threadno = $threadno->{no} if ref $threadno;
Branch: 1 branch
BranchTrueFalseTotal
if ref $threadno 2 0 2
111 2 ▶ $is_4chan //= (index $self->{url}, '4cdn.org') >= 0;
Condition50%1/2
$is_4chan //= index($self->{"url"}, "4cdn.org") != -1
l!l
02
MC/DC0%0/1
$is_4chan //= index($self->{"url"}, "4cdn.org") != -1
$is_4chan
Truth table50%1/2
A
A= $is_4chan
Aresult
11
00
112 2 ▶ my $res_or_thread = $is_4chan ? 'thread' : 'res';
Branch: 1 branch
BranchTrueFalseTotal
$is_4chan ? : 1 1 2
113 2 my $result =
114 $self->requestf('%s/%s/%s.json', $board, $res_or_thread, $threadno);
115 2 my @posts = map { Hash::Inflator->new($_) } @{$result->{posts}};
116 2 ▶ wantarray ? @posts : \@posts
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
117 }
118  
119 1 1;
120 __END__
↑ Summary