Schedule/Post.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Schedule.pm ↑ Summary Schedule/Post/Simple.pm »
1 package LJ::Schedule::Post;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 LJ::Schedule::Post - The 'abstract base' for LJ::Schedule posting components
9  
10 =head1 VERSION
11  
12 Version 0.6
13  
14 =cut
15  
16 our $VERSION = '0.6';
17  
18 =head1 SYNOPSIS
19  
20 This module is used internally by LJ::Schedule, and shouldn't need to be
21 used directly.
22  
23 It is an 'abstract' module, ie it is expected to be subclassed to provide
24 different posting functionalities. Currently the only one supported is
25 LJ::Simple
26  
27 =head1 AUTHOR
28  
29 Ben Evans, C<< <ben at bpfh.net> >>
30  
31 =head1 BUGS
32  
33 Please report any bugs or feature requests to
34 C<bug-lj-schedule-post at rt.cpan.org>, or through the web interface at
35 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LJ-Schedule>.
36 I will be notified, and then you'll automatically be notified of progress on
37 your bug as I make changes.
38  
39 =head1 SUPPORT
40  
41 You can find documentation for this module with the perldoc command.
42  
43 perldoc LJ::Schedule
44  
45 You can also look for information at:
46  
47 =over 4
48  
49 =item * AnnoCPAN: Annotated CPAN documentation
50  
51 L<http://annocpan.org/dist/LJ-Schedule>
52  
53 =item * CPAN Ratings
54  
55 L<http://cpanratings.perl.org/d/LJ-Schedule>
56  
57 =item * RT: CPAN's request tracker
58  
59 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LJ-Schedule>
60  
61 =item * Search CPAN
62  
63 L<http://search.cpan.org/dist/LJ-Schedule>
64  
65 =back
66  
67 =head1 ACKNOWLEDGEMENTS
68  
69 =head1 COPYRIGHT & LICENSE
70  
71 Copyright 2006 Ben Evans, all rights reserved.
72  
73 This program is free software; you can redistribute it and/or modify it
74 under the same terms as Perl itself.
75  
76 =cut
77  
78 0 ▶ use LJ::Schedule::Post::Simple;
Subroutine BEGIN: called CC=1 SCAR=2.6
79  
80 use Config::Tiny;
81 use Date::Format;
82  
83 # TMP
84 use Data::Dumper;
85  
86 our $SECS_IN_DAY = 60 * 60 * 24;
87 our $SECS_IN_WEEK = 60 * 60 * 24 * 7;
88 our @DAY_NAMES = qw(Sun Mon Tue Wed Thu Fri Sat);
89  
90 # Names of packages to dispatch to.
91 # Essentially plumbing for if we need to add additional
92 # posting modules
93 my $rh_dispatch = {
94 simple => 'LJ::Schedule::Post::Simple',
95 };
96  
97 #
98 # Standard constructor
99 #
100 sub new {
101 my ($pkg, $params) = @_;
102 my $rh_p = {};
103  
104 $rh_p = $params if (ref($params) eq 'HASH');
105 my $class = $rh_dispatch->{simple};
106  
107 if ($rh_p->{post}) {
108 $class ||= $rh_dispatch->{$rh_p->{post}};
109 }
110  
111 my $self = $class->new($rh_p);
112  
113 # Setup the config file if it hasn't been done already
114 LJ::Schedule::get_config() unless defined $LJ::Schedule::CONFIG;
115  
116 return $self;
117 }
118  
119 #
120 # Alias an event (eg add lj-user tags) for a post being built up.
121 #
122 sub do_summary_aliasing {
123 my $self = shift;
124 my $raw = shift;
125  
126 my $out = $raw;
127  
128 my @keys = keys(%$LJ::Schedule::ALIAS);
129 foreach (@keys) {
130 $out =~ s/^$_(\W+)/<lj user="$LJ::Schedule::ALIAS->{$_}">$1 /gi;
131 $out =~ s/\s+$_(\W+)/ <lj user="$LJ::Schedule::ALIAS->{$_}">$1/gi;
132 $out =~ s/\s+$_$/ <lj user="$LJ::Schedule::ALIAS->{$_}">/gi;
133 }
134  
135 return $out;
136 }
137  
138 #
139 # Format an event for adding to a post being built up
140 #
141 sub lj_add_event {
142 my $self = shift;
143 my $evt = shift;
144  
145 my @j = localtime($evt->{tval});
146 my $wday = $j[6];
147  
148 my $summary = $self->do_summary_aliasing($evt->{summary});
149  
150 return $DAY_NAMES[$wday]. ' '. $evt->{date}.' '. $summary. " <br>\n";
151 }
152  
153 #
154 # Helper method
155 #
156 sub lj_add_week_break {
157 my $self = shift;
158 my $evt = shift;
159  
160 return "\n<br>\n";
161 }
162  
163 #
164 # Main post building method
165 #
166 sub output_cal_for_lj {
167 my $self = shift;
168 my $cal = shift;
169  
170 my $ra_evts = $cal->evts();
171  
172 my $out = "<lj-cut text='Schedule'>\n";
173  
174 # This gets the day of week. 0 is Sunday, 1 is monday, etc.
175 my @junk = localtime(time());
176 my $today_wday = $junk[6];
177  
178 # Set up the first event's output
179 my $last_evt = shift(@$ra_evts);
180 $out .= $self->lj_add_event($last_evt);
181  
182 foreach my $evt (@$ra_evts) {
183 if ($evt->{tval} - $last_evt->{tval} >= $SECS_IN_WEEK) {
184 $out .= lj_add_week_break();
185 } else {
186 my @j = localtime($last_evt->{tval});
187 my $lwday = $j[6];
188  
189 @j = localtime($evt->{tval});
190 my $wday = $j[6];
191  
192 if ( ($wday < $lwday) && ($wday >= 0) ) {
193 $out .= $self->lj_add_week_break();
194 }
195 }
196  
197 $out .= $self->lj_add_event($evt);
198 $last_evt = $evt;
199 }
200  
201 $out .= "</lj-cut>\n";
202  
203 return $out;
204 }
205  
206  
207  
208  
209 1; # End of LJ::Schedule::Post
« Schedule.pm ↑ Summary Schedule/Post/Simple.pm »