blib/lib/Net/ITE.pm

filter: Statementstmt 17.6% 18 / 102 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 34.7% 8 / 23 Podpod n/a totaltotal 16.6% 26 / 156 CC 16 SCAR 21.6
CC
16
Cov
14%
posts
20.0
ping
20.0
topics
15.5
CRAP 8.7
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 =head1 NAME
4  
5 Net::ITE - OOP-ish interface to the Internet Topic Exchange
6  
7 =head1 SYNOPSIS
8  
9 use Net::ITE;
10 my $ite = Net::ITE->new();
11  
12 # Get all the posts for a topic
13  
14 my $topic = $ite->topic("montreal_quebec_canada");
15 my $posts = $topic->posts();
16  
17 while (my $item = $posts->next()) {
18 print $item->title()."\n";
19 print $item->excerpt()."\n";
20 }
21  
22 # Add your post to a topic listing
23  
24 $topic->ping({title=>"foo",
25 url=>"http://foo.com/123",
26 excerpt=>"yadda yadda yadda"});
27  
28 # Create a new topic
29  
30 $ite->new_topic("foobars");
31  
32 =head1 DESCRIPTION
33  
34 OOP-ish interface to the Internet Topic Exchange.
35  
36 =head1 NOTES
37  
38 =over 4
39  
40 =item *
41  
42 The error handling sucks and will be addressed in future releases.
43  
44 =back
45  
46 =cut
47  
48 package Net::ITE;
49  
50 $Net::ITE::VERSION = '0.05';
51  
52 =head1 PACKAGE METHODS
53  
54 =cut
55  
56 =head2 __PACKAGE__->new($blogname)
57  
58 Returns an object.Woot!
59  
60 =cut
61  
62 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
63 3 my $pkg = shift;
64 3 my $blog = shift;
65 3 return bless \$blog,$pkg;
66 }
67  
68 =head1 OBJECT METHODS
69  
70 =cut
71  
72 =head1 Net::ITE
73  
74 =cut
75  
76 =head2 $ite->topics()
77  
78 When called in a scalar context, returns an I<Net::ITE::Iterator>
79 object of I<Net::ITE::Topic> objects.
80  
81 When called in an array context, returns a list of I<Net::ITE::Topic>
82 objects.
83  
84 =cut
85  
86 1 ▶sub topics {
Subroutine topics: called CC=4 SCAR=27.4
87 1 my $self = shift;
88  
89 1 my $data = Net::ITE::Network->get(Net::ITE::Constants->TOPICS."rss");
90  
91 0 ▶ if (! $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
92 0 warn "Unable to topics.\n";
93 0 return undef;
94 }
95  
96 0 my $items = Parser->parse_feed($data);
97  
98 0 ▶ if (! $items) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($items) 0 0 0
99 0 warn "Unable to parse topics.\n";
100 0 return undef;
101 }
102  
103 0 map { $_ = {title=>$_->{title},blog=>$self} } @$items;
104  
105 return wantarray ?
106 0 ▶ map { Net::ITE::Topic->new($_); } @$items :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
107 Net::ITE::Iterator->new("Net::ITE::Topic",$items);
108 }
109  
110 =head2 $ite->topic($topic)
111  
112 Returns a I<Net::ITE::Topic> object.
113  
114 =cut
115  
116 2 ▶sub topic {
Subroutine topic: called CC=1 SCAR=0.0
117 2 my $self = shift;
118 2 return Net::ITE::Topic->new({title=>$_[0],blog=>$self});
119 }
120  
121 =head2 $ite->new_topic($topic)
122  
123 Returns true or false.
124  
125 =cut
126  
127 0 ▶sub new_topic {
Subroutine new_topic: not called CC=3 SCAR=24.8
128 0 my $self = shift;
129  
130 0 my $post = Net::ITE::Network->post(Net::ITE::Constants->NEWTOPIC,
131 {catname=>$_[0]});
132  
133 0 ▶ if (! $post) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($post) 0 0 0
134 0 warn "There was a transport error trying to create new topic.\n";
135 0 return 0;
136 }
137  
138 0 ▶ if ($$post =~ /<b>An error occurred<\/b>:(?:\s)+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$post =~ m[<b>An error occurred</b>:(?:\s)+(.*)$]) 0 0 0
139 0 warn $1,"\n";
140 0 return 0;
141 }
142  
143 0 return 1;
144 }
145  
146 =head1 Net::ITE::Topic
147  
148 =cut
149  
150 package Net::ITE::Topic;
151  
152 my $tb = undef;
153  
154 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
155 2 my $pkg = shift;
156 2 return bless $_[0],$pkg;
157 }
158  
159 =head2 $topic->title()
160  
161 =cut
162  
163 0 ▶sub title { return shift->{title}; }
Subroutine title: not called CC=1 SCAR=6.9
164  
165 =head2 $topic->about()
166  
167 I<Not implemented (yet)>
168  
169 =cut
170  
171 =head2 $topic->url()
172  
173 =cut
174  
175 2 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
176 2 my $self = shift;
177 2 return Net::ITE::Constants->TOPIC.$self->title()."/";
178 }
179  
180 =head2 $topic->posts()
181  
182 When called in a scalar context, returns an I<Net::ITE::Iterator>
183 object of I<Net::ITE::Post> objects.
184  
185 When called in an array context, returns a list of I<Net::ITE::Post>
186 objects.
187  
188 =cut
189  
190 0 ▶sub posts {
Subroutine posts: not called CC=4 SCAR=30.0
191 0 my $self = shift;
192  
193 0 my $data = Net::ITE::Network->get($self->url()."rss");
194  
195 0 ▶ if (! $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
196 0 warn "Unable to retrieve posts for topic.\n";
197 0 return undef;
198 }
199  
200 0 my $posts = Parser->parse_feed($data);
201  
202 0 ▶ if (! $posts) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($posts) 0 0 0
203 0 warn "Unable to parse posts for topic.\n";
204 0 return undef;
205 }
206  
207 return wantarray ?
208 0 ▶ map { Net::ITE::Post->new($_); } @$posts :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
209 Net::ITE::Iterator->new("Net::ITE::Post",$posts);
210 }
211  
212 =head2 $topic->ping(\%args)
213  
214 =over 4
215  
216 =item *
217  
218 B<blog_name>
219  
220 If this property is not passed then the value passed to the I<Net::ITE>
221 constructor will be used.
222  
223 =item *
224  
225 B<title>
226  
227 =item *
228  
229 B<url>
230  
231 =item *
232  
233 B<excerpt>
234  
235 =back
236  
237 Returns true or false.
238  
239 =cut
240  
241 0 ▶sub ping {
Subroutine ping: not called CC=4 SCAR=30.0
242 0 my $self = shift;
243 0 my $data = shift;
244  
245 0 $data->{ping_url} = $self->url();
246 0 ▶ $data->{blog_name} ||= ${$self->{blog}};
Condition0%0/3
$data->{"blog_name"} ||= ${$self->{"blog"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"blog_name"} ||= ${$self->{"blog"};}
$data->{"blog_name"} ${$self->{"blog"};}
Truth table0%0/3
A ||= B
A= $data->{"blog_name"}B= ${$self->{"blog"};}
ABresult
1X1
011
000
247  
248 0 ▶ if (! $tb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tb) 0 0 0
249 0 require Net::TrackBack;
250 0 $tb = Net::TrackBack->new();
251 }
252  
253 0 $tb->send_ping($data);
254  
255 0 ▶ if (! $tb->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tb->is_success) 0 0 0
256 0 warn $tb->message();
257 0 return 0;
258 }
259  
260 0 return 1;
261 }
262  
263 =head1 Net::ITE::Post
264  
265 =cut
266  
267 package Net::ITE::Post;
268  
269 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
270 0 my $pkg = shift;
271 0 return bless $_[0],$pkg;
272 }
273  
274 =head2 $post->title()
275  
276 =cut
277  
278 0 ▶sub title { return shift->{title} }
Subroutine title: not called CC=1 SCAR=6.9
279  
280 =head2 $post->url()
281  
282 =cut
283  
284 0 ▶sub url { return shift->{link}; }
Subroutine url: not called CC=1 SCAR=6.9
285  
286 =head2 $post->excerpt()
287  
288 =cut
289  
290 0 ▶sub excerpt { return shift->{description}; }
Subroutine excerpt: not called CC=1 SCAR=6.9
291  
292 =head1 Net::ITE::Iterator
293  
294 =cut
295  
296 package Net::ITE::Iterator;
297  
298 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
299 0 my $pkg = shift;
300 0 return bless {pkg=>$_[0],data=>$_[1],count=>0}, $pkg;
301 }
302  
303 =head2 $it->count()
304  
305 =cut
306  
307 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
308 0 my $self = shift;
309 0 return scalar @{$self->{data}};
310 }
311  
312 =head2 $it->next()
313  
314 Returns an object.Woot!
315  
316 =cut
317  
318 0 ▶sub next {
Subroutine next: not called CC=2 SCAR=17.9
319 0 my $self = shift;
320 0 ▶ if (my $data = $self->{data}->[$self->{count}++]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->{"data"}[$self->{"count"}++]) 0 0 0
321 0 return $self->{pkg}->new($data);
322 }
323 }
324  
325 package Net::ITE::Network;
326 my $ua = undef;
327  
328 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
329  
330 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
331 0 my $pkg = shift;
332 0 return &send_request(HTTP::Request->new(GET=>$_[0]));
333 }
334  
335 0 ▶sub post {
Subroutine post: not called CC=2 SCAR=17.9
336 0 my $pkg = shift;
337 0 my $uri = shift;
338 0 my $args = shift;
339  
340 0 my @params = ();
341  
342 0 foreach my $param (keys %$args) {
343 0 my $value = $args->{$param};
344 0 $value =~ s!([^a-zA-Z0-9_.-])!uc sprintf "%%%02x", ord($1)!eg;
345 0 push @params,"$param=$value";
346 }
347  
348 0 my $req = HTTP::Request->new(POST =>$uri);
349 0 $req->content_type('application/x-www-form-urlencoded');
350 0 $req->content(join('&',@params));
351  
352 0 return &send_request($req);
353 }
354  
355 0 ▶sub ua {
Subroutine ua: not called CC=2 SCAR=17.9
356 0 ▶ if (! $ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua) 0 0 0
357 0 require LWP::UserAgent;
358 0 $ua = LWP::UserAgent->new();
359 }
360  
361 0 return $ua;
362 }
363  
364 0 ▶sub send_request {
Subroutine send_request: not called CC=2 SCAR=17.9
365 0 my $res = &ua()->request($_[0]);
366  
367 0 ▶ if (! $res->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
368 0 warn "Failed to retrieve data, ".$res->message()."\n";
369 0 return undef;
370 }
371  
372 0 return $res->content_ref();
373 }
374  
375 package Parser;
376 0 ▶use XML::RSS;
Subroutine BEGIN: called CC=1 SCAR=2.6
377  
378 my $rss = undef;
379  
380 sub parse_feed {
381 my $pkg = shift;
382 my $sr_rss = shift;
383  
384 $rss ||= XML::RSS->new();
385  
386 eval { $rss->parse($$sr_rss); };
387 if ($@) {
388 warn $@,"\n";
389 return undef;
390 }
391  
392 return $rss->{items};
393 }
394  
395 package Net::ITE::Constants;
396  
397 use constant ITE => "http://topicexchange.com";
398 use constant TOPICS => ITE."/topics/";
399 use constant TOPIC => ITE."/t/";
400 use constant NEWTOPIC => ITE."/new";
401  
402 return 1;
403  
404 =head1 VERSION
405  
406 0.05
407  
408 =head1 DATE
409  
410 $Date: 2003/03/20 05:09:14 $
411  
412 =head1 AUTHOR
413  
414 Aaron Straup Cope
415  
416 =head1 SEE ALSO
417  
418 http://topicexchange.com
419  
420 =head1 LICENSE
421  
422 Copyright (c) 2003 Aaron Straup Cope, All Rights Reserved.
423  
424 This is free software, you may use it and distribute it under the same
425 terms as Perl itself.
426  
427 =cut
428  
↑ Summary