Class: Rockbox::Api::Playlist

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

Constant Summary collapse

TRACK_FIELDS =
<<~GQL
  fragment TrackFields on Track {
    id title artist album genre disc trackString yearString
    composer comment albumArtist grouping
    discnum tracknum layer year bitrate frequency
    filesize length elapsed path
    albumId artistId genreId albumArt
  }
GQL

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Playlist

Returns a new instance of Playlist.



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

def initialize(http)
  @http = http
end

Instance Method Details

#amountObject



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

def amount
  @http.execute("query PlaylistAmount { playlistAmount }")[:playlist_amount]
end

#clearObject



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

def clear
  @http.execute("mutation ClearPlaylist { playlistRemoveAllTracks }")
  nil
end

#create(name, tracks) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/rockbox/api/playlist.rb', line 103

def create(name, tracks)
  @http.execute(
    "mutation CreatePlaylist($name: String!, $tracks: [String!]!) { " \
    "playlistCreate(name: $name, tracks: $tracks) }",
    { name: name, tracks: tracks }
  )
  nil
end

#currentRockbox::Playlist

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rockbox/api/playlist.rb', line 23

def current
  data = @http.execute(<<~GQL)
    #{TRACK_FIELDS}
    query CurrentPlaylist {
      playlistGetCurrent {
        amount index maxPlaylistSize firstIndex
        lastInsertPos seed lastShuffledStart
        tracks { ...TrackFields }
      }
    }
  GQL
  pl = data[:playlist_get_current] || {}
  Rockbox::Playlist.new(
    amount:              pl[:amount],
    index:               pl[:index],
    max_playlist_size:   pl[:max_playlist_size],
    first_index:         pl[:first_index],
    last_insert_pos:     pl[:last_insert_pos],
    seed:                pl[:seed],
    last_shuffled_start: pl[:last_shuffled_start],
    tracks:              Array(pl[:tracks]).map { |t| Track.from_hash(t) }
  )
end

#insert_album(album_id, position: InsertPosition::LAST) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/rockbox/api/playlist.rb', line 76

def insert_album(album_id, position: InsertPosition::LAST)
  @http.execute(
    "mutation InsertAlbum($albumId: String!, $position: Int!) { " \
    "insertAlbum(albumId: $albumId, position: $position) }",
    { album_id: album_id, position: position }
  )
  nil
end

#insert_directory(directory, position: InsertPosition::LAST, playlist_id: nil) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/rockbox/api/playlist.rb', line 67

def insert_directory(directory, position: InsertPosition::LAST, playlist_id: nil)
  @http.execute(
    "mutation InsertDirectory($playlistId: String, $position: Int!, $directory: String!) { " \
    "insertDirectory(playlistId: $playlistId, position: $position, directory: $directory) }",
    { playlist_id: playlist_id, position: position, directory: directory }
  )
  nil
end

#insert_tracks(paths, position: InsertPosition::NEXT, playlist_id: nil) ⇒ Object

Parameters:

  • paths (Array<String>)

    file paths or track IDs to insert.

  • position (Integer) (defaults to: InsertPosition::NEXT)

    one of InsertPosition (default: NEXT).

  • playlist_id (String, nil) (defaults to: nil)

    target playlist; nil for the active queue.



58
59
60
61
62
63
64
65
# File 'lib/rockbox/api/playlist.rb', line 58

def insert_tracks(paths, position: InsertPosition::NEXT, playlist_id: nil)
  @http.execute(
    "mutation InsertTracks($playlistId: String, $position: Int!, $tracks: [String!]!) { " \
    "insertTracks(playlistId: $playlistId, position: $position, tracks: $tracks) }",
    { playlist_id: playlist_id, position: position, tracks: paths }
  )
  nil
end

#remove_track(index) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/rockbox/api/playlist.rb', line 85

def remove_track(index)
  @http.execute(
    "mutation RemoveTrack($index: Int!) { playlistRemoveTrack(index: $index) }",
    { index: index }
  )
  nil
end

#resumeObject



121
122
123
124
# File 'lib/rockbox/api/playlist.rb', line 121

def resume
  @http.execute("mutation PlaylistResume { playlistResume }")
  nil
end

#shuffleObject



98
99
100
101
# File 'lib/rockbox/api/playlist.rb', line 98

def shuffle
  @http.execute("mutation ShufflePlaylist { shufflePlaylist }")
  nil
end

#start(start_index: nil, elapsed: nil, offset: nil) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/rockbox/api/playlist.rb', line 112

def start(start_index: nil, elapsed: nil, offset: nil)
  @http.execute(
    "mutation PlaylistStart($startIndex: Int, $elapsed: Int, $offset: Int) { " \
    "playlistStart(startIndex: $startIndex, elapsed: $elapsed, offset: $offset) }",
    { start_index: start_index, elapsed: elapsed, offset: offset }.compact
  )
  nil
end