Class: Ape::Entry

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Entry

Returns a new instance of Entry.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ape/entry.rb', line 15

def initialize(opts)
  if opts[:xml]
    @element = opts[:xml]
  elsif opts[:text]
    @element = REXML::Document.new(opts[:text], { :raw => nil }).root
  else
    raise(ArgumentError, "Neither :xml nor :text provided")
  end
  
  if opts[:uri]
    @uri = opts[:uri]
    @base = AtomURI.new(@uri)
  else
    @base = @uri = nil
  end
  
  @authoritative = opts[:authoritative] || false
  @etag = opts[:etag] || nil
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Class Method Details

.dump(node, depth = 0) ⇒ Object

debugging



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ape/entry.rb', line 146

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



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

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


130
131
132
133
134
# File 'lib/ape/entry.rb', line 130

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ape/entry.rb', line 89

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



83
84
85
86
87
# File 'lib/ape/entry.rb', line 83

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

#content_srcObject



39
40
41
42
43
44
45
46
47
# File 'lib/ape/entry.rb', line 39

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

#delete_category(c) ⇒ Object



79
80
81
# File 'lib/ape/entry.rb', line 79

def delete_category(c)
  @element.delete_element c
end

#get_child(field, namespace = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ape/entry.rb', line 49

def get_child(field, namespace = nil)
  if (namespace)
    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



70
71
72
73
74
75
76
77
# File 'lib/ape/entry.rb', line 70

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


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

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



136
137
138
# File 'lib/ape/entry.rb', line 136

def summarize
  child_content('title')
end

#text_from(node) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ape/entry.rb', line 105

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



35
36
37
# File 'lib/ape/entry.rb', line 35

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

#xpath_match(xp) ⇒ Object

utility routine



141
142
143
# File 'lib/ape/entry.rb', line 141

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