Class: XSPF::Playlist

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

Direct Known Subclasses

Tracklist

Constant Summary collapse

ATTRIBUTES =

:stopdoc:

%w{ xmlns version }
ELEMENTS =
%w{ title creator annotation info location identifier image date license attribution extension }
ATTRIBUTE_AND_ELEMENT =
%w{ link meta }
ATTRIBUTION_CHILD_ELEMENTS =
%w{ location identifier }
EXTENSION_CHILD_ELEMENTS =
%w{ application content }
XMLNS_RDOC =
'The XML namespace. It must be http://xspf.org/ns/0/ for a valid XSPF document.'
XMLNS_DEFAULT =
'http://xspf.org/ns/0/'
VERSION_RDOC =
'The XSPF version. It may be 0 or 1, although 1 is strongly advised.'
VERSION_DEFAULT =
'1'
TITLE_RDOC =
'A human-readable title for the playlist. xspf:playlist elements MAY contain exactly one.'
CREATOR_RDOC =
'Human-readable name of the entity (author, authors, group, company, etc) that authored the playlist. XSPF::Playlist objects MAY contain exactly one.'
ANNOTATION_RDOC =
'A human-readable comment on the playlist. This is character data, not HTML, and it may not contain markup. XSPF::Playlist objects elements MAY contain exactly one.'
INFO_RDOC =
'URL of a web page to find out more about this playlist. Likely to be homepage of the author, and would be used to find out more about the author and to find more playlists by the author. XSPF::Playlist objects MAY contain exactly one.'
LOCATION_RDOC =
'Source URL for this playlist. XSPF::Playlist objects MAY contain exactly one.'
IDENTIFIER_RDOC =
'Canonical ID for this playlist. Likely to be a hash or other location-independent name. MUST be a legal URN. XSPF::Playlist objects MAY contain exactly one.'
IMAGE_RDOC =
'URL of an image to display if XSPF::Playlist#image return nil. XSPF::Playlist objects MAY contain exactly one.'
DATE_RDOC =
'Creation date (not last-modified date) of the playlist, formatted as a XML schema dateTime. XSPF::Playlist objects MAY contain exactly one.'
LICENSE_RDOC =
'URL of a resource that describes the license under which this playlist was released. XSPF::Playlist objects MAY contain zero or one license element.'
ATTRIBUTION_RDOC =
'An ordered list of URIs. The purpose is to satisfy licenses allowing modification but requiring attribution. If you modify such a playlist, move its XSPF::Playlist#location or XSPF::Playlist#identifier element to the top of the items in the XSPF::Playlist#attribution element. XSPF::Playlist objects MAY contain exactly one attribution element. Please note that currently XSPF for Ruby does not parse the contents of XSPF::Playlist#attribution.'
EXTENSION_RDOC =
'The extension element allows non-XSPF XML to be included in XSPF documents without breaking XSPF validation. The purpose is to allow nested XML, which the meta and link elements do not. XSPF::Playlist objects MAY contain zero or more extension elements but currently XSPF for Ruby returns only the first one.'
'The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid _link_ element has a _rel_ attribute and a _content_ element, obtained with XSPF::Playlist#link_rel and XSPF::Playlist#link_content respectively. XSPF::Playlist objects MAY contain zero or more link elements, but currently XSPF for Ruby returns only the first one.'
'The link element allows non-XSPF web resources to be included in XSPF documents without breaking XSPF validation. A valid _link_ element has a _rel_ attribute and a _content_ element, obtained with XSPF::Playlist#link_rel and XSPF::Playlist#link_content respectively. XSPF::Playlist objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one.'
META_REL_RDOC =
'The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid _meta_ element has a _rel_ attribute and a _content_ element, obtained with XSPF::Playlist#meta_rel and XSPF::Playlist#meta_content respectively. XSPF::Playlist objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one.'
META_CONTENT_RDOC =
'The meta element allows non-XSPF metadata to be included in XSPF documents without breaking XSPF validation. A valid _meta_ element has a _rel_ attribute and a _content_ element, obtained with XSPF::Playlist#meta_rel and XSPF::Playlist#meta_content respectively. XSPF::Playlist objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one.'

Constants inherited from XSPF

ENCODING_RDOC, HTML_RDOC, M3U_RDOC, OUTPUT_FORMATS, RDF_RDOC, SMIL_RDOC, SOUNDBLOX_RDOC

Instance Attribute Summary collapse

Attributes inherited from XSPF

#xspf

Instance Method Summary collapse

Methods inherited from XSPF

#playlist=

Constructor Details

#initialize(source = nil) ⇒ Playlist

Creates a XSPF::Playlist from a XSPF document (parse mode) or from a hash of values (generation mode)

Possible keys in the hash: :xmlns, :version, :title, :creator, :annotation, :info, :location, :identifier, :image, :date, :license, :attribution, :extension, :link_rel, :link_content, :meta_rel, :meta_content



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/xspf.rb', line 226

def initialize(source = nil)

  if ( source.instance_of?(Hash) || source.nil? ) then

    ATTRIBUTES.each do |attrib|
      add_instance_variable(source, attrib)
    end

    ELEMENTS.each do |element|
      add_instance_variable(source, element)
    end

    ATTRIBUTE_AND_ELEMENT.each do |ae|
      add_instance_variable(source, "#{ae}_content" )
      add_instance_variable(source, "#{ae}_rel" )
    end

    @tracklist = if ( !source.nil? && source.has_key?(:tracklist) && source[:tracklist].instance_of?(XSPF::Tracklist) )
                    source[:tracklist]
                  else
                    nil
                  end

  elsif source.instance_of?(XSPF) then

    @playlist = source.playlist_xml

    ATTRIBUTES.each do |attrib|
      eval('@' + attrib.downcase + '= parse_' + attrib.downcase)
    end

    ELEMENTS.each do |element|
      eval('@' + element.downcase + '= parse_' + element.downcase)
    end

    ATTRIBUTE_AND_ELEMENT.each do |ae|
      eval('@' + ae.downcase + '_content = parse_' + ae.downcase + '_content')
      eval('@' + ae.downcase + '_rel = parse_' + ae.downcase + '_rel')
    end

    @tracklist = XSPF::Tracklist.new(self)

  else
    raise(TypeError, 'You must pass a XSPF object (parsing mode) or a hash (generator mode) as argument to XSPF::Playlist#new')
  end
  
end

Instance Attribute Details

#playlistObject (readonly)

Returns the value of attribute playlist.



172
173
174
# File 'lib/xspf.rb', line 172

def playlist
  @playlist
end

Instance Method Details

#to_xmlObject

Exports the XSPF::Playlist to XML (only the <playlist> section)



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/xspf.rb', line 287

def to_xml

  xml = REXML::Element.new('playlist')

  ATTRIBUTES.each do |attrib|
    # TODO Sure there is a nicer way to do evaluate this condition...
    unless eval('@' + attrib.downcase + '.nil?')
      xml.attributes[attrib] = eval('@' + attrib.downcase)
    end 
  end
  
  ELEMENTS.each do |element|
    # TODO Sure there is a nicer way to do evaluate this condition...
    unless eval('@' + element.downcase + '.nil?')
      el = REXML::Element.new(element)
      el.add_text( eval('@' + element.downcase) )
      xml.add_element(el)
    end 
  end

  ATTRIBUTE_AND_ELEMENT.each do |ae|
    # TODO Sure there is a nicer way to do evaluate this condition...
    unless eval('@' + ae.downcase + '_rel.nil? && @'+ ae.downcase + '_content.nil?')
      el = REXML::Element.new(ae.downcase)
      el.add_attribute('rel', eval('@' + ae.downcase + '_rel') )
      el.add_text( eval('@' + ae.downcase + '_content') )
      xml.add_element(el)
    end 
  end

  xml << REXML::Document.new(@tracklist.to_xml)
  
  xml.to_s

end

#tracklistObject

A XSPF::Tracklist object



275
276
277
# File 'lib/xspf.rb', line 275

def tracklist
  @tracklist
end

#tracklist=(value) ⇒ Object Also known as: <<

Raises:

  • (TypeError)


279
280
281
282
# File 'lib/xspf.rb', line 279

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