Class: XRDS
Instance Attribute Summary collapse
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Class Method Summary collapse
- ._load(s) ⇒ Object
-
.parse(xml) ⇒ Object
Method for producing a valid XRDS object.
Instance Method Summary collapse
- #_dump(depth) ⇒ Object
-
#initialize(xml_text) ⇒ XRDS
constructor
Create a new XRDS object.
- #parse_xml(xml_text) ⇒ Object
-
#services ⇒ Object
Returns an Array of ServiceEndpoint objects, sorted by priority.
Methods included from XRDSUtil
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
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
23 24 25 |
# File 'lib/yadis/xrds.rb', line 23 def xml @xml end |
Class Method Details
.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
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 |
#services ⇒ Object
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 |