blib/lib/WWW/Reddit.pm

filter: Statementstmt 37.8% 31 / 82 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 47.0% 8 / 17 Podpod 0.0% 0 / 8 totaltotal 28.6% 39 / 136 CC 9 SCAR 16.4
CC
9
Cov
31%
submit_link
12.0
submit_story
12.0
new
6.0
CRAP 5.2
↑ Summary
1 package WWW::Reddit;
2  
3 1 our $VERSION = '0.10';
4  
5 1 ▶use 5.012004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 my $base_url = 'http://www.reddit.com/';
17 1 my $api_url = $base_url . 'api/';
18 1 my $login_url = $api_url . 'login';
19 1 my $submit_url = $api_url . 'submit';
20 1 my $comment_url = $api_url . 'comment';
21  
22 1 my $api_type = 'json';
23  
24 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
25 0 my $obj_class = shift;
26 0 ▶ my $class = ref $obj_class || $obj_class;
Condition0%0/3
ref $obj_class || $obj_class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $obj_class || $obj_class
ref $obj_class $obj_class
Truth table0%0/3
A || B
A= ref $obj_classB= $obj_class
ABresult
1X1
011
000
27
28 0 my ($user, $passwd, $subreddit) = @_;
29  
30 0 my $self = {
31 base_url => $base_url,
32 api_url => $api_url,
33
34 login_url => $login_url,
35 submit_url => $submit_url,
36  
37 api_type => $api_type,
38  
39 user => $user,
40 passwd => $passwd,
41  
42 subreddit => $subreddit,
43  
44 ua => new LWP::UserAgent,
45 cookie_jar => HTTP::Cookies->new,
46  
47 modhash => '',
48 };
49  
50 0 bless $self, $class;
51
52 0 $self->create_methods;
53 0 return $self;
54 }
55  
56 #>-----------------------------------------------<#
57 # Helper Methods
58 #>-----------------------------------------------<#
59  
60 # create accessor/mutator methods for defined parameters
61 0 ▶sub create_methods {
Subroutine create_methods: not called CC=2 SCAR=13.0
Pod undocumented
62 0 my $self = shift;
63 0 for my $datum (keys %{$self}) {
64 1 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
65 0 ▶ *$datum = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
66 0 my $self = shift;
67 0 ▶ $self->{$datum} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
68 0 return $self->{$datum};
69 0 };
70 }
71 }
72  
73 # Set cookie
74 0 ▶sub set_cookie {
Subroutine set_cookie: not called CC=1 SCAR=6.9
Pod undocumented
75 0 my $self = shift;
76 0 my $response = shift;
77  
78 0 $self->cookie_jar->extract_cookies ($response);
79 0 $self->ua->cookie_jar ($self->cookie_jar);
80 0 $self->parse_modhash ($response);
81 }
82  
83 # Set modhash
84 0 ▶sub parse_modhash {
Subroutine parse_modhash: not called CC=1 SCAR=6.9
Pod undocumented
85 0 my $self = shift;
86 0 my $response = shift;
87  
88 0 my $decoded = from_json ($response->content);
89 0 $self->modhash ($decoded->{json}{data}{modhash});
90 }
91  
92 # takes link, returns post ID
93 0 ▶sub parse_link {
Subroutine parse_link: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my $self = shift;
95 0 my $link = shift;
96  
97 0 my ($id) = $link =~ /comments\/(\w+)\//i;
98 0 return $id;
99 }
100  
101 #>---------------------------------------------------<#
102 # Main Methods
103 #>---------------------------------------------------<#
104  
105 # Login to reddit
106 0 ▶sub login {
Subroutine login: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my $self = shift;
108
109 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
110 0 $self->user ($_[0]);
111 0 $self->passwd ($_[1]);
112 }
113 0 my $response = $self->ua->post($self->login_url,
114 {
115 api_type => $self->api_type,
116 user => $self->user,
117 passwd => $self->passwd,
118 }
119 );
120  
121 0 $self->set_cookie( $response);
122 # print Dumper $response;
123 }
124  
125 # Submit link to reddit
126 0 ▶sub submit_link {
Subroutine submit_link: not called CC=3 SCAR=24.8
Pod undocumented
127 0 my $self = shift;
128 0 my ($title, $url, $subreddit) = @_;
129  
130 0 my $kind = 'link';
131  
132 0 ▶ my $newpost = $self->ua->post($self->submit_url,
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
133 {
134 uh => $self->modhash,
135 kind => $kind,
136 sr => $subreddit || $self->subreddit,
137 title => $title,
138 r => $subreddit || $self->subreddit,
139 url => $url,
140 }
141 );
142  
143 0 my $json_content = $newpost->content;
144 0 my $decoded = from_json $json_content;
145  
146 #returns link to new post if successful
147 0 my $link = $decoded->{jquery}[18][3][0];
148 0 my $id = $self->parse_link($link);
149  
150 0 return $id, $link;
151 }
152  
153 0 ▶sub submit_story {
Subroutine submit_story: not called CC=3 SCAR=24.8
Pod undocumented
154 0 my $self = shift;
155 0 my ($title, $text, $subreddit) = @_;
156  
157 0 my $kind = 'self';
158  
159 0 ▶ my $newpost = $self->ua->post($self->submit_url,
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
160 {
161 uh => $self->modhash,
162 kind => $kind,
163 sr => $subreddit || $self->subreddit,
164 r => $subreddit || $self->subreddit,
165 title => $title,
166 text => $text,
167 },
168 );
169  
170 0 my $json_content = $newpost->content;
171 0 my $decoded = from_json $json_content;
172  
173 #returns id and link to new post if successful
174 0 my $link = $decoded->{jquery}[12][3][0];
175 0 my $id = $self->parse_link($link);
176  
177 0 return $id, $link;
178 }
179  
180 1 1;
181 __END__
↑ Summary