Allow creating of new buckets by using the importer
[catagits/App-IdiotBox.git] / lib / App / IdiotBox.pm
index 0f28b4a..081327e 100644 (file)
@@ -1,10 +1,11 @@
 package App::IdiotBox;
 
+use App::IdiotBox::Common qw(@SupportedFormats);
 use Web::Simple __PACKAGE__;
-use Method::Signatures::Simple;
 use FindBin;
 use HTML::Zoom;
 use HTML::Zoom::FilterBuilder::Template;
+use List::Util qw(first);
 
 {
   package App::IdiotBox::Announcement;
@@ -41,13 +42,15 @@ use HTML::Zoom::FilterBuilder::Template;
   }
 }
 
-default_config(
+has $_ => (is => 'ro') for qw(recent_announcements buckets);
+
+sub default_config {
   template_dir => 'share/html',
   store => 'SQLite',
   db_file => 'var/lib/idiotbox.db',
   base_url => 'http://localhost:3000/',
   base_dir => do { use FindBin; $FindBin::Bin },
-);
+}
 
 sub BUILD {
   my $self = shift;
@@ -60,26 +63,22 @@ sub BUILD {
   $store_class->bind($self);
 }
   
-dispatch {
+sub dispatch_request {
+  my $self = shift;
   sub (/) { $self->show_front_page },
-  subdispatch sub (/*/...) {
+  sub (/*/...) {
     my $bucket = $self->buckets->get({ slug => $_[1] });
-    [
-      sub (/) {
-        $self->show_bucket($bucket)
-      },
-      sub (/*/) {
-        $self->show_video($bucket->videos->get({ slug => $_[1] }));
-      }
-    ]
+    sub (/) {
+      $self->show_bucket($bucket)
+    },
+    sub (/*/) {
+      $self->show_video($bucket->videos->get({ slug => $_[1] }));
+    }
   }
-};
-
-method recent_announcements { $self->{recent_announcements} }
-
-method buckets { $self->{buckets} }
+}
 
-method show_front_page {
+sub show_front_page {
+  my $self = shift;
   my $ann = $self->recent_announcements;
   $self->html_response(
     front_page => sub {
@@ -88,9 +87,10 @@ method show_front_page {
             my $obj = $_;
             sub {
               $_->select('.bucket-name')->replace_content($obj->bucket->name)
-                ->select('.bucket-link')->set_attribute({
-                    name => 'href', value => $obj->bucket->slug.'/'
-                  })
+                ->select('.made-at')->replace_content($obj->made_at)
+                ->select('.bucket-link')->set_attribute(
+                    'href' => $obj->bucket->slug.'/'
+                  )
                 ->select('.new-videos')->replace_content($obj->video_count)
                 ->select('.total-videos')->replace_content(
                     $obj->bucket->video_count
@@ -101,7 +101,8 @@ method show_front_page {
   );
 }
 
-method show_bucket ($bucket) {
+sub show_bucket {
+  my ($self, $bucket) = @_;
   $self->html_response(bucket => sub {
     $_->select('.bucket-name')->replace_content($bucket->name)
       ->select('#video-list')->repeat_content($bucket->videos->map(sub {
@@ -110,23 +111,29 @@ method show_bucket ($bucket) {
             $_->select('.video-name')->replace_content($video->name)
               ->select('.video-author')->replace_content($video->author)
               ->select('.video-link')->set_attribute(
-                  { name => 'href', value => $video->slug.'/' }
+                  href => $video->slug.'/'
                 )
           }
         }))
   });
 }
 
-method show_video ($video) {
+sub show_video {
+  my ($self, $video) = @_;
+  my $video_file = first {
+    -e join('/', $self->config->{base_dir}, $_)
+  } map {
+    join('/', $video->bucket->slug, $video->slug, $video->file_name.".$_")
+  } @SupportedFormats;
   $self->html_response(video => sub {
     my $video_url = 
       $self->base_url
-      .join('/', $video->bucket->slug, $video->slug, $video->file_name.'.flv');
+      .($video_file||'NO FILE FOUND SORRY');
 
     $_->select('.video-name')->replace_content($video->name)
       ->select('.author-name')->replace_content($video->author)
       ->select('.bucket-link')->set_attribute(
-          { name => 'href', value => '../' }
+          href => '../'
         )
       ->select('.bucket-name')->replace_content($video->bucket->name)
       ->select('.video-details')->replace_content($video->details)
@@ -134,22 +141,26 @@ method show_video ($video) {
   });
 }
 
-method html_response ($template_name, $selectors) {
+sub html_response {
+  my ($self, $template_name, $selectors) = @_;
   my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
   return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
 }
 
-method _template_filename_for ($name) {
+sub _template_filename_for {
+  my ($self, $name) = @_;
   $self->{config}{template_dir}.'/'.$name.'.html';
 }
 
-method _layout_zoom {
+sub _layout_zoom {
+  my $self = shift;
   $self->{layout_zoom} ||= HTML::Zoom->from_file(
     $self->_template_filename_for('layout')
   )
 }
 
-method _zoom_for ($template_name, $selectors) {
+sub _zoom_for {
+  my ($self, $template_name, $selectors) = @_;
   ($self->{zoom_for_template}{$template_name} ||= do {
     my @body;
     HTML::Zoom->from_file(
@@ -163,21 +174,24 @@ method _zoom_for ($template_name, $selectors) {
   })->apply($selectors);
 }
 
-method base_url {
+sub base_url {
+  my $self = shift;
   $self->{base_url} ||= do {
     (my $u = $self->config->{base_url}) =~ s/\/$//;
     "${u}/";
   }
 }
 
-method _run_cli {
+sub _run_cli {
+  my $self = shift;
   unless (@ARGV == 1 && $ARGV[0] eq 'import') {
     return $self->SUPER::_run_cli(@_);
   }
   $self->cli_import;
 }
 
-method _cli_usage {
+sub _cli_usage {
+  my $self = shift;
   "To import data into your idiotbox install, chdir into a directory\n".
   "containing video files and run:\n".
   "\n".
@@ -186,7 +200,8 @@ method _cli_usage {
   $self->SUPER::_cli_usage(@_);
 }
 
-method cli_import {
+sub cli_import {
+  my $self = shift;
   require App::IdiotBox::Importer;
   App::IdiotBox::Importer->run($self);
 }