Class: XRDS

Inherits:
Object
  • Object
show all
Includes:
XRDSUtil
Defined in:
lib/yadis/xrds.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XRDSUtil

#last_xrd

Constructor Details

#initialize(xml_text) ⇒ XRDS

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



38
39
40
41
# File 'lib/yadis/xrds.rb', line 38

def initialize(xml_text)
  @xml_text = xml_text
  parse_xml(xml_text)
end

Instance Attribute Details

#xmlObject (readonly)

Returns the value of attribute xml.



23
24
25
# File 'lib/yadis/xrds.rb', line 23

def xml
  @xml
end

Class Method Details

._load(s) ⇒ Object



47
48
49
# File 'lib/yadis/xrds.rb', line 47

def XRDS._load(s)
  XRDS.new(s)
end

.parse(xml) ⇒ Object

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



28
29
30
31
32
33
34
# File 'lib/yadis/xrds.rb', line 28

def XRDS.parse(xml)
  begin
    return new(xml)
  rescue
    return nil
  end
end

Instance Method Details

#_dump(depth) ⇒ Object



43
44
45
# File 'lib/yadis/xrds.rb', line 43

def _dump(depth)
  return @xml_text
end

#parse_xml(xml_text) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

#servicesObject

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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