drop Method::Signatures::Simple
[catagits/App-IdiotBox.git] / lib / App / IdiotBox.pm
CommitLineData
e30ed59d 1package App::IdiotBox;
2
3use Web::Simple __PACKAGE__;
d7497a23 4use FindBin;
5use HTML::Zoom;
998cc52c 6use HTML::Zoom::FilterBuilder::Template;
02ea620e 7use List::Util qw(first);
d7497a23 8
9{
10 package App::IdiotBox::Announcement;
11
ebba317f 12 sub id { shift->{id} }
d7497a23 13 sub made_at { shift->{made_at} }
14 sub bucket { shift->{bucket} }
15 sub video_count { shift->{video_count} }
16
17 package App::IdiotBox::Bucket;
18
19 sub slug { shift->{slug} }
20 sub name { shift->{name} }
d9702c6d 21 sub video_count {
22 exists $_[0]->{video_count}
23 ? $_[0]->{video_count}
24 : $_[0]->{videos}->count
25 }
71fd1550 26 sub videos { shift->{videos} }
27
28 package App::IdiotBox::Video;
29
30 sub slug { shift->{slug} }
31 sub name { shift->{name} }
32 sub author { shift->{author} }
33 sub details { shift->{details} }
34 sub bucket { shift->{bucket} }
998cc52c 35 sub file_name {
36 (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
37 $s;
38 }
ebba317f 39 sub url_path {
40 join('/', $_[0]->bucket->slug, $_[0]->slug);
41 }
d7497a23 42}
43
44default_config(
71a02d85 45 template_dir => 'share/html',
46 store => 'SQLite',
47 db_file => 'var/lib/idiotbox.db',
998cc52c 48 base_url => 'http://localhost:3000/',
ebba317f 49 base_dir => do { use FindBin; $FindBin::Bin },
d7497a23 50);
e30ed59d 51
71a02d85 52sub BUILD {
53 my $self = shift;
54 my $store;
55 ($store = $self->config->{store}) =~ /^(\w+)$/
56 or die "Store config should be just a name, got ${store} instead";
57 my $store_class = "App::IdiotBox::Store::${store}";
58 eval "require ${store_class}; 1"
59 or die "Couldn't load ${store} store: $@";
60 $store_class->bind($self);
61}
62
e30ed59d 63dispatch {
64 sub (/) { $self->show_front_page },
65 subdispatch sub (/*/...) {
66 my $bucket = $self->buckets->get({ slug => $_[1] });
67 [
68 sub (/) {
69 $self->show_bucket($bucket)
70 },
71a02d85 71 sub (/*/) {
d7497a23 72 $self->show_video($bucket->videos->get({ slug => $_[1] }));
e30ed59d 73 }
74 ]
75 }
76};
77
847de56a 78sub recent_announcements { shift->{recent_announcements} }
d7497a23 79
847de56a 80sub buckets { shift->{buckets} }
71a02d85 81
847de56a 82sub show_front_page {
83 my $self = shift;
e30ed59d 84 my $ann = $self->recent_announcements;
85 $self->html_response(
1a1c4f78 86 front_page => sub {
87 $_->select('#announcement-list')
88 ->repeat_content($ann->map(sub {
89 my $obj = $_;
90 sub {
91 $_->select('.bucket-name')->replace_content($obj->bucket->name)
02ea620e 92 ->select('.made-at')->replace_content($obj->made_at)
1a1c4f78 93 ->select('.bucket-link')->set_attribute({
94 name => 'href', value => $obj->bucket->slug.'/'
95 })
96 ->select('.new-videos')->replace_content($obj->video_count)
97 ->select('.total-videos')->replace_content(
98 $obj->bucket->video_count
99 )
100 }
101 }))
102 }
e30ed59d 103 );
104}
105
847de56a 106sub show_bucket {
107 my ($self, $bucket) = @_;
1a1c4f78 108 $self->html_response(bucket => sub {
109 $_->select('.bucket-name')->replace_content($bucket->name)
110 ->select('#video-list')->repeat_content($bucket->videos->map(sub {
111 my $video = $_;
112 sub {
113 $_->select('.video-name')->replace_content($video->name)
114 ->select('.video-author')->replace_content($video->author)
115 ->select('.video-link')->set_attribute(
116 { name => 'href', value => $video->slug.'/' }
117 )
118 }
119 }))
120 });
d7497a23 121}
122
847de56a 123sub show_video {
124 my ($self, $video) = @_;
02ea620e 125 my $video_file = first {
126 -e join('/', $self->config->{base_dir}, $_)
127 } map {
128 join('/', $video->bucket->slug, $video->slug, $video->file_name.".$_")
129 } qw(flv m4v);
1a1c4f78 130 $self->html_response(video => sub {
998cc52c 131 my $video_url =
132 $self->base_url
6df05090 133 .($video_file||'NO FILE FOUND SORRY');
998cc52c 134
1a1c4f78 135 $_->select('.video-name')->replace_content($video->name)
136 ->select('.author-name')->replace_content($video->author)
137 ->select('.bucket-link')->set_attribute(
138 { name => 'href', value => '../' }
139 )
140 ->select('.bucket-name')->replace_content($video->bucket->name)
141 ->select('.video-details')->replace_content($video->details)
998cc52c 142 ->select('script')->template_text_raw({ video_url => $video_url });
1a1c4f78 143 });
d7497a23 144}
145
847de56a 146sub html_response {
147 my ($self, $template_name, $selectors) = @_;
1a1c4f78 148 my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
e30ed59d 149 return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
150}
151
847de56a 152sub _template_filename_for {
153 my ($self, $name) = @_;
d7497a23 154 $self->{config}{template_dir}.'/'.$name.'.html';
155}
156
847de56a 157sub _layout_zoom {
158 my $self = shift;
d7497a23 159 $self->{layout_zoom} ||= HTML::Zoom->from_file(
160 $self->_template_filename_for('layout')
e30ed59d 161 )
162}
163
847de56a 164sub _zoom_for {
165 my ($self, $template_name, $selectors) = @_;
e30ed59d 166 ($self->{zoom_for_template}{$template_name} ||= do {
167 my @body;
d7497a23 168 HTML::Zoom->from_file(
169 $self->_template_filename_for($template_name)
e30ed59d 170 )
1a1c4f78 171 ->select('#main-content')->collect_content({ into => \@body })
d7497a23 172 ->run;
1a1c4f78 173 $self->_layout_zoom
174 ->select('#main-content')->replace_content(\@body)
175 ->memoize;
176 })->apply($selectors);
e30ed59d 177}
178
847de56a 179sub base_url {
180 my $self = shift;
998cc52c 181 $self->{base_url} ||= do {
182 (my $u = $self->config->{base_url}) =~ s/\/$//;
183 "${u}/";
184 }
185}
186
847de56a 187sub _run_cli {
188 my $self = shift;
ebba317f 189 unless (@ARGV == 1 && $ARGV[0] eq 'import') {
190 return $self->SUPER::_run_cli(@_);
191 }
192 $self->cli_import;
193}
194
847de56a 195sub _cli_usage {
196 my $self = shift;
ebba317f 197 "To import data into your idiotbox install, chdir into a directory\n".
198 "containing video files and run:\n".
199 "\n".
200 " $0 import\n".
201 "\n".
202 $self->SUPER::_cli_usage(@_);
203}
204
847de56a 205sub cli_import {
206 my $self = shift;
ebba317f 207 require App::IdiotBox::Importer;
208 App::IdiotBox::Importer->run($self);
209}
210
e30ed59d 2111;