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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/playlist/format/xspf.rb', line 32

def generate(playlist)
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.playlist(:version => 1, :xmlns => 'http://xspf.org/ns/0/') do
      build_xml_unless_nil(
        xml,
        :title => playlist.title,
        :creator => playlist.creator,
        :annotation => playlist.description,
        :image => playlist.image,
        :info => playlist.info_url
      )
      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
22
23
24
25
26
27
# 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')
    playlist.creator = doc.content_at('/xmlns:playlist/xmlns:creator')
    playlist.description = doc.content_at(
      '/xmlns:playlist/xmlns:annotation'
    )
    playlist.image = doc.content_at('/xmlns:playlist/xmlns:image')
    playlist.info_url = doc.content_at('/xmlns:playlist/xmlns:info')
    doc.xpath('/xmlns:playlist/xmlns:trackList/xmlns:track').each do |track|
      playlist.tracks << parse_track(track)
    end
  end
end