Reddit.pm

filter: Statementstmt 34.6% 43 / 124 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 32.1% 9 / 28 Podpod 60.0% 3 / 5 totaltotal 28.0% 55 / 196 CC 13 SCAR 20.3
CC
13
Cov
28%
get_user_info
20.0
vote
20.0
_parse_comment_id
12.0
CRAP 7.6
↑ Summary Reddit/Type/Subreddit.pm »
1 package Reddit;
2 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
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  
7 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Reddit - Perl extension for http://www.reddit.com
16  
17 See github for the most up to date/development branch: https://github.com/three18ti/Reddit.pm
18  
19 =head1 SYNOPSIS
20  
21 use Reddit;
22
23 # instantatiate a new reddit object
24 # Automajically handles logging in and cookie handling
25 $r = Reddit->new(
26 {
27 user_name => 'Foo',
28 password => 'Bar',
29 subreddit => 'Perl'
30 }
31 );
32  
33 # Submit a link
34 # $title, $url, $subreddit
35 # This overrides a subreddit set duriing instantiation
36 $r->submit_link( 'Test', 'http://example.com', 'NotPerl');
37  
38 # Submit a Self Post
39 # $title, $text, $subreddit
40 # This overrides a subreddit set during instantiation
41 $r->submit_story( 'Self.test', 'Some Text Here', 'shareCoding');
42  
43 # Post a top level comment to a URL or .self post
44 $r->comment($post_id, $comment);
45
46 # Post a reply to a comment
47 $r->comment($comment_id, $comment);
48  
49 =head1 DESCRIPTION
50  
51 Perl module for interacting with Reddit.
52  
53 This module is still largely inprogress.
54  
55 =head2 Requires
56  
57 common::sense
58 LWP::UserAgent
59 JSON
60 HTTP::Cookies
61  
62 For Testing:
63 Data::Dumper
64  
65 =head2 EXPORT
66  
67 None.
68  
69 =cut
70  
71 # for testing purposes only
72 #use lib './';
73  
74 1 ▶use Reddit::Type::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 #use Reddit::Type::Subreddit;
76  
77 1 has 'base_url' => (
78 is => 'ro',
79 isa => 'Str',
80 default => 'http://www.reddit.com/',
81 );
82  
83 has 'api_url' => (
84 is => 'ro',
85 isa => 'Str',
86 lazy => 1,
87 0 ▶ default => sub { $_[0]->base_url . 'api/' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 1 );
89  
90 has 'login_api' => (
91 is => 'ro',
92 isa => 'Str',
93 lazy => 1,
94 0 ▶ default => sub { $_[0]->api_url . 'login' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 1 );
96  
97 has 'submit_api' => (
98 is => 'ro',
99 isa => 'Str',
100 lazy => 1,
101 0 ▶ default => sub { $_[0]->api_url . 'submit' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 1 );
103  
104 has 'comment_api' => (
105 is => 'ro',
106 isa => 'Str',
107 lazy => 1,
108 0 ▶ default => sub { $_[0]->api_url . 'comment' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 1 );
110  
111 has 'vote_api' => (
112 is => 'ro',
113 isa => 'Str',
114 lazy => 1,
115 0 ▶ default => sub { $_[0]->api_url . 'vote' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 1 );
117  
118 1 has 'api_type' => (
119 is => 'ro',
120 isa => 'Str',
121 default => 'json',
122 );
123  
124 has 'ua' => (
125 is => 'rw',
126 isa => 'LWP::UserAgent',
127 0 ▶ default => sub { LWP::UserAgent->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 # handles => qr/^(?:head|get|post|agent|request.*)/,
129 1 handles => {
130 post => 'post',
131 get => 'get',
132 agent_cookie_jar => 'cookie_jar'
133 }
134 );
135  
136 has 'cookie_jar' => (
137 is => 'rw',
138 isa => 'HTTP::Cookies',
139 lazy => 1,
140 0 ▶ default => sub { HTTP::Cookies->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 1 );
142  
143 1 has [ 'user_name', 'password', ] => (
144 is => 'rw',
145 isa => 'Str',
146 required => 1,
147 trigger => \&_login,
148 );
149  
150 1 has 'subreddit' => (
151 is => 'rw',
152 isa => 'Str',
153 );
154  
155 1 has 'modhash' => (
156 is => 'rw',
157 isa => 'Str',
158 );
159  
160 1 has '_user_search_name' => (
161 is => 'rw',
162 isa => 'Str',
163 lazy => 1,
164 default => '',
165 );
166  
167 has 'about_user_api' => (
168 is => 'rw',
169 isa => 'Str',
170 lazy => 1,
171 0 ▶ default => sub { $_[0]->base_url . 'user/' . $_[0]->_user_search_name . '/about.json' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 1 );
173  
174 has 'user_info' => (
175 is => 'rw',
176 isa => 'Reddit::Type::User',
177 lazy => 1,
178 0 ▶ default => sub { Reddit::Type::User->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 1 );
180  
181 0 ▶sub _login {
Subroutine _login: not called CC=1 SCAR=6.9
182 0 my $self = shift;
183
184 0 my $response = $self->ua->post($self->login_api,
185 {
186 api_type => $self->api_type,
187 user => $self->user_name,
188 passwd => $self->password,
189 }
190 );
191  
192 0 $self->_set_cookie($response);
193 }
194  
195 0 ▶sub _set_cookie {
Subroutine _set_cookie: not called CC=1 SCAR=6.9
196 0 my $self = shift;
197 0 my $response = shift;
198  
199 0 $self->cookie_jar->extract_cookies ($response);
200 0 $self->agent_cookie_jar ($self->cookie_jar);
201 0 $self->_parse_modhash ($response);
202 }
203  
204 0 ▶sub _parse_modhash {
Subroutine _parse_modhash: not called CC=1 SCAR=6.9
205 0 my $self = shift;
206 0 my $response = shift;
207  
208 0 my $decoded = from_json ($response->content);
209 0 $self->modhash ($decoded->{json}{data}{modhash});
210 }
211  
212 0 ▶sub _parse_link {
Subroutine _parse_link: not called CC=1 SCAR=6.9
213 0 my $self = shift;
214 0 my $link = shift;
215  
216 0 my ($id) = $link =~ /comments\/(\w+)\//i;
217 0 return 't3_' . $id;
218 }
219  
220 0 ▶sub _parse_comment_id {
Subroutine _parse_comment_id: not called CC=3 SCAR=24.8
221 # ID's require a t3_ or t1_ prefix depending on whether it is a
222 # post or comment id respectively.
223 0 my $id = shift;
224 0 ▶ if (length($id) == 5){ $id = "t3_" . $id}
Branch: 2 branches
BranchTrueFalseTotal
if (length $id == 5) { } 0 0 0
elsif (length $id == 7) { } 0 0 0
225 0 elsif (length($id) == 7){ $id = "t1_" . $id}
226 0 else { die "Invalid ID length"}
227 0 return $id;
228 }
229  
230 =head1 Provided Methods
231  
232 =over 2
233  
234 =item B<submit_link($title, $url, $subreddit)>
235  
236 $r->submit_link( 'Test', 'http://example.com', 'NotPerl');
237  
238 This method posts links to the specified subreddit. The subreddit parameter is optional if it is not set at the time of instantiation
239 $subreddit is required in one place or the other, subreddit specified here will take precedence over the subreddit specified at time of instantiation.
240  
241 =back
242  
243 =cut
244  
245 # Submit link to reddit
246 0 ▶sub submit_link {
Subroutine submit_link: not called CC=3 SCAR=24.8
Pod documented
247 0 my $self = shift;
248 0 my ($title, $url, $subreddit) = @_;
249  
250 0 my $kind = 'link';
251  
252 0 ▶ my $newpost = $self->ua->post($self->submit_api,
Condition0%0/6
$subreddit || $self->subreddit
l!l&&r!l&&!r
000
$subreddit || $self->subreddit
l!l&&r!l&&!r
000
MC/DC0%0/2
$subreddit || $self->subreddit
$subreddit $self->subreddit
MC/DC0%0/2
$subreddit || $self->subreddit
$subreddit $self->subreddit
Truth table0%0/3
A || B
A= $subredditB= $self->subreddit
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $subredditB= $self->subreddit
ABresult
1X1
011
000
253 {
254 uh => $self->modhash,
255 kind => $kind,
256 sr => $subreddit || $self->subreddit,
257 title => $title,
258 r => $subreddit || $self->subreddit,
259 url => $url,
260 }
261 );
262  
263 0 my $json_content = $newpost->content;
264 0 my $decoded = from_json $json_content;
265  
266 #returns link to new post if successful
267 0 my $link = $decoded->{jquery}[18][3][0];
268 0 my $id = $self->parse_link($link);
269  
270 0 return $id, $link;
271 }
272  
273 =over 2
274  
275 =item B<submit_story($title, $text, $subreddit)>
276  
277 $r->submit_story( 'Self.test', 'Some Text Here', 'shareCoding');
278  
279 This method makes a Self.post to the specified subreddit. The subreddit parameter is optional if it is not set at the time of instantiation
280 $subreddit is required in one place or the other, subreddit specified here will take precedence over the subreddit specified at time of instantiation.
281  
282 =back
283  
284 =cut
285  
286 0 ▶sub submit_story {
Subroutine submit_story: not called CC=3 SCAR=24.8
Pod documented
287 0 my $self = shift;
288 0 my ($title, $text, $subreddit) = @_;
289
290 0 my $kind = 'self';
291 0 ▶ my $newpost = $self->post($self->submit_api,
Condition0%0/6
$subreddit || $self->subreddit
l!l&&r!l&&!r
000
$subreddit || $self->subreddit
l!l&&r!l&&!r
000
MC/DC0%0/2
$subreddit || $self->subreddit
$subreddit $self->subreddit
MC/DC0%0/2
$subreddit || $self->subreddit
$subreddit $self->subreddit
Truth table0%0/3
A || B
A= $subredditB= $self->subreddit
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $subredditB= $self->subreddit
ABresult
1X1
011
000
292 {
293 uh => $self->modhash,
294 kind => $kind,
295 sr => $subreddit || $self->subreddit,
296 r => $subreddit || $self->subreddit,
297 title => $title,
298 text => $text,
299 },
300 );
301  
302 0 my $json_content = $newpost->content;
303 0 my $decoded = from_json $json_content;
304  
305 #returns id and link to new post if successful
306 0 my $link = $decoded->{jquery}[12][3][0];
307 0 my $id = $self->_parse_link($link);
308  
309 0 return $id, $link;
310 }
311  
312 =over 2
313  
314 =item B<comment($post_id, $comment)>
315
316 To post a top level comment to a URL or .self post
317  
318 $r->comment($post_id, $comment);
319  
320 To post a reply to a comment
321
322 $r->comment($comment_id, $comment);
323  
324 This methid requires you pass in the cannonical thing ID with the correct thing prefix.
325 Submit methods return cannonical thing IDs, L<See the FULLNAME Glossary|https://github.com/reddit/reddit/wiki/API> for futher information
326  
327 The post_id is the alphanumeric string after the name of the subreddit, before the title of the post
328 The comment_id is the alphanumeric string after the title of the post
329  
330 =back
331  
332 =cut
333  
334 0 ▶sub comment {
Subroutine comment: not called CC=1 SCAR=6.9
Pod documented
335 0 my $self = shift;
336 0 my ($thing_id, $comment) = @_;
337 0 $thing_id = $self->_parse_comment_id($thing_id);
338 0 my $response = $self->post($self->comment_api,
339 {
340 thing_id => $thing_id,
341 text => $comment,
342 uh => $self->modhash,
343 },
344 );
345  
346 0 my $decoded = from_json $response->content;
347 0 return $decoded->{jquery}[18][3][0][0]->{data}{id};
348 }
349  
350 0 ▶sub get_user_info {
Subroutine get_user_info: not called CC=4 SCAR=30.0
Pod undocumented
351 0 my $self = shift;
352 0 my $search_name = shift;
353  
354 0 $self->_user_search_name($search_name);
355  
356 0 my $response = $self->get ($self->about_user_api);
357 0 my $decoded = from_json $response->content;
358 0 my $data = $decoded->{data};
359  
360 0 ▶ while (my ($key, $value) = each %{$data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data;}) 0 0 0
361 0 ▶ if (JSON::is_bool ref $value){
Branch: 1 branch
BranchTrueFalseTotal
if (&JSON::is_bool(ref $value)) 0 0 0
362 0 ▶ $value = $value ? '1' : '0' ;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
363 }
364 0 $self->user_info->$key($value);
365 }
366 0 return $self->user_info;
367 }
368  
369 0 ▶sub vote {
Subroutine vote: not called CC=4 SCAR=30.0
Pod undocumented
370 0 my $self = shift;
371 0 my ($thing_id, $direction) = @_;
372
373 0 given ($direction) {
374 0 ▶ when ( /up/i || 1) {
Condition0%0/2
/up/i || 1
l!l
00
MC/DC0%0/1
/up/i || 1
/up/i
Truth table0%0/2
A
A= /up/i
Aresult
11
00
375 0 $direction = 1;
376 }
377 0 ▶ when ( /down/i || -1) {
Condition0%0/2
/down/i || -1
l!l
00
MC/DC0%0/1
/down/i || -1
/down/i
Truth table0%0/2
A
A= /down/i
Aresult
11
00
378 0 $direction = -1;
379 }
380 0 ▶ when ( /rescind/i || 0 ) {
Condition0%0/2
/rescind/i || 0
l!l
00
MC/DC0%0/1
/rescind/i || 0
/rescind/i
Truth table0%0/2
A
A= /rescind/i
Aresult
11
00
381 0 $direction = 0;
382 }
383 0 default {
384 0 warn "Please enter a valid direction";
385 0 return 0;
386 }
387 }
388  
389 0 my $response = $self->post ( $self->vote_api,
390 {
391 id => $thing_id,
392 dir => $direction,
393 uh => $self->modhash
394 }
395 );
396
397 0 return $response->content;
398 }
399  
400  
401  
402 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
403 1 __PACKAGE__->meta->make_immutable;
404  
405 1 1;
406 __END__
↑ Summary Reddit/Type/Subreddit.pm »