Class: Atom::Service

Inherits:
Element
  • Object
show all
Defined in:
lib/atom/service.rb

Overview

Atom::Service represents an Atom Publishing Protocol service document. Its only child is #workspaces, which is an Array of Atom::Workspace s

Instance Attribute Summary

Attributes inherited from Element

#base, #extensions

Instance Method Summary collapse

Methods inherited from Element

#[], #[]=, attrb, attrs, define_accessor, element, elements, inherited, #local_name, required, #taguri, #to_element, #to_s, #to_yaml, #to_yaml_properties

Constructor Details

#initialize(service_url = "", http = Atom::HTTP.new) ⇒ Service

retrieves and parses an Atom service document.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/atom/service.rb', line 90

def initialize(service_url = "", http = Atom::HTTP.new)
  super("service")
  
  @http = http

  return if service_url.empty?

  base = URI.parse(service_url)

  rxml = nil

  res = @http.get(base, "Accept" => "application/atomserv+xml")
  res.validate_content_type(["application/atomserv+xml"])

  unless res.code == "200" # XXX needs to handle redirects, &c.
    raise WrongResponse, "service document URL responded with unexpected code #{res.code}"
  end

  parse(res.body, base)
end

Instance Method Details

#parse(xml, base = "") ⇒ Object

parse a service document, adding its workspaces to this object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/atom/service.rb', line 112

def parse xml, base = ""
  rxml = if xml.is_a? REXML::Document
    xml.root
  elsif xml.is_a? REXML::Element
    xml
  else 
    REXML::Document.new(xml)
  end

  unless rxml.root.namespace == PP_NS
    raise WrongNamespace, "this isn't an atom service document!"
  end

  REXML::XPath.match( rxml, "/app:service/app:workspace", {"app" => Atom::PP_NS} ).each do |ws_el|
    self.workspaces << Atom::Workspace.parse(ws_el, base, @http)
  end

  self
end

#to_xmlObject

serialize to a (namespaced) REXML::Document



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/atom/service.rb', line 133

def to_xml
  doc = REXML::Document.new
  
  root = REXML::Element.new "service"
  root.add_namespace Atom::PP_NS
  root.add_namespace "atom", Atom::NS

  self.workspaces.each do |ws|
    root << ws.to_element
  end

  doc << root
  doc
end