3 use Web::Simple __PACKAGE__;
4 use Method::Signatures::Simple;
7 use HTML::Zoom::FilterBuilder::Template;
10 package App::IdiotBox::Announcement;
12 sub made_at { shift->{made_at} }
13 sub bucket { shift->{bucket} }
14 sub video_count { shift->{video_count} }
16 package App::IdiotBox::Bucket;
18 sub slug { shift->{slug} }
19 sub name { shift->{name} }
21 exists $_[0]->{video_count}
22 ? $_[0]->{video_count}
23 : $_[0]->{videos}->count
25 sub videos { shift->{videos} }
27 package App::IdiotBox::Video;
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} }
35 (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
41 template_dir => 'share/html',
43 db_file => 'var/lib/idiotbox.db',
44 base_url => 'http://localhost:3000/',
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);
59 sub (/) { $self->show_front_page },
60 subdispatch sub (/*/...) {
61 my $bucket = $self->buckets->get({ slug => $_[1] });
64 $self->show_bucket($bucket)
67 $self->show_video($bucket->videos->get({ slug => $_[1] }));
73 method recent_announcements { $self->{recent_announcements} }
75 method buckets { $self->{buckets} }
77 method show_front_page {
78 my $ann = $self->recent_announcements;
81 $_->select('#announcement-list')
82 ->repeat_content($ann->map(sub {
85 $_->select('.bucket-name')->replace_content($obj->bucket->name)
86 ->select('.bucket-link')->set_attribute({
87 name => 'href', value => $obj->bucket->slug.'/'
89 ->select('.new-videos')->replace_content($obj->video_count)
90 ->select('.total-videos')->replace_content(
91 $obj->bucket->video_count
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 {
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.'/' }
115 method show_video ($video) {
116 $self->html_response(video => sub {
119 .join('/', $video->bucket->slug, $video->slug, $video->file_name.'.flv');
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 => '../' }
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 });
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 ]
137 method _template_filename_for ($name) {
138 $self->{config}{template_dir}.'/'.$name.'.html';
141 method _layout_zoom {
142 $self->{layout_zoom} ||= HTML::Zoom->from_file(
143 $self->_template_filename_for('layout')
147 method _zoom_for ($template_name, $selectors) {
148 ($self->{zoom_for_template}{$template_name} ||= do {
150 HTML::Zoom->from_file(
151 $self->_template_filename_for($template_name)
153 ->select('#main-content')->collect_content({ into => \@body })
156 ->select('#main-content')->replace_content(\@body)
158 })->apply($selectors);
162 $self->{base_url} ||= do {
163 (my $u = $self->config->{base_url}) =~ s/\/$//;