Class: Turntabler::PlaylistDirectory

Inherits:
Object
  • Object
show all
Includes:
Assertions
Defined in:
lib/turntabler/playlist_directory.rb

Overview

Provides a set of helper methods for interacting with a user’s playlists.

Instance Method Summary collapse

Methods included from Assertions

#assert_valid_keys, #assert_valid_values

Constructor Details

#initialize(client) ⇒ PlaylistDirectory

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PlaylistDirectory.



9
10
11
12
# File 'lib/turntabler/playlist_directory.rb', line 9

def initialize(client)
  @client = client
  @playlists = {}
end

Instance Method Details

#allArray<Turntabler::Playlist>

Gets the list of playlists created.

Examples:

playlists.all   # => [#<Turntabler::Playlist ...>, ...]

Returns:

Raises:



34
35
36
37
# File 'lib/turntabler/playlist_directory.rb', line 34

def all
  data = api('playlist.list_all')
  data['list'].map {|attrs| build(attrs)}
end

#build(attrs) ⇒ Turntabler::Playlist

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets the playlist represented by the given attributes.

If the playlist hasn’t been previously accessed, then a new Playlist instance will get created.

Parameters:

  • attrs (Hash)

    The attributes representing the playlist

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/turntabler/playlist_directory.rb', line 47

def build(attrs)
  playlist = Playlist.new(client, attrs)

  # Update existing in cache or cache a new playlist
  if existing = @playlists[playlist.id]
    playlist = existing
    playlist.attributes = attrs
  else
    @playlists[playlist.id] = playlist
  end

  playlist
end

#create(id) ⇒ Turntabler::Playlist

Note:

This will automatically enter the playlist when it is created

Creates a new playlist with the given id. This should only be used if the playlist doesn’t already exist.

Examples:

playlists.create("Rock")  # => #<Turntabler::Playlist ...>

Parameters:

  • id (String)

    The unique identifier of the playlist

Returns:

Raises:



23
24
25
26
# File 'lib/turntabler/playlist_directory.rb', line 23

def create(id)
  api('playlist.create', :playlist_name => id)
  build(:_id => id)
end