/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

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