Class: Hallon::Playlist::Track

Inherits:
Track show all
Defined in:
lib/hallon/playlist.rb

Overview

Playlist::Track is a Track with additional information attached to it, that is specific to the playlist it was created from. The returned track is a snapshot of the information, so even if the underlying track moves, this Playlist::Track will still contain the same information.

There is no way to refresh the information. You’ll have to retrieve the track again.

Instance Attribute Summary collapse

Attributes inherited from Track

#offset

Attributes inherited from Base

#pointer

Instance Method Summary collapse

Methods inherited from Track

#_to_link, #album, #artist, #artists, #autolinked?, #availability, #available?, #disc, #duration, #from_link, #loaded?, local, #local?, #name, #offline_status, #placeholder?, #playable_track, #popularity, #starred=, #starred?, #status, #to_link, #unwrap

Methods included from Loadable

#load

Methods included from Linkable

#===, included, #to_str

Methods inherited from Base

#==, from, from_link, #is_linkable?, #session, to_link, #to_pointer, #to_s

Constructor Details

#initialize(pointer, playlist_pointer, index) ⇒ Track

Returns a new instance of Track.


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hallon/playlist.rb', line 27

def initialize(pointer, playlist_pointer, index)
  super(pointer)

  @index        = index
  @playlist_ptr = playlist_pointer
  @message      = Spotify.playlist_track_message(playlist_ptr, index).to_s
  @seen         = Spotify.playlist_track_seen(playlist_ptr, index)
  @added_at     = Time.at(Spotify.playlist_track_create_time(playlist_ptr, index)).utc
  @adder        = begin
    creator = Spotify.playlist_track_creator(playlist_ptr, index)
    User.from(creator)
  end
end

Instance Attribute Details

#added_atTime? (readonly)

Returns time when track at #index was added to playlist.

Returns:

  • (Time, nil)

    time when track at #index was added to playlist.


50
51
52
# File 'lib/hallon/playlist.rb', line 50

def added_at
  @added_at
end

#adderUser? (readonly)

Returns person who added track at #index to this playlist.

Returns:

  • (User, nil)

    person who added track at #index to this playlist.


53
54
55
# File 'lib/hallon/playlist.rb', line 53

def adder
  @adder
end

#indexInteger (readonly)

Note:

this value never changes, even if the original track is moved/removed

Returns index this track was created with.

Returns:

  • (Integer)

    index this track was created with.


47
48
49
# File 'lib/hallon/playlist.rb', line 47

def index
  @index
end

#messageString (readonly)

Returns message attached to this track at #index.

Returns:

  • (String)

    message attached to this track at #index.


56
57
58
# File 'lib/hallon/playlist.rb', line 56

def message
  @message
end

#playlist_ptrSpotify::Playlist (readonly, private)

Returns playlist pointer this track was created from.

Returns:

  • (Spotify::Playlist)

    playlist pointer this track was created from.


42
43
44
# File 'lib/hallon/playlist.rb', line 42

def playlist_ptr
  @playlist_ptr
end

Instance Method Details

#moved?Boolean

Returns true if the track has not yet moved.

Returns:

  • (Boolean)

    true if the track has not yet moved.


88
89
90
91
92
# File 'lib/hallon/playlist.rb', line 88

def moved?
  # using non-GC version deliberately; no need to keep a reference to
  # this pointer once we’re done here anyway
  Spotify.playlist_track(playlist_ptr, index) != pointer
end

#playlistPlaylist

Returns playlist this track was created from.

Returns:

  • (Playlist)

    playlist this track was created from.


59
60
61
# File 'lib/hallon/playlist.rb', line 59

def playlist
  Playlist.new(playlist_ptr)
end

#seen=(seen) ⇒ Playlist::Track

Note:

Word of warning; this method will update the value you get from #seen?!

Set seen status of the Playlist::Track at the given index.

Parameters:

  • seen (Boolean)

    true if the track is now seen

Returns:

Raises:

  • (IndexError)

    if the underlying track has moved

  • (Error)

    if the operation could not be completed


77
78
79
80
81
82
83
84
85
# File 'lib/hallon/playlist.rb', line 77

def seen=(seen)
  if moved?
    raise IndexError, "track has moved from #{index}"
  end

  error = Spotify.playlist_track_set_seen(playlist_ptr, index, !! seen)
  Error.maybe_raise(error)
  @seen = Spotify.playlist_track_seen(playlist_ptr, index)
end

#seen?Boolean

Returns true if track at #index has been seen.

Returns:

  • (Boolean)

    true if track at #index has been seen.

See Also:

  • Playlist#seen

65
66
67
# File 'lib/hallon/playlist.rb', line 65

def seen?
  @seen
end