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

Returns:

  • (Boolean)

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

Returns:

  • (String)


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

Returns:



18
19
20
# File 'lib/turntabler/playlist.rb', line 18

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

Returns:

  • (true)

Raises:



77
78
79
80
81
# File 'lib/turntabler/playlist.rb', line 77

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

Returns:

  • (true)

Raises:



90
91
92
93
# File 'lib/turntabler/playlist.rb', line 90

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 ...>, ...]

Parameters:

  • options (Hash) (defaults to: {})

    The configuration options

Options Hash (options):

  • minimal (Boolean) — default: false

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

Returns:

  • (true)

Raises:



32
33
34
35
36
37
38
39
# File 'lib/turntabler/playlist.rb', line 32

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 ...>

Parameters:

  • song_id (String)

    The id for the song

Returns:

Raises:



102
103
104
# File 'lib/turntabler/playlist.rb', line 102

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

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to update

Options Hash (attributes):

  • :id (String)

Returns:

  • (true)

Raises:

  • (ArgumentError)

    if an invalid attribute or value is specified

  • (Turntabler::Error)

    if the command fails



50
51
52
53
54
55
56
57
58
# File 'lib/turntabler/playlist.rb', line 50

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

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

  true
end