Class: XSPF::Track

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

Constant Summary collapse

ELEMENTS =

:stopdoc:

%w{ location identifier title creator annotation info image album trackNum duration extension }
ATTRIBUTE_AND_ELEMENT =
%w{ link meta }
LOCATION_RDOC =
'URL of resource to be rendered. Probably an audio resource, but MAY be any type of resource with a well-known duration, such as video, a SMIL document, or an XSPF document. The duration of the resource defined in this element defines the duration of rendering. XSPF::Track objects MAY contain zero or more location elements, but a user-agent MUST NOT render more than one of the named resources. Currently, XSPF for Ruby returns only the first location.'
IDENTIFIER_RDOC =
'Canonical ID for this resource. Likely to be a hash or other location-independent name, such as a MusicBrainz identifier or isbn URN (if there existed isbn numbers for audio). MUST be a legal URN. XSPF::Track objects elements MAY contain zero or more identifier elements, but currently XSPF for Ruby returns only the first one.'
TITLE_RDOC =
'Human-readable name of the track that authored the resource which defines the duration of track rendering. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one.'
CREATOR_RDOC =
'Human-readable name of the entity (author, authors, group, company, etc) that authored the resource which defines the duration of track rendering. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one.'
ANNOTATION_RDOC =
'A human-readable comment on the track. This is character data, not HTML, and it may not contain markup. XSPF::Track objects MAY contain exactly one.'
INFO_RDOC =
'URL of a place where this resource can be bought or more info can be found.'
IMAGE_RDOC =
'URL of an image to display for the duration of the track. XSPF::Track objects MAY contain exactly one.'
ALBUM_RDOC =
'Human-readable name of the collection from which the resource which defines the duration of track rendering comes. For a song originally published as a part of a CD or LP, this would be the title of the original release. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one.'
TRACKNUM_RDOC =
'Integer with value greater than zero giving the ordinal position of the media on the XSPF::Track#album. This value is primarily for fuzzy lookups, though a user-agent may display it. XSPF::Track objects MAY contain exactly one. It MUST be a valid XML Schema nonNegativeInteger.'
DURATION_RDOC =
'The time to render a resource, in milliseconds. It MUST be a valid XML Schema nonNegativeInteger. This value is only a hint -- different XSPF generators will generate slightly different values. A user-agent MUST NOT use this value to determine the rendering duration, since the data will likely be low quality. XSPF::Track objects MAY contain exactly one duration element.'
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::Track 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::Track#link_rel and XSPF::Track#link_content respectively. XSPF::Track 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::Track#link_rel and XSPF::Track#link_content respectively. XSPF::Track 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::Track#meta_rel and XSPF::Track#meta_content respectively. XSPF::Track 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::Track#meta_rel and XSPF::Track#meta_content respectively. XSPF::Track objects MAY contain zero or more meta elements, but currently XSPF for Ruby returns only the first one.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tr) ⇒ Track

Creates a XSPF::Track object from a <track> section of the XSPF document or from a hash of values

Possible keys in the hash in generation mode: :location, :identifier, :title, :creator, :annotation, :info, :image, :album, :tracknum, :duration, :extension, :link_rel, :link_content, :meta_rel, :meta_content)



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/xspf.rb', line 424

def initialize(tr)
  
  if tr.instance_of?(Hash)

    ELEMENTS.each do |element|
      add_instance_variable(tr, element.downcase)
    end

    ATTRIBUTE_AND_ELEMENT.each do |ae|
      add_instance_variable(tr, "#{ae.downcase}_content" )
      add_instance_variable(tr, "#{ae.downcase}_rel" )
    end
    
  else
    @track = tr

    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
  end
  
end

Instance Attribute Details

#trackObject (readonly)

Returns the value of attribute track.



388
389
390
# File 'lib/xspf.rb', line 388

def track
  @track
end

Instance Method Details

#to_xmlObject

Exports the XSPF::Track to XML (only the <track> section)



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/xspf.rb', line 453

def to_xml

  xml = REXML::Element.new('track')
  
  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.to_s
  
end