Module: Playlist::Format::XSPF

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

Overview

Module to parse and generate XSPF playlists

Class Method Summary collapse

Class Method Details

.generate(playlist) ⇒ String

Generate a XSPF file from a Playlist object

Parameters:

Returns:

  • (String)

    the XSPF playlist as a String



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/playlist/format/xspf.rb', line 26

def generate(playlist)
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.playlist(:version => 1, :xmlns => 'http://xspf.org/ns/0/') do
      xml.title(playlist.title) unless playlist.title.nil?
      xml.trackList do
        playlist.tracks.each { |track| generate_track(xml, track) }
      end
    end
  end
  builder.to_xml
end

.parse(input) ⇒ Playlist

Parse a XSPF file into a new Playlist object

Parameters:

  • input (String, IO)

    the source of the XSPF file

Returns:



13
14
15
16
17
18
19
20
21
# File 'lib/playlist/format/xspf.rb', line 13

def parse(input)
  Playlist.new do |playlist|
    doc = Nokogiri::XML(input)
    playlist.title = doc.content_at('/xmlns:playlist/xmlns:title')
    doc.xpath('/xmlns:playlist/xmlns:trackList/xmlns:track').each do |track|
      playlist.tracks << parse_track(track)
    end
  end
end