Event.pm

filter: Statementstmt 99.1% 117 / 118 Branchbran 94.6% 53 / 56 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 26 / 26 Podpod 100% 20 / 20 totaltotal 98.2% 226 / 230 CC 35 SCAR 14.1
CC
35
Cov
98%
to_string
11.0
who
5.0
what
5.0
CRAP 4.1
« Bot.pm ↑ Summary
1 package WWW::Wikevent::Event;
2 #
3 # Copyright 2007 Mark Jaroski
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of either:
7 #
8 # a) the GNU General Public License as published by the Free Software
9 # Foundation; either version 3, or (at your option) any later version,
10 # or
11 # b) the "Artistic License" which comes with this Kit.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
16 # GNU General Public License or the Artistic License for more details.
17 #
18 # You should have received a copy of the Artistic License with this Kit,
19 # in the file named "Artistic".
20 #
21 # You should also have received a copy of the GNU General Public License
22 # along with this program in the file named "Copying". If not, write to
23 # the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
24 # Boston, MA 02111-1307, USA or visit their web page on the internet at
25 # http://www.gnu.org/copyleft/gpl.html.
26  
27 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use overload q{""} => 'to_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Date::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 =head1 NAME
35  
36 WWW::Wikevent::Event
37  
38 =cut
39  
40 =head1 SYNOPSIS
41  
42 use WWW::Wikevent::Event;
43  
44 my $event = WWW::Wikevent::Event->new();
45  
46 but more usually you will get an event object from a Wikevent bot:
47  
48 my $bot = WWW::Wikevent::Bot->new();
49 my $event = $bot->add_event();
50  
51 Then use accessor methods to set event data:
52  
53 $event->name( 'Hideout Block Party' );
54 $event->price( '$10' );
55 $event->date( '2007-09-09' );
56 $event->locality( 'Chicago' );
57 $event->venue( 'The Hideout' );
58  
59 etcetera. Then:
60  
61 print $event;
62  
63 which will print the event out as wikitext.
64  
65 =cut
66  
67 =head1 DESCRIPTION
68  
69 WWW::Wikevent::Event is a package which will help you write scraper scripts
70 for gathering events from venue and artist websites and for inclusion in
71 the Free content events compendium, Wikevent.
72  
73 The module takes care of building up an event tag for Wikevent, so you can
74 get busy with the fun work of scraping a venue's web pages for the
75 data.
76  
77 =cut
78  
79 =head1 CONSTANTS
80  
81 =over
82  
83 =item $REQ_WARNING;
84  
85 The warning given if you print an event which is missing required
86 attributes. In fact there really aren't any truly required attributes, but
87 these are needed to correctly place an event on the Wikevent site.
88  
89 =back
90  
91 =cut
92  
93 2 my $REQ_WARNING = "The following attributes are missing:\n";
94  
95 =head1 CONSTRUCTORS
96  
97 =cut
98  
99 =head2 new
100  
101 my $event = WWW::Wikevent::Event->new();
102  
103 Creates and returns a new event object.
104  
105 =cut
106  
107 15 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
108 15 my $pkg = shift;
109 15 my $self = bless {}, $pkg;
110 15 $self->{'who'} = [];
111 15 $self->{'what'} = [];
112 15 return $self;
113 }
114  
115 =head1 ACCESSORS
116  
117 =cut
118  
119 =head2 name
120  
121 $event->name( $name );
122 my $name = $event->name();
123  
124 The name of the event.
125  
126 =cut
127  
128 23 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
129 23 my ( $self, $name ) = @_;
130 23 ▶ if ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 14 9 23
131 14 $self->{'name'} = $name;
132 14 $self->{'name'} =~ s{(\w+)}{\u\L$1}g;
133 }
134 23 return $self->{'name'};
135 }
136  
137 =head2 date
138
139 $event->date( $date_string );
140 my $date_string = $event->date();
141  
142 The date of the event.
143  
144 While Wikevent will accept and try to work with a number of date formats,
145 in practice the very best results will be achieved by using the a format
146 like '2007-09-20'.
147  
148 =cut
149  
150 2 ▶sub date {
Subroutine date: called CC=2 SCAR=6.9
Pod documented
151 2 my ( $self, $date ) = @_;
152 2 ▶ $self->{'date'} = $date if $date;
Branch: 1 branch
BranchTrueFalseTotal
if $date 1 1 2
153 2 return $self->{'date'};
154 }
155  
156 =head2 time
157  
158 $event->time( $time_string );
159 my $time_string = $event->time();
160  
161 The start time of the event.
162  
163 Wikevent accepts a fairly wide range of formats for the time fields. You
164 can use am/pm times like this: "9pm", "9:15pm", or if you prefer 24 hour
165 times like this: "15:30" or even the French style "15h30".
166  
167 =cut
168  
169 2 ▶sub time {
Subroutine time: called CC=2 SCAR=6.9
Pod documented
170 2 my ( $self, $time ) = @_;
171 2 ▶ $self->{'time'} = $time if $time;
Branch: 1 branch
BranchTrueFalseTotal
if $time 1 1 2
172 2 return $self->{'time'};
173 }
174  
175 =head2 endtime
176  
177 $event->endtime( $time_string );
178 my $time_string = $event->endtime();
179
180 The time at which your event ends.
181  
182 See C<time> for details.
183  
184 =cut
185  
186 2 ▶sub endtime {
Subroutine endtime: called CC=2 SCAR=6.9
Pod documented
187 2 my ( $self, $endtime ) = @_;
188 2 ▶ $self->{'endtime'} = $endtime if $endtime;
Branch: 1 branch
BranchTrueFalseTotal
if $endtime 1 1 2
189 2 return $self->{'endtime'};
190 }
191  
192 =head2 duration
193  
194 $event->duration( $duration_string );
195 my $duration_string = $event->duration();
196  
197 The duration of the event.
198  
199 An alternative to setting the endtime, this field accepts pretty much the same
200 format as the time fields.
201  
202 =cut
203  
204 4 ▶sub duration {
Subroutine duration: called CC=2 SCAR=6.9
Pod documented
205 4 my ( $self, $duration ) = @_;
206 4 ▶ $self->{'duration'} = $duration if $duration;
Branch: 1 branch
BranchTrueFalseTotal
if $duration 2 2 4
207 4 return $self->{'duration'};
208 }
209  
210 =head2 price
211  
212 $event->price( $price_string );
213 my $price_string = $event->price();
214  
215 The price of attending, and some short info.
216  
217 This is a free text string, but should be used sparingly to report ticket
218 and door prices.
219  
220 =cut
221  
222 2 ▶sub price {
Subroutine price: called CC=2 SCAR=6.9
Pod documented
223 2 my ( $self, $price ) = @_;
224 2 ▶ $self->{'price'} = $price if $price;
Branch: 1 branch
BranchTrueFalseTotal
if $price 1 1 2
225 2 return $self->{'price'};
226 }
227  
228 =head2 tickets
229  
230 $event->tickets( $tickets_url );
231 my $tickets_url = $event->tickets();
232  
233 A URL which points to the venue's e-commerce page, if there is one.
234  
235 This field must be a URL or it won't work. Please pay attention to any
236 rules that the venue site might have about "deep" linking, and do make sure
237 that you only link to the venue site, or it's designated agent, NEVER to
238 some 3rd party.
239  
240 =cut
241  
242 2 ▶sub tickets {
Subroutine tickets: called CC=2 SCAR=6.9
Pod documented
243 2 my ( $self, $tickets ) = @_;
244 2 ▶ $self->{'tickets'} = $tickets if $tickets;
Branch: 1 branch
BranchTrueFalseTotal
if $tickets 1 1 2
245 2 return $self->{'tickets'};
246 }
247  
248 =head2 restrictions
249  
250 $event->restrictions( $restrictions );
251 my $restrictions = $event->restrictions();
252  
253 Any restrictions placed on attendance.
254  
255 In many jursidtictions there are limits on who can attend events at which
256 alcohol is being sold, for instance. This field is for recording those
257 rules, examples might be "21 and over", or "18 and over", or "All Ages".
258  
259 =cut
260  
261 2 ▶sub restrictions {
Subroutine restrictions: called CC=2 SCAR=6.9
Pod documented
262 2 my ( $self, $restrictions ) = @_;
263 2 ▶ $self->{'restrictions'} = $restrictions if $restrictions;
Branch: 1 branch
BranchTrueFalseTotal
if $restrictions 1 1 2
264 2 return $self->{'restrictions'};
265 }
266  
267 =head2 lang
268  
269 $event->lang( $language_code );
270 my $language_code = $event->lang();
271  
272 A comma seperated list of two letter language codes for languages which
273 will be used on stage at the event.
274  
275 =cut
276  
277 2 ▶sub lang {
Subroutine lang: called CC=2 SCAR=6.9
Pod documented
278 2 my ( $self, $lang ) = @_;
279 2 ▶ $self->{'lang'} = $lang if $lang;
Branch: 1 branch
BranchTrueFalseTotal
if $lang 1 1 2
280 2 return $self->{'lang'};
281 }
282  
283 =head2 locality
284  
285 $event->locality( $locality );
286 my $locality = $event->locality();
287  
288 The city, town, or village in which the event is taking place.
289  
290 =cut
291  
292 2 ▶sub locality {
Subroutine locality: called CC=2 SCAR=6.9
Pod documented
293 2 my ( $self, $locality ) = @_;
294 2 ▶ $self->{'locality'} = $locality if $locality;
Branch: 1 branch
BranchTrueFalseTotal
if $locality 1 1 2
295 2 return $self->{'locality'};
296 }
297  
298 =head2 venue
299  
300 $event->venue( $venue );
301 my $venue = $event->venue();
302  
303 The club, hall, auditorium, or street where the event is taking place.
304  
305 =cut
306  
307 2 ▶sub venue {
Subroutine venue: called CC=2 SCAR=6.9
Pod documented
308 2 my ( $self, $venue ) = @_;
309 2 ▶ $self->{'venue'} = $venue if $venue;
Branch: 1 branch
BranchTrueFalseTotal
if $venue 1 1 2
310 2 return $self->{'venue'};
311 }
312  
313 =head2 desc
314  
315 $event->desc( $wikitext );
316 my $wikitext = $event->desc();
317  
318 A discription of the event in Mediawiki wikitext.
319  
320 For a complete description of the Wikitext markup language please see the
321 L<http://mediawiki.org>.
322  
323 =cut
324  
325 47 ▶sub desc {
Subroutine desc: called CC=2 SCAR=6.9
Pod documented
326 47 my ( $self, $desc ) = @_;
327 47 ▶ $self->{'desc'} = $desc if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 1 46 47
328 47 return $self->{'desc'};
329 }
330  
331 =head2 who
332  
333 $event->who( @who );
334 $event->who( $who_ref );
335 my @who = $event->who();
336 my $who_ref = $event->who();
337  
338 An array, or array reference, of names of artists, etc. appearing at the
339 event.
340  
341 It's best to use this field and its related methods only if you can't
342 include the appropriate markup in the description wikitext itself. Don't
343 do both, since this list will be printed out as a wikitext unordered list
344 at the top of the event description.
345  
346 =cut
347  
348 52 ▶sub who {
Subroutine who: called CC=5 SCAR=16.1
Pod documented
349 52 my $self = shift;
350 52 ▶ if ( @_ && ref $_[0] eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ and ref $_[0] eq 'ARRAY') { } 1 51 52
elsif (@_) { } 1 50 51
Condition100%3/3
@_ and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
5011
MC/DC100%2/2
@_ and ref $_[0] eq "ARRAY"
@_ ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
351 1 $self->{'who'} = shift;
352 } elsif ( @_ ) {
353 1 $self->{'who'} = \@_;
354 }
355 52 ▶ return wantarray ? @{$self->{'who'}} : $self->{'who'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 47 5 52
356 }
357  
358 =head2 what
359  
360 $event->what( @what );
361 $event->what( $what_ref );
362 my @what = $event->what();
363 my $what_ref = $event->what();
364  
365 An array, or array reference, of names of cateogories to which this event
366 belongs.
367  
368 =cut
369  
370 52 ▶sub what {
Subroutine what: called CC=5 SCAR=16.1
Pod documented
371 52 my $self = shift;
372 52 ▶ if ( @_ && ref $_[0] eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ and ref $_[0] eq 'ARRAY') { } 1 51 52
elsif (@_) { } 1 50 51
Condition100%3/3
@_ and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
5011
MC/DC100%2/2
@_ and ref $_[0] eq "ARRAY"
@_ ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
373 1 $self->{'what'} = shift;
374 } elsif ( @_ ) {
375 1 $self->{'what'} = \@_;
376 }
377 52 ▶ return wantarray ? @{$self->{'what'}} : $self->{'what'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 47 5 52
378 }
379  
380 =head1 METHODS
381  
382 =cut
383  
384 =head2 add_who
385  
386 $event->add_who( $name );
387  
388 Add a single artist, organizer, etc. to the C<who> list.
389  
390 =cut
391  
392 1 ▶sub add_who {
Subroutine add_who: called CC=1 SCAR=0.0
Pod documented
393 1 my ( $self, $who ) = @_;
394 1 push @{$self->{'who'}}, $who;
395 1 return $self->who();
396 }
397  
398 =head2 add_what
399  
400 $event->add_what( $category );
401  
402 Add a single category to the C<what> list.
403  
404  
405 =cut
406  
407 1 ▶sub add_what {
Subroutine add_what: called CC=1 SCAR=0.0
Pod documented
408 1 my ( $self, $what ) = @_;
409 1 push @{$self->{'what'}}, $what;
410 1 return $self->what();
411 }
412  
413 =head2 who_string
414  
415 $who_tags = $this->who_string();
416  
417 Renders the C<who> list as a as tags for inclusion in the event
418 description.
419  
420 =cut
421  
422 46 ▶sub who_string {
Subroutine who_string: called CC=3 SCAR=11.0
Pod documented
423 46 my $self = shift;
424 46 my $string = '';
425 46 foreach my $who ( $self->who() ) {
426 16 $string .= "* <who>$who</who>\n";
427 }
428 46 ▶ return $string eq '' ? undef : $string;
Branch: 1 branch
BranchTrueFalseTotal
$string eq '' ? : 39 7 46
429 }
430  
431 =head2 what_string
432  
433 $what_tags = $this->what_string();
434  
435 Renders the C<what> list as a as tags for inclusion in the event
436 description.
437  
438  
439 =cut
440  
441 46 ▶sub what_string {
Subroutine what_string: called CC=3 SCAR=11.3
Pod documented
442 46 my $self = shift;
443 46 my @ret;
444 46 foreach my $what ( $self->what() ) {
445 11 push @ret, "<what>$what</what>";
446 }
447 46 ▶ if ( length( @ret ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length @ret > 0) { } 46 0 46
448 46 return join( ', ', @ret );
449 } else {
450 0 return undef;
451 }
452 }
453  
454 =head2 to_string
455  
456 my $event_tag = $event->to_string();
457  
458 Renders the event as an Event tag for inclusion on Wikevent.
459  
460 =cut
461  
462 45 ▶sub to_string {
Subroutine to_string: called CC=11 SCAR=24.0
Pod documented
463 45 my ( $e, $bot) = @_;
464 45 my @attrs = qw{ name date time endtime duration lang
465 price tickets restrictions locality venue };
466 45 my @req = qw{ name date time venue locality };
467 45 my @missing;
468 45 foreach my $key ( @req ) {
469 225 ▶ if ( ! defined ( $e->{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $e->{$key}) 134 91 225
470 134 push @missing, $key;
471 }
472 }
473 45 ▶ warn $REQ_WARNING . join( ', ', @missing) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $missing[0] 37 8 45
474 if ( defined( $missing[0] ) );
475 45 my $attrs = '';
476 45 foreach my $key ( @attrs ) {
477 495 ▶ next unless defined $e->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e->{$key} 327 168 495
478 168 $attrs .= " $key=\"$e->{$key}\"\n";
479 }
480 45 my $desc = $e->desc();
481 45 my $who = $e->who_string();
482 45 my $what = $e->what_string();
483 45 ▶ my $by = "<by>$bot</by>" if defined $bot;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bot 0 45 45
484 45 my $event = "<event\n$attrs>";
485 45 ▶ $event .= "$who\n" if $who;
Branch: 1 branch
BranchTrueFalseTotal
if $who 6 39 45
486 45 ▶ $event .= "$desc\n" if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 7 38 45
487 45 ▶ $event .= "$what\n" if $what;
Branch: 1 branch
BranchTrueFalseTotal
if $what 4 41 45
488 45 ▶ $event .= "$by\n" if $by;
Branch: 1 branch
BranchTrueFalseTotal
if $by 0 45 45
489 45 $event .= "</event>\n";
490 45 return $event;
491 }
492  
493 2 1;
494  
495 __END__
« Bot.pm ↑ Summary