Class: Ape::CollElement

Inherits:
Object
  • Object
show all
Defined in:
lib/ape/coll_element.rb

Constant Summary collapse

@@mime_re =
%r{^(.*)/(.*)$}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(el, doc_uri = nil) ⇒ CollElement

Returns a new instance of CollElement.

Raises:

  • (SyntaxError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ape/coll_element.rb', line 18

def initialize(el, doc_uri = nil)
  @xml = el
  @accept = []
  @title = REXML::XPath.first(el, './atom:title', Names::XmlNamespaces)

  # sigh, RNC validation *should* take care of this
  raise(SyntaxError, "Collection is missing required 'atom:title'") unless @title
  @title = @title.texts.join

  if doc_uri
    uris = AtomURI.new(doc_uri)
    @href = uris.absolutize(el.attributes['href'], el)
  else
    @href = el.attributes['href']
  end

  # now we have to go looking for the accept
  @accept = REXML::XPath.match(@xml, './app:accept/(text)', Names::XmlNamespaces)
  @accept = [ Names::AtomEntryMediaType ] if @accept.empty?
end

Instance Attribute Details

#acceptObject (readonly)

Returns the value of attribute accept.



11
12
13
# File 'lib/ape/coll_element.rb', line 11

def accept
  @accept
end

#hrefObject (readonly)

Returns the value of attribute href.



11
12
13
# File 'lib/ape/coll_element.rb', line 11

def href
  @href
end

#titleObject (readonly)

Returns the value of attribute title.



11
12
13
# File 'lib/ape/coll_element.rb', line 11

def title
  @title
end

#xmlObject (readonly)

Returns the value of attribute xml.



11
12
13
# File 'lib/ape/coll_element.rb', line 11

def xml
  @xml
end

Class Method Details

.find_colls(source, doc_uri = nil) ⇒ Object



13
14
15
16
# File 'lib/ape/coll_element.rb', line 13

def CollElement::find_colls(source, doc_uri = nil)
  els = REXML::XPath.match(source, '//app:collection', Names::XmlNamespaces)
  els.map { |el| CollElement.new(el, doc_uri) }
end

Instance Method Details

#accept?(mime_type) ⇒ Boolean

check if the collection accepts a given mime type; watch out for wildcards

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ape/coll_element.rb', line 40

def accept?(mime_type)
  if mime_type =~ @@mime_re
    p1, p2 = $1, $2
  else
    return false # WTF?
  end
  @accept.each do |pat|
    pat = pat.to_s # working around REXML ticket 164
    if pat =~ @@mime_re
      if ($1 == p1 || $1 == "*") && ($2 == p2 || $2 == "*")
        return true
      elsif ((pat == Names::AtomMediaType && mime_type == Names::AtomFeedMediaType) ||
            (pat == Names::AtomFeedMediaType && mime_type == Names::AtomMediaType))
        return true  
      end
    end
  end
  return false
end

#catsesObject

the name is supposed to suggest multiple instances of “categories”



61
62
63
# File 'lib/ape/coll_element.rb', line 61

def catses
  REXML::XPath.match(@xml, './app:categories', Names::XmlNamespaces)
end