2ch/Subject.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 95 CC 6 SCAR 24.2
CC
6
Cov
0%
load
30.0
threads
6.0
new
2.0
CRAP 11.2
1 package WWW::2ch::Subject;
2 use strict;
3  
4 use base qw( Class::Accessor::Fast );
5 __PACKAGE__->mk_accessors( qw( c url title noname image) );
6  
7 use HTTP::Date;
8  
9 use WWW::2ch::Dat;
10  
11 sub new {
12 my $class = shift;
13 my $c = shift;
14 my $url = shift;
15  
16 my $self = bless {
17 c => $c,
18 url => $url,
19 threads => [],
20 thread_by_key => {},
21 }, $class;
22  
23 $self->title($c->setting->{title});
24 $self->noname($c->setting->{noname});
25 $self->image($c->setting->{image});
26  
27 $self;
28 }
29  
30 sub load {
31 my ($self) = @_;
32  
33 $self->{threads} = [];
34 $self->{thread_by_key} = {};
35 return 0 unless $self->c && $self->url;
36  
37 my $cache = $self->c->cache->get($self->file);
38 my $time = $cache->{time} || 0;
39 my $res = $self->c->ua->diff_request($self->url, time => $time);
40  
41 my $data;
42 if (!$res->is_success) {
43 return 0 unless $res->code eq '304';
44 $data = $cache->{data};
45 } elsif ($res->content eq $cache->{data}) {
46 $data = $cache->{data};
47 } else {
48 my $lasttime = HTTP::Date::str2time($res->header('Last-Modified'));
49 $self->c->cache->set($self->file, {
50 data => $res->content,
51 time => $lasttime,
52 fetch_time => time,
53 url => $self->url,
54 title => $self->title,
55 noname => $self->noname,
56 image => $self->image,
57 });
58 $data = $res->content;
59 }
60 my $subject = $self->c->worker->parse_subject($data);
61 foreach (@{ $subject }) {
62 $_->{url} = $self->url;
63 $_->{bbstitle} = $self->title;
64 $_->{noname} = $self->noname;
65 $_->{image} = $self->image;
66 $self->add_thread( WWW::2ch::Dat->new($self->c, $_) );
67 }
68 return 1;
69 }
70  
71 sub add_thread {
72 my($self, $dat) = @_;
73 push @{ $self->{threads} }, $dat;
74 $self->{thread_by_key}->{$dat->key} = $dat;
75 }
76  
77 sub threads {
78 my $self = shift;
79 wantarray ? @{ $self->{threads} } : $self->{threads};
80 }
81  
82 sub thread {
83 my ($self, $key) = @_;
84 $self->{thread_by_key}->{$key};
85 }
86  
87 sub file {
88 my ($self) = @_;
89 $self->c->conf->{local_path} . 'subject.txt';
90 }
91  
92 sub permalink {
93 my ($self) = @_;
94 $self->c->worker->permalink;
95 }
96  
97 1;
98 __END__