Class: Walkman::Playlist

Inherits:
Object
  • Object
show all
Defined in:
lib/walkman/playlist.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Playlist

Returns a new instance of Playlist.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/walkman/playlist.rb', line 6

def initialize(options = {})
  songs = options.delete(:songs) || []
  @queue = [songs].flatten # can add one or more Songs
  @auto_queue = options.delete(:auto_queue) || false

  if echonest_playlist = echonest_playlist_create(options)
    @session_id = echonest_playlist.session_id
  end

  if @auto_queue && @session_id
    auto_queue
    self.next
  end
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



4
5
6
# File 'lib/walkman/playlist.rb', line 4

def queue
  @queue
end

#session_idObject

Returns the value of attribute session_id.



3
4
5
# File 'lib/walkman/playlist.rb', line 3

def session_id
  @session_id
end

Instance Method Details

#add(songs, position = -1)) ⇒ Object



34
35
36
37
38
# File 'lib/walkman/playlist.rb', line 34

def add(songs, position = -1)
  index = @queue.size
  index = [position, index].min if position >= 0
  @queue.insert(index, songs).flatten!
end

#clearObject



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

def clear
  @queue = []
end

#feedback(types, songs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/walkman/playlist.rb', line 65

def feedback(types, songs)
  songs    = [songs].flatten # one or more
  types    = [types].flatten # one or more
  song_ids = songs.map(&:echonest_song_id)
  args     = { session_id: @session_id }

  if types.include?(:favorite)
    args[:favorite_song] = song_ids
    args[:favorite_artist] = songs.map(&:echonest_artist_id)
  end

  args[:unplay_song] = song_ids if types.include?(:unplay)
  args[:skip_song] = song_ids if types.include?(:skip)

  Walkman.echowrap.playlist_dynamic_feedback(args)
end

#include?(song) ⇒ Boolean Also known as: queued?

Returns:

  • (Boolean)


25
26
27
# File 'lib/walkman/playlist.rb', line 25

def include?(song)
  @queue.include?(song)
end

#next(count = 1) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/walkman/playlist.rb', line 44

def next(count = 1)
  # if the playlist is not empty, we can get one or more
  # songs back so we need to make sure we get the last one
  songs = @queue.shift(count)
  songs = [songs].flatten
  song = songs.pop # the last song skipped

  # skip and unplay songs so our echonest catalog/profile stays true
  feedback([:skip, :unplay], songs) unless songs.empty?

  if @auto_queue && size <= 5
    auto_queue(5) if @session_id
  end

  song
end

#remove(song) ⇒ Object



40
41
42
# File 'lib/walkman/playlist.rb', line 40

def remove(song)
  @queue.delete_if { |s| s == song }
end

#shuffle!Object



30
31
32
# File 'lib/walkman/playlist.rb', line 30

def shuffle!
  @queue.shuffle!
end

#sizeObject



61
62
63
# File 'lib/walkman/playlist.rb', line 61

def size
  @queue.size
end