class XRDS

Attributes

xml[R]

Public Class Methods

_load(s) click to toggle source
# File lib/yadis/xrds.rb, line 47
def XRDS._load(s)
  XRDS.new(s)
end
new(xml_text) click to toggle source

Create a new XRDS object. Raises ArgumentError if xml_text is malformed or invalid XRDS.

# File lib/yadis/xrds.rb, line 38
def initialize(xml_text)
  @xml_text = xml_text
  parse_xml(xml_text)
end
parse(xml) click to toggle source

Method for producing a valid XRDS object. Accepts an XML String. Returns an XRDS object on success, or nil on failure. Same as calling ::new, but does not raise ArgumentErrors.

# File lib/yadis/xrds.rb, line 28
def XRDS.parse(xml)
  begin
    return new(xml)
  rescue
    return nil
  end
end

Public Instance Methods

_dump(depth) click to toggle source
# File lib/yadis/xrds.rb, line 43
def _dump(depth)
  return @xml_text
end
parse_xml(xml_text) click to toggle source
# File lib/yadis/xrds.rb, line 51
def parse_xml(xml_text)   
  begin
    xml = @xml = REXML::Document.new(xml_text)
  rescue
    raise ArgumentError, "Can't parse XRDS"
  end
  
  if xml.root.nil?
    raise ArgumentError, "No document root"
  end

  xrd = self.last_xrd(xml.root)
  raise ArgumentError, "No XRD Elements found" if xrd.nil?
 
  @services = {}  # keyed by [service_priority, uri_priority]
  REXML::XPath.each(xrd, 'xrdns:Service', @@namespaces) do |s|
    _create_services(s)
  end
  
  REXML::XPath.each(xrd, 'xrdns:CanonicalID', @@namespaces) do |c|
    canonical_id = c.text.strip
    if canonical_id.length > 0
      self.services.each {|s| s.canonical_id = canonical_id}
    end
  end

end
services() click to toggle source

Returns an Array of ServiceEndpoint objects, sorted by priority. Highest priority is at element 0.

# File lib/yadis/xrds.rb, line 82
def services
  s = []

  @services.keys.sort.each do |key|
    services_list = @services[key].dup

    # randomize services with the same priority
    while services_list.length > 0
      s << services_list.delete_at((rand * services_list.length).to_i)
    end

  end
  
  return s
end