PageRenderTime 51ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 0ms

/lib/Teto/Tatsumaki/Handler/API.pm

http://github.com/motemen/Teto
Perl | 63 lines | 51 code | 12 blank | 0 comment | 3 complexity | 28da749e7369ea1acb0cb1966c140e03 MD5 | raw file
 1package Teto::Tatsumaki::Handler::API;
 2use strict;
 3use warnings;
 4use parent 'Teto::Tatsumaki::Handler';
 5use Teto::Playlist;
 6use Tatsumaki::Error;
 7
 8sub get {
 9    my ($self, $command) = @_;
10    my $get_command = $self->can("_get_$command")
11        or Tatsumaki::Error::HTTP->throw(405);
12    $self->$get_command;
13}
14
15sub post {
16    my ($self, $command) = @_;
17    my $post_command = $self->can("_post_$command")
18        or Tatsumaki::Error::HTTP->throw(405);
19    $self->$post_command;
20}
21
22sub _get_playlist {
23    my ($self, $playlist) = @_;
24
25    my $control = $self->build_control;
26    $playlist ||= Teto::Playlist->of_url($self->request->param('url')) || $control->playlist;
27    $self->render('_playlist.html', { 
28        playlist => $playlist,
29        control  => $control,
30    });
31}
32
33sub _post_playlist {
34    my $self = shift;
35    my $playlist = Teto::Playlist->feed_async($self->request->param('url'));
36    $self->_get_playlist;
37}
38
39sub _post_delete_track {
40    my $self = shift;
41
42    my $control = $self->build_control;
43    my $i = $self->request->param('i');
44    if ($i > 0) {
45        splice @{ $control->queue->tracks }, $i, 1;
46    }
47
48    $self->response->code(204);
49}
50
51sub _post_play {
52    my $self = shift;
53    
54    my $control = $self->build_control;
55    my $playlist = Teto::Playlist->of_url( $self->request->param('playlist') );
56    if ($playlist) {
57        $control->set_playlist($playlist);
58    }
59
60    $self->_get_playlist($playlist);
61}
62
631;