Journal/Entry.pm

filter: Statementstmt 97.5% 80 / 82 Branchbran 45.4% 10 / 22 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 94.7% 18 / 19 Podpod 100% 5 / 5 totaltotal 79.7% 114 / 143 CC 16 SCAR 21.3
CC
16
Cov
81%
_get_content
20.9
new
4.0
content
2.1
CRAP 8.5
« Journal.pm ↑ Summary
1 package WWW::UsePerl::Journal::Entry;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 $VERSION = '0.25';
8  
9 #----------------------------------------------------------------------------
10  
11 =head1 NAME
12  
13 WWW::UsePerl::Journal::Entry - use.perl.org journal entry
14  
15 =head1 DESCRIPTION
16  
17 Do not use directly. See L<WWW::UsePerl::Journal> for details of usage.
18  
19 =cut
20  
21 # -------------------------------------
22 # Library Modules
23  
24 12 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 12 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 12 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 12 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 12 ▶use Time::Seconds;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 12 ▶use WWW::UsePerl::Journal;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 #----------------------------------------------------------------------------
35 # Accessors
36  
37 =head2 The Accessors
38  
39 The following accessor methods are available:
40  
41 date
42 subject
43 author
44 uid
45 content
46  
47 All functions can be called to return the current value of the associated
48 object variable.
49  
50 =cut
51  
52 12 __PACKAGE__->mk_accessors($_) for qw(date subject author eid);
53  
54 # -------------------------------------
55 # Constants & Variables
56  
57 12 my $UP_URL = 'http://use.perl.org/use.perl.org';
58 12 ▶use overload q{""} => sub { $_[0]->stringify() };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
59  
60 12 my $UID = '
61 <div \s+ class="title" \s+ id="user-info-title"> \s+
62 <h4> \s+ (.*?) \s+ \((\d+)\) \s+ </h4> \s+ </div>
63 ';
64  
65 12 my %mons = (
66 1 => 'January',
67 2 => 'February',
68 3 => 'March',
69 4 => 'April',
70 5 => 'May',
71 6 => 'June',
72 7 => 'July',
73 8 => 'August',
74 9 => 'September',
75 10 => 'October',
76 11 => 'November',
77 12 => 'December',
78 );
79  
80 # -------------------------------------
81 # The Public Interface
82  
83 =head1 INTERFACE
84  
85 =head2 Constructor
86  
87 =over 4
88  
89 =item * new
90  
91 use WWW::UsePerl::Journal::Entry;
92 my $j = WWW::UsePerl::Journal::Entry->new(%hash);
93  
94 Creates an instance for a specific entry. The hash must contain values for
95 the keys 'j' (journal object), 'author' (entry author) and 'eid' (entry id).
96  
97 =back
98  
99 =cut
100  
101 sub new {
102 6 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
103 6 my %opts = (@_);
104  
105 6 for(qw/j author eid/) {
106 17 ▶ return unless($opts{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{$_} 1 16 17
107 }
108  
109 die "No parent object"
110 5 ▶ unless $opts{j}->isa('WWW::UsePerl::Journal');
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"j"}->isa("WWW::UsePerl::Journal") 0 5 5
111  
112 #use Data::Dumper;
113 #print STDERR "\n#self->new: ".Dumper(\%opts);
114  
115 5 my $self = bless {%opts}, $class;
116 5 return $self;
117 }
118  
119 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
120  
121 =head2 Methods
122  
123 =over 4
124  
125 =item * stringify
126  
127 use WWW::UsePerl::Journal::Entry;
128 my $j = WWW::UsePerl::Journal::Entry->new(%hash);
129 print "$j";
130  
131 Returns the content of the journal entry when the object is directly referenced
132 in a string.
133  
134 =cut
135  
136 sub stringify {
137 2 ▶ my $self = shift;
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
138 2 $self->content();
139 }
140  
141 =item * eid
142  
143 Returns the entry id for the current journal entry.
144  
145 =cut
146  
147 sub eid {
148 2 ▶ my $self = shift;
Subroutine eid: called CC=1 SCAR=0.0
Pod documented
149 2 return $self->{eid};
150 }
151  
152 =item * content
153  
154 Return the content of an journal entry.
155  
156 =cut
157  
158 sub content {
159 4 ▶ my $self = shift;
Subroutine content: called CC=2 SCAR=7.6
Pod documented
160 4 ▶ $self->{content} ||= do { $self->_get_content };
Condition33%1/3
$self->{"content"} ||= do { $self->_get_content }
l!l&&r!l&&!r
004
MC/DC0%0/2
$self->{"content"} ||= do { $self->_get_content }
$self->{"content"} do { $self->_get_content }
Truth table33%1/3
A ||= B
A= $self->{"content"}B= do { $self->_get_content }
ABresult
1X1
011
000
161 }
162  
163 =item * raw
164  
165 For debugging purposes.
166  
167 =back
168  
169 =cut
170  
171 sub raw {
172 1 ▶ my $self = shift;
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
173 1 my $eid = $self->{eid};
174 1 my $author = $self->{author};
175 #print STDERR "\n#raw: URL=[". $UP_URL . "/_$author/journal/$eid.html]";
176 1 return $self->{j}->{ua}->request(GET $UP_URL . "/_$author/journal/$eid.html")->content;
177 }
178  
179 # -------------------------------------
180 # The Private Subs
181  
182 # name: _get_content
183 # args: self .... the current object
184 # retv: content text
185 # desc: Given a uid and journal entry id, will retrieve a specific journal
186 # entry and disassemble into component parts. returns the content text
187  
188 sub _get_content {
189 5 ▶ my $self = shift;
Subroutine _get_content: called CC=12 SCAR=30.4
190 5 my $eid = $self->{eid};
191 5 my $author = $self->{author};
192 5 my $content;
193  
194 5 eval {
195 5 $content = $self->{j}->{ua}->request(GET $UP_URL . "/_$author/journal/$eid.html")->content;
196 };
197  
198 #print STDERR "\n#eval=[$@]\n";
199  
200 5 ▶ return $self->{j}->error("error getting entry") if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
201  
202 #print STDERR "\n#e->_get_content: URL=[". $UP_URL . "/_$author/journal/$eid.html]";
203 #print STDERR "\n#content=[$content]\n";
204  
205 5 ▶ return $self->{j}->error("error getting entry") unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 5 5
206 5 ▶ return $self->{j}->error("error getting entry") if($content =~ m!<b>Error type:</b>\s+\d+!);
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ m[<b>Error type:</b>\s+\d+] 0 5 5
207  
208 5 ▶ return $self->{j}->error("$eid does not exist")
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ m[Sorry, there are no journal entries found for this user.</TD></TR></TABLE><P>]is 0 5 5
209 if $content =~
210 m#Sorry, there are no journal entries
211 found for this user.</TD></TR></TABLE><P>#is;
212 5 ▶ return $self->{j}->error("$eid does not exist")
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /Sorry, the requested journal entries were not found./is 0 5 5
213 if $content =~ m!Sorry, the requested journal entries were not found.!is;
214  
215 5 ($author,$self->{uid}) = $content =~ m!$UID!six;
216 #print STDERR "\n#e->_get_content: UID=[". ($self->{uid}) ."]";
217  
218 5 ($self->{subject}) = $content =~ m!
219 <div \s+ id="journalslashdot"> .*?
220 <div \s+ class="title"> \s+
221 <h3> \s* (.*?) \s* </h3>
222 !six;
223  
224 # date/time fields
225 5 my ($month, $day, $year, $hr, $mi, $amp) = $content =~ m!
226 <div \s+ class="journaldate">\w+ \s+ (\w+) \s+ (\d+), \s+ (\d+)</div> .*?
227 <div \s+ class="details">(\d+):(\d+) \s+ ([AP]M)</div>
228 !six;
229  
230 5 ▶ unless($day) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($day) 5 0 5
231 5 (undef,$mi,$hr,$day,$month,$year) = localtime(time());
232 5 $month = $mons{$month};
233 }
234  
235 # just in case we can't get the time
236 5 ▶ if($amp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amp) 0 5 5
237 0 ▶ $hr += 12 if($hr > 12 && $amp eq 'PM');
Branch: 1 branch
BranchTrueFalseTotal
if $hr > 12 and $amp eq "PM" 0 0 0
Condition0%0/3
$hr > 12 and $amp eq "PM"
!ll&&!rl&&r
000
MC/DC0%0/2
$hr > 12 and $amp eq "PM"
$hr > 12 $amp eq "PM"
Truth table0%0/3
A and B
A= $hr > 12B= $amp eq "PM"
ABresult
0X0
100
111
238 0 ▶ $hr = 0 if($hr == 12 && $amp eq 'AM');
Branch: 1 branch
BranchTrueFalseTotal
if $hr == 12 and $amp eq "AM" 0 0 0
Condition0%0/3
$hr == 12 and $amp eq "AM"
!ll&&!rl&&r
000
MC/DC0%0/2
$hr == 12 and $amp eq "AM"
$hr == 12 $amp eq "AM"
Truth table0%0/3
A and B
A= $hr == 12B= $amp eq "AM"
ABresult
0X0
100
111
239 }
240  
241 # sometimes Time::Piece can't parse the date :(
242 5 eval {
243 5 $self->{date} = Time::Piece->strptime(
244 "$month $day $year ${hr}:$mi",
245 '%B %d %Y %H:%M'
246 );
247 };
248  
249 #$self->{date} += 4*ONE_HOUR; # correct TZ?
250  
251 5 $content =~ m! <div \s+ class="intro">\s*(.*?)\s*</div> !six;
252 5 return $1;
253 }
254  
255 12 1;
256  
257 __END__
« Journal.pm ↑ Summary