split out HTML::hCalendar
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 14 Nov 2009 14:42:09 +0000 (15:42 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 14 Nov 2009 14:42:09 +0000 (15:42 +0100)
lib/HTML5TV/hCalendar.pm [new file with mode: 0644]
t/HTML5TV-hCalendar.t [new file with mode: 0755]

diff --git a/lib/HTML5TV/hCalendar.pm b/lib/HTML5TV/hCalendar.pm
new file mode 100644 (file)
index 0000000..aba6700
--- /dev/null
@@ -0,0 +1,49 @@
+package HTML5TV::hCalendar;
+
+use warnings;
+use strict;
+
+use File::Slurp;
+use HTML::TreeBuilder;
+use Data::Dump qw/dump/;
+
+sub new {
+       my $class = shift;
+       my $path = shift || die "need path to hCalendar";
+
+       my $tree = HTML::TreeBuilder->new;
+       $tree->parse_file($path);
+
+       my $self = {
+               path => $path,
+               tree => $tree,
+       };
+       bless $self, $class;
+       return $self;
+}
+
+# we don't want DESTROY to fallback into AUTOLOAD
+sub DESTROY {}
+
+our $AUTOLOAD;
+sub AUTOLOAD {
+       my $self = shift;
+
+    my $class = $AUTOLOAD;
+    $class =~ s/.*://;
+
+warn "XX $class\n";
+
+       if ( my $vevent = $self->{tree}->look_down( class => 'vevent' ) ) {
+               if ( my $el = $vevent->look_down( class => $class ) ) {
+                       return $el->as_trimmed_text;
+               } else {
+                       die "can't find vevent.$class in ", $self->{path};
+               }
+       } else {
+               die "can't find vevent in ", $self->{path};
+       }
+
+}
+
+1;
diff --git a/t/HTML5TV-hCalendar.t b/t/HTML5TV-hCalendar.t
new file mode 100755 (executable)
index 0000000..6f30c19
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+
+use Test::More tests => 7;
+
+use lib 'lib';
+
+BEGIN {
+       use_ok( 'HTML5TV::hCalendar' );
+}
+
+my $path = shift @ARGV || 'media/hCalendar.html';
+
+ok( my $hcal = HTML5TV::hCalendar->new( $path ), "new $path" );
+
+foreach my $class ( qw/organiser summary url dtstart description/ ) {
+       ok( defined( my $text = $hcal->$class ), $class );
+       diag "$class: $text";
+}
+