Class | Atom::Service |
In: |
lib/atom/service.rb
|
Parent: | Atom::Element |
Atom::Service represents an Atom Publishing Protocol service document. Its only child is workspaces, which is an Array of Atom::Workspace s
retrieves and parses an Atom service document.
# File lib/atom/service.rb, line 92 92: def initialize(service_url = "", http = Atom::HTTP.new) 93: super("service") 94: 95: @http = http 96: 97: return if service_url.empty? 98: 99: base = URI.parse(service_url) 100: 101: rxml = nil 102: 103: res = @http.get(base, "Accept" => "application/atomserv+xml") 104: res.validate_content_type(["application/atomserv+xml"]) 105: 106: unless res.code == "200" # XXX needs to handle redirects, &c. 107: raise WrongResponse, "service document URL responded with unexpected code #{res.code}" 108: end 109: 110: parse(res.body, base) 111: end
parse a service document, adding its workspaces to this object
# File lib/atom/service.rb, line 114 114: def parse xml, base = "" 115: rxml = if xml.is_a? REXML::Document 116: xml.root 117: elsif xml.is_a? REXML::Element 118: xml 119: else 120: REXML::Document.new(xml) 121: end 122: 123: unless rxml.root.namespace == PP_NS 124: raise WrongNamespace, "this isn't an atom service document!" 125: end 126: 127: REXML::XPath.match( rxml, "/app:service/app:workspace", {"app" => Atom::PP_NS} ).each do |ws_el| 128: self.workspaces << Atom::Workspace.parse(ws_el, base, @http) 129: end 130: 131: self 132: end
serialize to a (namespaced) REXML::Document
# File lib/atom/service.rb, line 135 135: def to_xml 136: doc = REXML::Document.new 137: 138: root = REXML::Element.new "service" 139: root.add_namespace Atom::PP_NS 140: root.add_namespace "atom", Atom::NS 141: 142: self.workspaces.each do |ws| 143: root << ws.to_element 144: end 145: 146: doc << root 147: doc 148: end