Class: Turntabler::Playlist

Inherits:
Resource show all
Defined in:
lib/turntabler/playlist.rb

Overview

Represents a collection of songs managed by the user and that can be played within a room

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, attribute, #attributes=, #hash, #initialize, #loaded?, #pretty_print, #pretty_print_instance_variables

Methods included from Assertions

#assert_valid_keys, #assert_valid_values

Methods included from DigestHelpers

#digest

Constructor Details

This class inherits a constructor from Turntabler::Resource

Instance Attribute Details

#activeBoolean (readonly)

Whether this is the currently active playlist

Examples:

playlist.active   # => true

Raises:



14
# File 'lib/turntabler/playlist.rb', line 14

attribute :active, :load => false

#idString (readonly)

Allow the id to be set via the “name” attribute



10
# File 'lib/turntabler/playlist.rb', line 10

attribute :id, :name, :load => false

#songsArray<Turntabler::Song> (readonly)

The songs that have been added to this playlist. Songs will be listed in the order that they will be played. If the user is currently playing, then that song will be listed last since it has been removed from the top of the queue by Turntable.



21
22
23
# File 'lib/turntabler/playlist.rb', line 21

attribute :songs, :list do |songs|
  songs.map {|attrs| Song.new(client, attrs.merge(:playlist => id))}
end

Instance Method Details

#activatetrue

Changes this playlist to be used for queueing new songs with the room.

Examples:

playlist.switch   # => true

Raises:



80
81
82
83
84
# File 'lib/turntabler/playlist.rb', line 80

def activate
  api('playlist.switch')
  self.attributes = {'active' => true}
  true
end

#deletetrue

Permanently deletes this playlist and the list of songs within it. If this is the currently active playlist, the “default” playlist will become active.

Examples:

playlist.delete   # => true

Raises:



93
94
95
96
# File 'lib/turntabler/playlist.rb', line 93

def delete
  api('playlist.delete')
  true
end

#load(options = {}) ⇒ true

Loads the attributes for this playlist. Attributes will automatically load when accessed, but this allows data to be forcefully loaded upfront.

Examples:

playlist.load   # => true
playlist.songs  # => [#<Turntabler::Song ...>, ...]

Options Hash (options):

  • minimal (Boolean) — default: false

    Whether to only include the identifiers for songs and not the entire metadata

Raises:



35
36
37
38
39
40
41
42
# File 'lib/turntabler/playlist.rb', line 35

def load(options = {})
  assert_valid_keys(options, :minimal)
  options = {:minimal => false}.merge(options)

  data = api('playlist.all', options)
  self.attributes = data
  super()
end

#song(song_id) ⇒ Turntabler::Song

Gets the song with the given id.

Examples:

playlist.song('4fd8...')    # => #<Turntabler::Song ...>

Raises:



105
106
107
# File 'lib/turntabler/playlist.rb', line 105

def song(song_id)
  songs.detect {|song| song.id == song_id}
end

#update(attributes = {}) ⇒ true

Updates this playlist's information.

Examples:

playlist.update(:id => "rock")  # => true

Options Hash (attributes):

  • :id (String)

Raises:

  • (ArgumentError)

    if an invalid attribute or value is specified

  • (Turntabler::Error)

    if the command fails



53
54
55
56
57
58
59
60
61
# File 'lib/turntabler/playlist.rb', line 53

def update(attributes = {})
  assert_valid_keys(attributes, :id)

  # Update id
  id = attributes.delete(:id)
  update_id(id) if id

  true
end