WebSearch/Provider/Reddit.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 53.3% 16 / 30 Conditioncond 52.5% 21 / 40 MC/DCmcdc 18.1% 4 / 22 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 72.4% 139 / 192 CC 34 SCAR 22.6
CC
34
Cov
76%
search
16.6
_parse_listing
16.5
_init
7.2
CRAP 9.6
« WebSearch/Provider/Reddit/OAuth.pm ↑ Summary WebSearch/Provider/Google.pm »
1 package Net::Async::WebSearch::Provider::Reddit;
2 3 our $VERSION = '0.003';
3 # ABSTRACT: Reddit search provider (keyless JSON endpoint)
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Net::Async::WebSearch::Provider';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use HTTP::Request::Common qw( GET );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Net::Async::WebSearch::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶sub _init {
Subroutine _init: called CC=7 SCAR=19.7
15 14 my ( $self ) = @_;
16 14 ▶ $self->{endpoint} ||= 'https://www.reddit.com';
Condition100%2/2
$self->{"endpoint"} ||= "https://www.reddit.com"
l!l
131
MC/DC100%1/1
$self->{"endpoint"} ||= "https://www.reddit.com"
$self->{"endpoint"}
Truth table100%2/2
A
A= $self->{"endpoint"}
Aresult
11
00
17 14 $self->{endpoint} =~ s{/+$}{};
18 14 ▶ $self->{name} ||= 'reddit';
Condition100%2/2
$self->{"name"} ||= "reddit"
l!l
131
MC/DC100%1/1
$self->{"name"} ||= "reddit"
$self->{"name"}
Truth table100%2/2
A
A= $self->{"name"}
Aresult
11
00
19 14 ▶ $self->{subreddit} //= undef;
Condition50%1/2
$self->{"subreddit"} //= undef
l!l
014
MC/DC0%0/1
$self->{"subreddit"} //= undef
$self->{"subreddit"}
Truth table50%1/2
A
A= $self->{"subreddit"}
Aresult
11
00
20 14 ▶ $self->{sort} ||= 'relevance'; # relevance | hot | top | new | comments
Condition50%1/2
$self->{"sort"} ||= "relevance"
l!l
014
MC/DC0%0/1
$self->{"sort"} ||= "relevance"
$self->{"sort"}
Truth table50%1/2
A
A= $self->{"sort"}
Aresult
11
00
21 14 ▶ $self->{time} ||= 'all'; # hour | day | week | month | year | all
Condition50%1/2
$self->{"time"} ||= "all"
l!l
014
MC/DC0%0/1
$self->{"time"} ||= "all"
$self->{"time"}
Truth table50%1/2
A
A= $self->{"time"}
Aresult
11
00
22 14 ▶ $self->{link_base} ||= 'https://www.reddit.com';
Condition100%2/2
$self->{"link_base"} ||= "https://www.reddit.com"
l!l
131
MC/DC100%1/1
$self->{"link_base"} ||= "https://www.reddit.com"
$self->{"link_base"}
Truth table100%2/2
A
A= $self->{"link_base"}
Aresult
11
00
23 }
24  
25 10 ▶sub endpoint { $_[0]->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
26 10 ▶sub subreddit { @_ > 1 ? ($_[0]->{subreddit} = $_[1]) : $_[0]->{subreddit} }
Subroutine subreddit: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 10 10
27 10 ▶sub sort { @_ > 1 ? ($_[0]->{sort} = $_[1]) : $_[0]->{sort} }
Subroutine sort: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 10 10
28 10 ▶sub time { @_ > 1 ? ($_[0]->{time} = $_[1]) : $_[0]->{time} }
Subroutine time: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 10 10
29 9 ▶sub link_base { $_[0]->{link_base} }
Subroutine link_base: called CC=1 SCAR=0.0
Pod documented
30  
31 1 ▶sub search {
Subroutine search: called CC=11 SCAR=28.1
Pod documented
32 1 my ( $self, $http, $query, $opts ) = @_;
33 1 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
10
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
34 1 ▶ my $limit = $opts->{limit} || 10;
Condition50%1/2
$opts->{'limit'} || 10
l!l
10
MC/DC0%0/1
$opts->{'limit'} || 10
$opts->{'limit'}
Truth table50%1/2
A
A= $opts->{'limit'}
Aresult
11
00
35  
36 1 ▶ my $sub = defined $opts->{subreddit} ? $opts->{subreddit} : $self->subreddit;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'subreddit'} ? : 0 1 1
37 1 ▶ my $path = defined $sub && length $sub ? "/r/$sub/search.json" : "/search.json";
Branch: 1 branch
BranchTrueFalseTotal
defined $sub && length $sub ? : 0 1 1
Condition33%1/3
defined $sub and length $sub
!ll&&!rl&&r
100
MC/DC0%0/2
defined $sub and length $sub
defined $sub length $sub
Truth table33%1/3
A and B
A= defined $subB= length $sub
ABresult
0X0
100
111
38  
39 1 my $uri = URI->new( $self->endpoint . $path );
40 my %q = (
41 q => $query,
42 limit => $limit,
43 sort => $opts->{sort} // $self->sort,
44 1 ▶ t => $opts->{time} // $self->time,
Condition33%2/6
$opts->{"sort"} // $self->sort
l!l&&r!l&&!r
010
$opts->{"time"} // $self->time
l!l&&r!l&&!r
010
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 table33%1/3
A // B
A= $opts->{"sort"}B= $self->sort
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $opts->{"time"}B= $self->time
ABresult
1X1
011
000
45 );
46 1 ▶ $q{restrict_sr} = 1 if defined $sub && length $sub;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sub and length $sub 0 1 1
Condition33%1/3
defined $sub and length $sub
!ll&&!rl&&r
100
MC/DC0%0/2
defined $sub and length $sub
defined $sub length $sub
Truth table33%1/3
A and B
A= defined $subB= length $sub
ABresult
0X0
100
111
47 1 ▶ $q{include_over_18} = $opts->{include_nsfw} ? 'on' : 'off';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'include_nsfw'} ? : 0 1 1
48 1 $uri->query_form(%q);
49  
50 1 my $req = GET( $uri->as_string );
51 # Reddit is picky — generic UAs get rate-limited hard.
52 1 $req->header( 'User-Agent' => $self->user_agent_string );
53 1 $req->header( 'Accept' => 'application/json' );
54  
55 1 ▶ return $http->do_request( request => $req )->then(sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
56 1 my ( $resp ) = @_;
57 1 ▶ unless ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 1 1
58 0 return Future->fail(
59 $self->name.": HTTP ".$resp->status_line, 'websearch', $self->name,
60 );
61 }
62 1 my $data = eval { decode_json( $resp->decoded_content ) };
63 1 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 1 1
64 0 return Future->fail( $self->name.": invalid JSON: $e", 'websearch', $self->name );
65 }
66 1 return Future->done( $self->_parse_listing($data, $limit) );
67 1 });
68 }
69  
70 10 ▶sub _parse_listing {
Subroutine _parse_listing: called CC=13 SCAR=28.0
71 10 my ( $self, $data, $limit ) = @_;
72 10 my @out;
73 10 my $rank = 0;
74 10 ▶ for my $child ( @{ ($data->{data} || {})->{children} || [] } ) {
Condition50%2/4
+($data->{'data'} || {})->{'children'} || []
l!l
100
$data->{'data'} || {}
l!l
100
MC/DC0%0/1
+($data->{'data'} || {})->{'children'} || []
$data->{'data'}
Truth table50%1/2
A
A= $data->{'data'}
Aresult
11
00
75 9 ▶ my $d = $child->{data} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $d = $child->{"data"} or next 9 0 9
76 9 $rank++;
77 9 ▶ my $permalink = $self->link_base . ( $d->{permalink} // '' );
Condition50%1/2
$d->{"permalink"} // ""
l!l
90
MC/DC0%0/1
$d->{"permalink"} // ""
$d->{"permalink"}
Truth table50%1/2
A
A= $d->{"permalink"}
Aresult
11
00
78 9 ▶ my $target = $d->{url} // $permalink;
Condition33%1/3
$d->{"url"} // $permalink
l!l&&r!l&&!r
900
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 9 my $iso;
80 9 ▶ if ( defined $d->{created_utc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d->{"created_utc"}) 9 0 9
81 9 my @t = gmtime( $d->{created_utc} );
82 9 $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 9 ▶ ( defined $d->{created_utc} ? ( created_utc => $d->{created_utc} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
$d->{'selftext'} && length $d->{'selftext'} ? : 1 8 9
$d->{'over_18'} ? : 0 9 9
defined $d->{'created_utc'} ? : 9 0 9
Condition66%2/3
$d->{"selftext"} and length $d->{"selftext"}
!ll&&!rl&&r
801
MC/DC50%1/2
$d->{"selftext"} and length $d->{"selftext"}
$d->{"selftext"} length $d->{"selftext"}
Truth table66%2/3
A and B
A= $d->{"selftext"}B= length $d->{"selftext"}
ABresult
0X0
100
111
104 },
105 );
106 9 ▶ last if $rank >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $rank >= $limit 0 9 9
107 }
108 10 return \@out;
109 }
110  
111 3 1;
112  
113 __END__
« WebSearch/Provider/Reddit/OAuth.pm ↑ Summary WebSearch/Provider/Google.pm »