| blib/lib/WWW/UsePerl/Journal/Entry.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 74 | 77 | 96.1 |
| branch | 14 | 22 | 63.6 |
| condition | 4 | 9 | 44.4 |
| subroutine | 18 | 19 | 94.7 |
| pod | 5 | 5 | 100.0 |
| total | 115 | 132 | 87.1 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package WWW::UsePerl::Journal::Entry; | ||||||
| 2 | |||||||
| 3 | 12 | 12 | 71 | use strict; | |||
| 12 | 29 | ||||||
| 12 | 495 | ||||||
| 4 | 12 | 12 | 67 | use warnings; | |||
| 12 | 23 | ||||||
| 12 | 547 | ||||||
| 5 | |||||||
| 6 | 12 | 12 | 4466 | use vars qw($VERSION); | |||
| 12 | 20 | ||||||
| 12 | 958 | ||||||
| 7 | $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 |
||||||
| 18 | |||||||
| 19 | =cut | ||||||
| 20 | |||||||
| 21 | # ------------------------------------- | ||||||
| 22 | # Library Modules | ||||||
| 23 | |||||||
| 24 | 12 | 12 | 74 | use base qw(Class::Accessor::Fast); | |||
| 12 | 23 | ||||||
| 12 | 48843 | ||||||
| 25 | |||||||
| 26 | 12 | 12 | 75606 | use HTTP::Cookies; | |||
| 12 | 32 | ||||||
| 12 | 318 | ||||||
| 27 | 12 | 12 | 325 | use HTTP::Request::Common; | |||
| 12 | 24 | ||||||
| 12 | 1409 | ||||||
| 28 | 12 | 12 | 75 | use LWP::UserAgent; | |||
| 12 | 22 | ||||||
| 12 | 290 | ||||||
| 29 | 12 | 12 | 67 | use Time::Piece; | |||
| 12 | 23 | ||||||
| 12 | 118 | ||||||
| 30 | 12 | 12 | 1010 | use Time::Seconds; | |||
| 12 | 294 | ||||||
| 12 | 1082 | ||||||
| 31 | |||||||
| 32 | 12 | 12 | 69 | use WWW::UsePerl::Journal; | |||
| 12 | 24 | ||||||
| 12 | 1109 | ||||||
| 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 | __PACKAGE__->mk_accessors($_) for qw(date subject author eid); | ||||||
| 53 | |||||||
| 54 | # ------------------------------------- | ||||||
| 55 | # Constants & Variables | ||||||
| 56 | |||||||
| 57 | my $UP_URL = 'http://use.perl.org/use.perl.org'; | ||||||
| 58 | 12 | 12 | 70 | use overload q{""} => sub { $_[0]->stringify() }; | |||
| 12 | 2 | 18 | |||||
| 12 | 138 | ||||||
| 2 | 782 | ||||||
| 59 | |||||||
| 60 | my $UID = ' | ||||||
| 61 | \s+ |
||||||
| 62 | \s+ (.*?) \s+ \((\d+)\) \s+\s+ |
||||||
| 63 | '; | ||||||
| 64 | |||||||
| 65 | 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 | 295 | 295 | 1 | 2755 | my $class = shift; | ||
| 103 | 295 | 1126 | my %opts = (@_); | ||||
| 104 | |||||||
| 105 | 295 | 512 | for(qw/j author eid/) { | ||||
| 106 | 882 | 100 | 2263 | return unless($opts{$_}); | |||
| 107 | } | ||||||
| 108 | |||||||
| 109 | 293 | 100 | 1128 | die "No parent object" | |||
| 110 | unless $opts{j}->isa('WWW::UsePerl::Journal'); | ||||||
| 111 | |||||||
| 112 | #use Data::Dumper; | ||||||
| 113 | #print STDERR "\n#self->new: ".Dumper(\%opts); | ||||||
| 114 | |||||||
| 115 | 292 | 2177 | my $self = bless {%opts}, $class; | ||||
| 116 | 292 | 3692 | return $self; | ||||
| 117 | } | ||||||
| 118 | |||||||
| 119 | 0 | 0 | 0 | sub DESTROY {} | |||
| 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 | 2 | 1 | 6 | my $self = shift; | ||
| 138 | 2 | 10 | $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 | 3 | 3 | 1 | 1655 | my $self = shift; | ||
| 149 | 3 | 17 | 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 | 6 | 6 | 1 | 1523 | my $self = shift; | ||
| 160 | 6 | 66 | 52 | $self->{content} ||= do { $self->_get_content }; | |||
| 5 | 21 | ||||||
| 161 | } | ||||||
| 162 | |||||||
| 163 | =item * raw | ||||||
| 164 | |||||||
| 165 | For debugging purposes. | ||||||
| 166 | |||||||
| 167 | =back | ||||||
| 168 | |||||||
| 169 | =cut | ||||||
| 170 | |||||||
| 171 | sub raw { | ||||||
| 172 | 1 | 1 | 1 | 2 | my $self = shift; | ||
| 173 | 1 | 100 | my $eid = $self->{eid}; | ||||
| 174 | 1 | 3 | my $author = $self->{author}; | ||||
| 175 | #print STDERR "\n#raw: URL=[". $UP_URL . "/_$author/journal/$eid.html]"; | ||||||
| 176 | 1 | 10 | 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 | 6 | 6 | 22 | my $self = shift; | |||
| 190 | 6 | 173 | my $eid = $self->{eid}; | ||||
| 191 | 6 | 16 | my $author = $self->{author}; | ||||
| 192 | 6 | 10 | my $content; | ||||
| 193 | |||||||
| 194 | 6 | 16 | eval { | ||||
| 195 | 6 | 64 | $content = $self->{j}->{ua}->request(GET $UP_URL . "/_$author/journal/$eid.html")->content; | ||||
| 196 | }; | ||||||
| 197 | |||||||
| 198 | #print STDERR "\n#eval=[$@]\n"; | ||||||
| 199 | |||||||
| 200 | 6 | 50 | 1591012 | return $self->{j}->error("error getting entry") if($@); | |||
| 201 | |||||||
| 202 | #print STDERR "\n#e->_get_content: URL=[". $UP_URL . "/_$author/journal/$eid.html]"; | ||||||
| 203 | #print STDERR "\n#content=[$content]\n"; | ||||||
| 204 | |||||||
| 205 | 6 | 50 | 32 | return $self->{j}->error("error getting entry") unless $content; | |||
| 206 | 6 | 100 | 85 | return $self->{j}->error("error getting entry") if($content =~ m!Error type:\s+\d+!); | |||
| 207 | |||||||
| 208 | 5 | 50 | 609 | return $self->{j}->error("$eid does not exist") | |||
| 209 | if $content =~ | ||||||
| 210 | m#Sorry, there are no journal entries | ||||||
| 211 | found for this user. |
#is;