| 1 | package WebService::8tracks::Session; | ||||||||||
| 2 | 3 | ▶ | use Any::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | =pod | ||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | WebService::8tracks::Session - 8tracks mix playing session | ||||||||||
| 9 | |||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||
| 11 | |||||||||||
| 12 | my $session = $api->create_session($mix_id); # start playing mix | ||||||||||
| 13 | |||||||||||
| 14 | # to start playing | ||||||||||
| 15 | my $res = $session->play; | ||||||||||
| 16 | my $media_url = $res->{set}->{track}->{url}; | ||||||||||
| 17 | ... | ||||||||||
| 18 | # to play next track | ||||||||||
| 19 | $res = $session->next; | ||||||||||
| 20 | ... | ||||||||||
| 21 | # to skip a track | ||||||||||
| 22 | $res = $session->skip; | ||||||||||
| 23 | |||||||||||
| 24 | if ($res->{set}->{at_end}) { | ||||||||||
| 25 | # played all tracks, does not contain URL | ||||||||||
| 26 | ... | ||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | =cut | ||||||||||
| 30 | |||||||||||
| 31 | 3 | has 'api', ( | |||||||||
| 32 | is => 'rw', | ||||||||||
| 33 | isa => 'WebService::8tracks', | ||||||||||
| 34 | required => 1, | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 3 | has 'mix_id', ( | |||||||||
| 38 | is => 'rw', | ||||||||||
| 39 | isa => 'Str', | ||||||||||
| 40 | required => 1, | ||||||||||
| 41 | ); | ||||||||||
| 42 | |||||||||||
| 43 | 3 | has 'play_token', ( | |||||||||
| 44 | is => 'rw', | ||||||||||
| 45 | isa => 'Str', | ||||||||||
| 46 | required => 1, | ||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | 3 | has '_started', ( | |||||||||
| 50 | is => 'rw', | ||||||||||
| 51 | isa => 'Bool', | ||||||||||
| 52 | default => 0, | ||||||||||
| 53 | ); | ||||||||||
| 54 | |||||||||||
| 55 | 3 | has 'at_beginning', ( | |||||||||
| 56 | is => 'rw', | ||||||||||
| 57 | isa => 'Bool', | ||||||||||
| 58 | default => 0, | ||||||||||
| 59 | ); | ||||||||||
| 60 | |||||||||||
| 61 | 3 | has 'at_end', ( | |||||||||
| 62 | is => 'rw', | ||||||||||
| 63 | isa => 'Bool', | ||||||||||
| 64 | default => 0, | ||||||||||
| 65 | ); | ||||||||||
| 66 | |||||||||||
| 67 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||
| 68 | |||||||||||
| 69 | 3 | ▶ | no Any::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | |||||||||||
| 71 | 10 | ▶ | sub update_status { | ||||||||
|
Subroutine
update_status: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 72 | 10 | my ($self, $res) = @_; | |||||||||
| 73 | |||||||||||
| 74 | 10 | foreach (qw(at_beginning at_end)) { | |||||||||
| 75 | 20 | ▶ | $self->$_(!!$res->{set}->{$_}) if defined $res->{set}->{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 10 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 80 | 10 | my ($self, $command) = @_; | |||||||||
| 81 | |||||||||||
| 82 | 10 | my $res = $self->api->request_api( | |||||||||
| 83 | GET => "sets/$self->{play_token}/$command", | ||||||||||
| 84 | { mix_id => $self->mix_id }, | ||||||||||
| 85 | ); | ||||||||||
| 86 | 10 | $self->update_status($res); | |||||||||
| 87 | 10 | return $res; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | =head1 METHODS | ||||||||||
| 91 | |||||||||||
| 92 | =over 4 | ||||||||||
| 93 | |||||||||||
| 94 | =item play | ||||||||||
| 95 | |||||||||||
| 96 | my $res = $session->play; | ||||||||||
| 97 | |||||||||||
| 98 | Start playing. | ||||||||||
| 99 | |||||||||||
| 100 | =cut | ||||||||||
| 101 | |||||||||||
| 102 | 1 | ▶ | sub play { | ||||||||
|
Subroutine
play: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 103 | 1 | my $self = shift; | |||||||||
| 104 | 1 | $self->_started(1); | |||||||||
| 105 | 1 | return $self->execute('play'); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | =item next | ||||||||||
| 109 | |||||||||||
| 110 | my $res = $session->next; | ||||||||||
| 111 | |||||||||||
| 112 | Go to next track. Calls play() if playing is not started. | ||||||||||
| 113 | |||||||||||
| 114 | =cut | ||||||||||
| 115 | |||||||||||
| 116 | 6 | ▶ | sub next { | ||||||||
|
Subroutine
next: called CC=2 SCAR=8.4
Pod
documented
| |||||||||||
| 117 | 6 | my $self = shift; | |||||||||
| 118 | 6 | ▶ | if ($self->_started) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 6 | return $self->execute('next'); | |||||||||
| 120 | } else { | ||||||||||
| 121 | 0 | $self->_started(1); | |||||||||
| 122 | 0 | return $self->execute('play'); | |||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | =item skip | ||||||||||
| 127 | |||||||||||
| 128 | my $res = $session->skip; | ||||||||||
| 129 | |||||||||||
| 130 | Skip to next track. | ||||||||||
| 131 | |||||||||||
| 132 | =back | ||||||||||
| 133 | |||||||||||
| 134 | =cut | ||||||||||
| 135 | |||||||||||
| 136 | 3 | ▶ | sub skip { | ||||||||
|
Subroutine
skip: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 137 | 3 | my $self = shift; | |||||||||
| 138 | 3 | return $self->execute('skip'); | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 3 | 1; | |||||||||
| 142 | |||||||||||
| 143 | __END__ | ||||||||||