video url handling
[catagits/App-IdiotBox.git] / lib / App / IdiotBox.pm
CommitLineData
e30ed59d 1package App::IdiotBox;
2
3use Web::Simple __PACKAGE__;
4use Method::Signatures::Simple;
d7497a23 5use FindBin;
6use HTML::Zoom;
998cc52c 7use HTML::Zoom::FilterBuilder::Template;
d7497a23 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} }
d9702c6d 20 sub video_count {
21 exists $_[0]->{video_count}
22 ? $_[0]->{video_count}
23 : $_[0]->{videos}->count
24 }
71fd1550 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} }
998cc52c 34 sub file_name {
35 (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
36 $s;
37 }
d7497a23 38}
39
40default_config(
71a02d85 41 template_dir => 'share/html',
42 store => 'SQLite',
43 db_file => 'var/lib/idiotbox.db',
998cc52c 44 base_url => 'http://localhost:3000/',
d7497a23 45);
e30ed59d 46
71a02d85 47sub 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
e30ed59d 58dispatch {
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 },
71a02d85 66 sub (/*/) {
d7497a23 67 $self->show_video($bucket->videos->get({ slug => $_[1] }));
e30ed59d 68 }
69 ]
70 }
71};
72
d7497a23 73method recent_announcements { $self->{recent_announcements} }
74
71a02d85 75method buckets { $self->{buckets} }
76
e30ed59d 77method show_front_page {
78 my $ann = $self->recent_announcements;
79 $self->html_response(
1a1c4f78 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 }
e30ed59d 96 );
97}
98
d7497a23 99method show_bucket ($bucket) {
1a1c4f78 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 });
d7497a23 113}
114
115method show_video ($video) {
1a1c4f78 116 $self->html_response(video => sub {
998cc52c 117 my $video_url =
118 $self->base_url
119 .join('/', $video->bucket->slug, $video->slug, $video->file_name.'.flv');
120
1a1c4f78 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)
998cc52c 128 ->select('script')->template_text_raw({ video_url => $video_url });
1a1c4f78 129 });
d7497a23 130}
131
e30ed59d 132method html_response ($template_name, $selectors) {
1a1c4f78 133 my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
e30ed59d 134 return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
135}
136
d7497a23 137method _template_filename_for ($name) {
138 $self->{config}{template_dir}.'/'.$name.'.html';
139}
140
e30ed59d 141method _layout_zoom {
d7497a23 142 $self->{layout_zoom} ||= HTML::Zoom->from_file(
143 $self->_template_filename_for('layout')
e30ed59d 144 )
145}
146
147method _zoom_for ($template_name, $selectors) {
148 ($self->{zoom_for_template}{$template_name} ||= do {
149 my @body;
d7497a23 150 HTML::Zoom->from_file(
151 $self->_template_filename_for($template_name)
e30ed59d 152 )
1a1c4f78 153 ->select('#main-content')->collect_content({ into => \@body })
d7497a23 154 ->run;
1a1c4f78 155 $self->_layout_zoom
156 ->select('#main-content')->replace_content(\@body)
157 ->memoize;
158 })->apply($selectors);
e30ed59d 159}
160
998cc52c 161method base_url {
162 $self->{base_url} ||= do {
163 (my $u = $self->config->{base_url}) =~ s/\/$//;
164 "${u}/";
165 }
166}
167
e30ed59d 1681;