Class: XSPF

Inherits:
Object
  • Object
show all
Defined in:
lib/xspf.rb

Direct Known Subclasses

Playlist

Defined Under Namespace

Classes: Playlist, Track, Tracklist

Constant Summary collapse

ATTRIBUTES =

:stopdoc:

%w{ version encoding }
VERSION_RDOC =
'Version for the XML document or _nil_ if not defined'
ENCODING_RDOC =
'Encoding of the XML document or _nil_ if not defined'
OUTPUT_FORMATS =
%w{ m3u html smil rdf soundblox }
M3U_RDOC =
'Creates a .m3u playlist from the XSPF document. This method makes use of the official XSPF to M3U XSLT transformation by Lucas Gonze.'
HTML_RDOC =
'Outputs the playlist as an HTML page. This method makes use of the official XSPF to HTML XSLT transformation by Lucas Gonze.'
SMIL_RDOC =
'Creates a .smil playlist from the XSPF document. This method makes use of the official XSPF to SMIL XSLT transformation by Lucas Gonze.'
SOUNDBLOX_RDOC =
'Creates a SoundBlox playlist from the XSPF document. This method makes use of the official XSPF to SoundBlox XSLT tranformation by Lucas Gonze.'
RDF_RDOC =
'Creates a RDF feed from the XSPF document. This method makes use of the XSPF to RDF XSLT transformation by Libby Miller.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ XSPF

Creates a XSPF object from a file or string (parse mode) or from a hash or nil (generation mode).

Possible keys in the hash: :version, :encoding



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/xspf.rb', line 110

def initialize(source = nil)
  if ( source.nil? || source.instance_of?(Hash) ) then
      @version = if source.nil? || !source.has_key?(:version)
                   '1.0'
                 else
                   source[:version]
                 end
      @encoding = if source.nil? || !source.has_key?(:encoding)
                    'UTF-8'
                  else
                    source[:encoding]
                  end
      @playlist = nil
      @playlist = if !source.nil? && source.has_key?(:playlist) then
                      if source[:playlist].instance_of?(XSPF::Playlist)
                          source[:playlist]
                      else
                        raise(TypeError, 'You must pass a file/string (parsing mode) or a hash/nothing (generator mode) as argument to XSPF#new')
                      end
                  end

  elsif ( source.instance_of?(File) || source.instance_of?(String) ) then
      @xspf = REXML::Document.new(source)
      ATTRIBUTES.each do |attrib|
        eval('@' + attrib + '= parse_' + attrib)
      end

      @playlist = XSPF::Playlist.new(self)
      
  else
    raise(TypeError, 'You must pass a file/string (parsing mode) or a hash/nothing (generator mode) as argument to XSPF#new')
  end
end

Instance Attribute Details

#xspfObject (readonly)

Returns the value of attribute xspf.



83
84
85
# File 'lib/xspf.rb', line 83

def xspf
  @xspf
end

Instance Method Details

#playlistObject

A XSPF::Playlist object



145
146
147
# File 'lib/xspf.rb', line 145

def playlist
  @playlist
end

#playlist=(value) ⇒ Object

Raises:

  • (TypeError)


149
150
151
152
# File 'lib/xspf.rb', line 149

def playlist=(value)
  raise(TypeError, 'The playlist must be an instance of XSPF::Playlist') unless value.instance_of?(XSPF::Playlist)
  @playlist = value
end

#to_xmlObject

Exports the XSPF object to XML



155
156
157
158
159
160
# File 'lib/xspf.rb', line 155

def to_xml
  xml = REXML::Document.new
  xml << REXML::XMLDecl.new(@version, @encoding)
  xml << REXML::Document.new(@playlist.to_xml) unless @playlist.nil?
  xml.to_s
end