WebSearch/Provider/Reddit.pm

filter: Statementstmt 71.4% 55 / 77 Branchbran 31.2% 10 / 32 Conditioncond 26.3% 10 / 38 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 88.2% 15 / 17 Podpod 100% 6 / 6 totaltotal 50.0% 96 / 192 CC 34 SCAR 35.9
CC
34
Cov
51%
search
132.0
_parse_listing
18.8
__ANON__
12.0
CRAP 36.3
« WebSearch/Provider/Serper.pm ↑ Summary WebSearch/Provider/DuckDuckGo.pm »
1 package Net::Async::WebSearch::Provider::Reddit;
2 2 our $VERSION = '0.002';
3 # ABSTRACT: Reddit search provider (keyless JSON endpoint)
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use parent 'Net::Async::WebSearch::Provider';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Request::Common qw( GET );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::Async::WebSearch::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub _init {
15 13 ▶ my ( $self ) = @_;
Subroutine _init: called CC=7 SCAR=21.2
16 13 ▶ $self->{endpoint} ||= 'https://www.reddit.com';
Condition50%1/2
$self->{"endpoint"} ||= "https://www.reddit.com"
l!l
130
MC/DC0%0/1
$self->{"endpoint"} ||= "https://www.reddit.com"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
17 13 $self->{endpoint} =~ s{/+$}{};
18 13 ▶ $self->{name} ||= 'reddit';
Condition50%1/2
$self->{"name"} ||= "reddit"
l!l
130
MC/DC0%0/1
$self->{"name"} ||= "reddit"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
19 13 ▶ $self->{subreddit} //= undef;
Condition50%1/2
$self->{"subreddit"} //= undef
l!l
013
MC/DC0%0/1
$self->{"subreddit"} //= undef
$self->{"subreddit"}
Truth table50%1/2
A
A= $self->{"subreddit"}
Aresult
11
00
20 13 ▶ $self->{sort} ||= 'relevance'; # relevance | hot | top | new | comments
Condition50%1/2
$self->{"sort"} ||= "relevance"
l!l
013
MC/DC0%0/1
$self->{"sort"} ||= "relevance"
$self->{"sort"}
Truth table50%1/2
A
A= $self->{"sort"}
Aresult
11
00
21 13 ▶ $self->{time} ||= 'all'; # hour | day | week | month | year | all
Condition50%1/2
$self->{"time"} ||= "all"
l!l
013
MC/DC0%0/1
$self->{"time"} ||= "all"
$self->{"time"}
Truth table50%1/2
A
A= $self->{"time"}
Aresult
11
00
22 13 ▶ $self->{link_base} ||= 'https://www.reddit.com';
Condition50%1/2
$self->{"link_base"} ||= "https://www.reddit.com"
l!l
130
MC/DC0%0/1
$self->{"link_base"} ||= "https://www.reddit.com"
$self->{"link_base"}
Truth table50%1/2
A
A= $self->{"link_base"}
Aresult
11
00
23 }
24  
25 9 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
26 9 ▶sub subreddit { @_ > 1 ? ($_[0]->{subreddit} = $_[1]) : $_[0]->{subreddit} }
Subroutine subreddit: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 9 9
27 9 ▶sub sort { @_ > 1 ? ($_[0]->{sort} = $_[1]) : $_[0]->{sort} }
Subroutine sort: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 9 9
28 9 ▶sub time { @_ > 1 ? ($_[0]->{time} = $_[1]) : $_[0]->{time} }
Subroutine time: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 9 9
29 7 ▶sub link_base { $_[0]->{link_base} }
Subroutine link_base: called CC=1 SCAR=0.0
Pod documented
30  
31 sub search {
32 0 ▶ my ( $self, $http, $query, $opts ) = @_;
Subroutine search: not called CC=11 SCAR=48.8
Pod documented
33 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
34 0 ▶ my $limit = $opts->{limit} || 10;
Condition0%0/2
$opts->{'limit'} || 10
l!l
00
MC/DC0%0/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table0%0/2
A
A= $opts->{'limit'}
Aresult
11
00
35  
36 0 ▶ my $sub = defined $opts->{subreddit} ? $opts->{subreddit} : $self->subreddit;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'subreddit'} ? : 0 0 0
37 0 ▶ my $path = defined $sub && length $sub ? "/r/$sub/search.json" : "/search.json";
Branch: 1 branch
BranchTrueFalseTotal
defined $sub && length $sub ? : 0 0 0
Condition0%0/3
defined $sub and length $sub
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sub and length $sub
defined $sub length $sub
Truth table0%0/3
A and B
A= defined $subB= length $sub
ABresult
0X0
100
111
38  
39 0 my $uri = URI->new( $self->endpoint . $path );
40 my %q = (
41 q => $query,
42 limit => $limit,
43 sort => $opts->{sort} // $self->sort,
44 0 ▶ t => $opts->{time} // $self->time,
Condition0%0/6
$opts->{"sort"} // $self->sort
l!l&&r!l&&!r
000
$opts->{"time"} // $self->time
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"sort"} // $self->sort
$opts->{"sort"} $self->sort
MC/DC0%0/2
$opts->{"time"} // $self->time
$opts->{"time"} $self->time
Truth table0%0/3
A // B
A= $opts->{"sort"}B= $self->sort
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $opts->{"time"}B= $self->time
ABresult
1X1
011
000
45 );
46 0 ▶ $q{restrict_sr} = 1 if defined $sub && length $sub;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sub and length $sub 0 0 0
Condition0%0/3
defined $sub and length $sub
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sub and length $sub
defined $sub length $sub
Truth table0%0/3
A and B
A= defined $subB= length $sub
ABresult
0X0
100
111
47 0 ▶ $q{include_over_18} = $opts->{include_nsfw} ? 'on' : 'off';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'include_nsfw'} ? : 0 0 0
48 0 $uri->query_form(%q);
49  
50 0 my $req = GET( $uri->as_string );
51 # Reddit is picky — generic UAs get rate-limited hard.
52 0 $req->header( 'User-Agent' => $self->user_agent_string );
53 0 $req->header( 'Accept' => 'application/json' );
54  
55 return $http->do_request( request => $req )->then(sub {
56 0 ▶ my ( $resp ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
57 0 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
58 0 return Future->fail(
59 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
60 );
61 }
62 0 my $data = eval { decode_json( $resp->decoded_content ) };
63 0 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
64 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
65 }
66 0 return Future->done( $self->_parse_listing($data, $limit) );
67 0 });
68 }
69  
70 sub _parse_listing {
71 9 ▶ my ( $self, $data, $limit ) = @_;
Subroutine _parse_listing: called CC=13 SCAR=29.3
72 9 my @out;
73 9 my $rank = 0;
74 9 ▶ for my $child ( @{ ($data->{data} || {})->{children} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
+($data->{"data"} || {})->{"children"} or [] 0 9 9
Condition50%1/2
$data->{'data'} || {}
l!l
90
MC/DC0%0/1
$data->{'data'} || {}
$data->{'data'}
Truth table50%1/2
A
A= $data->{'data'}
Aresult
11
00
75 7 ▶ my $d = $child->{data} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $d = $child->{"data"} or next 0 7 7
76 7 $rank++;
77 7 ▶ my $permalink = $self->link_base . ( $d->{permalink} // '' );
Condition50%1/2
$d->{"permalink"} // ""
l!l
70
MC/DC0%0/1
$d->{"permalink"} // ""
$d->{"permalink"}
Truth table50%1/2
A
A= $d->{"permalink"}
Aresult
11
00
78 7 ▶ my $target = $d->{url} // $permalink;
Condition33%1/3
$d->{"url"} // $permalink
l!l&&r!l&&!r
700
MC/DC0%0/2
$d->{"url"} // $permalink
$d->{"url"} $permalink
Truth table33%1/3
A // B
A= $d->{"url"}B= $permalink
ABresult
1X1
011
000
79 7 my $iso;
80 7 ▶ if ( defined $d->{created_utc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d->{"created_utc"}) 7 0 7
81 7 my @t = gmtime( $d->{created_utc} );
82 7 $iso = sprintf '%04d-%02d-%02dT%02d:%02d:%02dZ',
83 $t[5]+1900, $t[4]+1, $t[3], $t[2], $t[1], $t[0];
84 }
85 push @out, Net::Async::WebSearch::Result->new(
86 url => $target,
87 title => $d->{title},
88 snippet => ( $d->{selftext} && length $d->{selftext}
89 ? substr( $d->{selftext}, 0, 400 )
90 : undef ),
91 provider => $self->name,
92 rank => $rank,
93 published_at => $iso,
94 nsfw => ( $d->{over_18} ? 1 : 0 ),
95 domain => $d->{domain},
96 raw => $d,
97 extra => {
98 permalink => $permalink,
99 subreddit => $d->{subreddit},
100 author => $d->{author},
101 reddit_score => $d->{score},
102 num_comments => $d->{num_comments},
103 7 ▶ ( defined $d->{created_utc} ? ( created_utc => $d->{created_utc} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
$d->{'selftext'} && length $d->{'selftext'} ? : 0 7 7
$d->{'over_18'} ? : 0 7 7
defined $d->{'created_utc'} ? : 7 0 7
Condition33%1/3
$d->{"selftext"} and length $d->{"selftext"}
!ll&&!rl&&r
700
MC/DC0%0/2
$d->{"selftext"} and length $d->{"selftext"}
$d->{"selftext"} length $d->{"selftext"}
Truth table33%1/3
A and B
A= $d->{"selftext"}B= length $d->{"selftext"}
ABresult
0X0
100
111
104 },
105 );
106 7 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 7 7
107 }
108 9 return \@out;
109 }
110  
111 2 1;
112  
113 __END__
« WebSearch/Provider/Serper.pm ↑ Summary WebSearch/Provider/DuckDuckGo.pm »