dff116bbb82888e4d51f0d2ed43abbbe6d3fdc57
[catagits/App-IdiotBox.git] / lib / App / IdiotBox.pm
1 package App::IdiotBox;
2
3 use Web::Simple __PACKAGE__;
4 use Method::Signatures::Simple;
5 use FindBin;
6 use HTML::Zoom;
7 use HTML::Zoom::FilterBuilder::Template;
8
9 {
10   package App::IdiotBox::Announcement;
11
12   sub made_at { shift->{made_at} } 
13   sub bucket { shift->{bucket} } 
14   sub video_count { shift->{video_count} } 
15
16   package App::IdiotBox::Bucket;
17
18   sub slug { shift->{slug} }
19   sub name { shift->{name} }
20   sub video_count {
21     exists $_[0]->{video_count}
22       ? $_[0]->{video_count}
23       : $_[0]->{videos}->count
24   }
25   sub videos { shift->{videos} }
26
27   package App::IdiotBox::Video;
28
29   sub slug { shift->{slug} }
30   sub name { shift->{name} }
31   sub author { shift->{author} }
32   sub details { shift->{details} }
33   sub bucket { shift->{bucket} }
34   sub file_name {
35     (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
36     $s;
37   }
38 }
39
40 default_config(
41   template_dir => 'share/html',
42   store => 'SQLite',
43   db_file => 'var/lib/idiotbox.db',
44   base_url => 'http://localhost:3000/',
45 );
46
47 sub BUILD {
48   my $self = shift;
49   my $store;
50   ($store = $self->config->{store}) =~ /^(\w+)$/
51     or die "Store config should be just a name, got ${store} instead";
52   my $store_class = "App::IdiotBox::Store::${store}";
53   eval "require ${store_class}; 1"
54     or die "Couldn't load ${store} store: $@";
55   $store_class->bind($self);
56 }
57   
58 dispatch {
59   sub (/) { $self->show_front_page },
60   subdispatch sub (/*/...) {
61     my $bucket = $self->buckets->get({ slug => $_[1] });
62     [
63       sub (/) {
64         $self->show_bucket($bucket)
65       },
66       sub (/*/) {
67         $self->show_video($bucket->videos->get({ slug => $_[1] }));
68       }
69     ]
70   }
71 };
72
73 method recent_announcements { $self->{recent_announcements} }
74
75 method buckets { $self->{buckets} }
76
77 method show_front_page {
78   my $ann = $self->recent_announcements;
79   $self->html_response(
80     front_page => sub {
81       $_->select('#announcement-list')
82         ->repeat_content($ann->map(sub {
83             my $obj = $_;
84             sub {
85               $_->select('.bucket-name')->replace_content($obj->bucket->name)
86                 ->select('.bucket-link')->set_attribute({
87                     name => 'href', value => $obj->bucket->slug.'/'
88                   })
89                 ->select('.new-videos')->replace_content($obj->video_count)
90                 ->select('.total-videos')->replace_content(
91                     $obj->bucket->video_count
92                   )
93             }
94           }))
95     }
96   );
97 }
98
99 method show_bucket ($bucket) {
100   $self->html_response(bucket => sub {
101     $_->select('.bucket-name')->replace_content($bucket->name)
102       ->select('#video-list')->repeat_content($bucket->videos->map(sub {
103           my $video = $_;
104           sub {
105             $_->select('.video-name')->replace_content($video->name)
106               ->select('.video-author')->replace_content($video->author)
107               ->select('.video-link')->set_attribute(
108                   { name => 'href', value => $video->slug.'/' }
109                 )
110           }
111         }))
112   });
113 }
114
115 method show_video ($video) {
116   $self->html_response(video => sub {
117     my $video_url = 
118       $self->base_url
119       .join('/', $video->bucket->slug, $video->slug, $video->file_name.'.flv');
120
121     $_->select('.video-name')->replace_content($video->name)
122       ->select('.author-name')->replace_content($video->author)
123       ->select('.bucket-link')->set_attribute(
124           { name => 'href', value => '../' }
125         )
126       ->select('.bucket-name')->replace_content($video->bucket->name)
127       ->select('.video-details')->replace_content($video->details)
128       ->select('script')->template_text_raw({ video_url => $video_url });
129   });
130 }
131
132 method html_response ($template_name, $selectors) {
133   my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
134   return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
135 }
136
137 method _template_filename_for ($name) {
138   $self->{config}{template_dir}.'/'.$name.'.html';
139 }
140
141 method _layout_zoom {
142   $self->{layout_zoom} ||= HTML::Zoom->from_file(
143     $self->_template_filename_for('layout')
144   )
145 }
146
147 method _zoom_for ($template_name, $selectors) {
148   ($self->{zoom_for_template}{$template_name} ||= do {
149     my @body;
150     HTML::Zoom->from_file(
151                   $self->_template_filename_for($template_name)
152                 )
153               ->select('#main-content')->collect_content({ into => \@body })
154               ->run;
155     $self->_layout_zoom
156          ->select('#main-content')->replace_content(\@body)
157          ->memoize;
158   })->apply($selectors);
159 }
160
161 method base_url {
162   $self->{base_url} ||= do {
163     (my $u = $self->config->{base_url}) =~ s/\/$//;
164     "${u}/";
165   }
166 }
167
168 1;