Class: Ape::Entry

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, uri = nil) ⇒ Entry

Returns a new instance of Entry.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ape/entry.rb', line 14

def initialize(node, uri = nil)
  if node.class == String
    @element = REXML::Document.new(node, { :raw => nil }).root
  else
    @element = node
  end
  if uri
    @base = AtomURI.new(uri)
  else
    @base = nil
  end
end

Class Method Details

.dump(node, depth = 0) ⇒ Object

debugging



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ape/entry.rb', line 139

def Entry.dump(node, depth=0)
  prefix = '.' * depth
  name = node.getNodeName
  uri = node.getNamespaceURI
  if uri
    puts "#{prefix} #{uri}:#{node.getNodeName}"
  else
    puts "#{prefix} #{node.getNodeName}"
  end
  Nodes.each_node(node.getChildNodes) {|child| dump(child, depth+1)}
end

Instance Method Details

#add_category(term, scheme = nil, label = nil) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/ape/entry.rb', line 54

def add_category(term, scheme = nil, label = nil)
  c = REXML::Element.new('atom:category', @element)
  c.add_namespace('atom', Names::AtomNamespace)
  c.add_attribute('term', term)
  c.add_attribute('scheme', scheme) if scheme
  c.add_attribute('label', label) if label
  c
end


123
124
125
126
127
# File 'lib/ape/entry.rb', line 123

def alt_links
  REXML::XPath.match(@element, "./atom:link", Names::XmlNamespaces).select do |l|
    l.attributes['rel'] == nil || l.attributes['rel'] == 'alternate'
  end
end

#child_content(field, namespace = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ape/entry.rb', line 82

def child_content(field, namespace = nil)
  n = get_child(field, namespace)
  return nil unless n
  
  # if it's type="xhtml", we'll get the content out of the contained
  #  XHTML <div> rather than this element
  if n.attributes['type'] == 'xhtml'
    n = REXML::XPath.first(n, "./xhtml:div", Names::XmlNamespaces)
    unless n
      return "Error: required xhtml:div child of #{field} is missing"
    end
  end 

  text_from n
end

#child_type(field) ⇒ Object



76
77
78
79
80
# File 'lib/ape/entry.rb', line 76

def child_type(field)
  n = get_child(field, nil)
  return nil unless n
  return n.attributes['type'] || "text"
end

#content_srcObject



31
32
33
34
35
36
37
38
39
# File 'lib/ape/entry.rb', line 31

def content_src
  content = REXML::XPath.first(@element, './atom:content', Names::XmlNamespaces)
  if content
    cs = content.attributes['src']
    cs = @base.absolutize(cs, @element) if @base
  else
    nil
  end
end

#delete_category(c) ⇒ Object



72
73
74
# File 'lib/ape/entry.rb', line 72

def delete_category(c)
  @element.delete_element c
end

#get_child(field, namespace = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ape/entry.rb', line 41

def get_child(field, namespace = nil)
  if (namespace)
    thisNS = {}
    prefix = 'NN'
    thisNS[prefix] = namespace
  else
    prefix = 'atom'
    thisNS = Names::XmlNamespaces
  end
  xpath = "./#{prefix}:#{field}"
  return REXML::XPath.first(@element, xpath, thisNS)
end

#has_cat(cat) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/ape/entry.rb', line 63

def has_cat(cat)
  xpath = "./atom:category[@term=\"#{cat.attributes['term']}\""
  if cat.attributes['scheme']
    xpath += "and @scheme=\"#{cat.attributes['scheme']}\""
  end
  xpath += "]"
  REXML::XPath.first(@element, xpath, Names::XmlNamespaces)
end


113
114
115
116
117
118
119
120
121
# File 'lib/ape/entry.rb', line 113

def link(rel, ape=nil)
  l = nil
  a = REXML::XPath.first(@element, "./atom:link[@rel=\"#{rel}\"]", Names::XmlNamespaces)
  if a
    l = a.attributes['href']
    l = @base.absolutize(l, @element) if @base
  end
  l
end

#summarizeObject



129
130
131
# File 'lib/ape/entry.rb', line 129

def summarize
  child_content('title')
end

#text_from(node) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ape/entry.rb', line 98

def text_from node
  text = ''
  is_html = node.name =~ /(rights|subtitle|summary|title|content)$/ && node.attributes['type'] == 'html'
  node.find_all do | child |
    if child.kind_of? REXML::Text
      v = child.value
      v = CGI.unescapeHTML(v).gsub(/&apos;/, "'") if is_html
      text << v
    elsif child.kind_of? REXML::Element
      text << text_from(child)
    end
  end
  text
end

#to_sObject



27
28
29
# File 'lib/ape/entry.rb', line 27

def to_s
  "<?xml version='1.0' ?>\n" + @element.to_s
end

#xpath_match(xp) ⇒ Object

utility routine



134
135
136
# File 'lib/ape/entry.rb', line 134

def xpath_match(xp)
  REXML::XPath.match(@element, xp, Names::XmlNamespaces)
end