Module: Playlist::Format::PLS

Defined in:
lib/playlist/format/pls.rb

Overview

Module to parse and generate PLS playlists

Class Method Summary collapse

Class Method Details

.generate(playlist) ⇒ String

Generate a PLS file from a [Playlist]

Parameters:

  • playlist (Playlist)

    the playlist to be converted to PLS

Returns:

  • (String)

    PLS as a string



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/playlist/format/pls.rb', line 18

def generate(playlist)
  text = "[playlist]\n\n"
  playlist.tracks.each_with_index do |t, index|
    index += 1
    duration = (t.duration / 1000).round
    text += "File#{index}=#{t.location}\n"
    if t.creator && t.title
      text += "Title#{index}=#{t.creator} - #{t.title}\n"
    elsif t.title
      text += "Title#{index}=#{t.title}\n"
    end
    text += "Length#{index}=#{duration}\n"
    text += "\n"
  end
  text += "NumberOfEntries=#{playlist.tracks.count}\n"
  text += "Version=2\n"
  text
end

.parse(input) ⇒ Playlist

Parse a PLS file into a [Playlist]

Parameters:

  • input

    any object that responds to #each_line (either a String or a IO object)

Returns:



8
9
10
11
12
13
# File 'lib/playlist/format/pls.rb', line 8

def parse(input)
  tracks = parse_lines_to_array(input)

  # Now convert to objects
  array_to_object(tracks)
end