Class: Atom::Workspace

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

Overview

an Atom::Workspace has a #title (Atom::Text) and #collections, an Array of Atom::Collection s

Instance Attribute Summary

Attributes inherited from Element

#base, #extensions

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

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

Constructor Details

This class inherits a constructor from Atom::Element

Class Method Details

.parse(xml, base = "", http = Atom::HTTP.new) ⇒ Object

:nodoc:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/atom/service.rb', line 18

def self.parse(xml, base = "", http = Atom::HTTP.new) # :nodoc:
  ws = Atom::Workspace.new("workspace")

  rxml = if xml.is_a? REXML::Document
    xml.root
  elsif xml.is_a? REXML::Element
    xml
  else 
    REXML::Document.new(xml)
  end

  xml.fill_text_construct(ws, "title")

  REXML::XPath.match( rxml, 
                      "./app:collection",
                      {"app" => Atom::PP_NS} ).each do |col_el|
    # absolutize relative URLs
    url = base.to_uri + col_el.attributes["href"].to_uri
   
    coll = Atom::Collection.new(url, http)

    # XXX this is a Text Construct, and should be parsed as such
    col_el.fill_text_construct(coll, "title")

    accepts = REXML::XPath.first( col_el,
                                  "./app:accept",
                                  {"app" => Atom::PP_NS} )
    coll.accepts = (accepts ? accepts.text : "entry")
    
    ws.collections << coll
  end

  ws
end

Instance Method Details

#to_elementObject

:nodoc:



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
78
79
80
# File 'lib/atom/service.rb', line 53

def to_element # :nodoc:
  root = REXML::Element.new "workspace" 

  # damn you, REXML. Damn you and you bizarre handling of namespaces
  title = self.title.to_element
  title.name = "atom:title"
  root << title

  self.collections.each do |coll|
    el = REXML::Element.new "collection"

    el.attributes["href"] = coll.uri

    title = coll.title.to_element
    title.name = "atom:title"
    el << title
   
    unless coll.accepts.nil?
      accepts = REXML::Element.new "accepts"
      accepts.text = coll.accepts
      el << accepts
    end

    root << el
  end

  root
end