Class: TinyXPathHelper

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, options = {}) ⇒ TinyXPathHelper

Returns a new instance of TinyXPathHelper.



6
7
8
9
10
# File 'lib/tiny_xpath_helper.rb', line 6

def initialize(xml, options = {})
  @xml  = xml
  @node = self.class.xml_node_for_xmlish(xml, options)
  @options = options.freeze
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



4
5
6
# File 'lib/tiny_xpath_helper.rb', line 4

def node
  @node
end

Class Method Details

.classes_that_are_xmlishObject



47
48
49
# File 'lib/tiny_xpath_helper.rb', line 47

def self.classes_that_are_xmlish
  io_stream_classes + [ String, REXML::Document ]
end

.default_optionsObject



80
81
82
# File 'lib/tiny_xpath_helper.rb', line 80

def self.default_options
  {:format => nil, :auto_text => true, :find => :first}.freeze
end

.find_xpath_from(element, path, options = {}, &blk) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tiny_xpath_helper.rb', line 84

def self.find_xpath_from(element, path, options = {}, &blk)
  if options[:with_indifferent_access]
    path = path.to_s
  end

  options = self.default_options.dup.update(options)
  format    = options[:format]
  auto_text = options[:auto_text]
  count     = options[:find]

  if format == :array
    count     = :all
    format    = nil
  elsif format == :text
    auto_text = true
    format    = nil
  elsif format == :xml or format == :rexml
    auto_text = false
    format    = nil
  elsif format == :xpath_helper or format == :tiny_xpath_helper
    auto_text = false
    format    = self.method(:new)
  end

  filter1 = auto_text ? self.method(:xml_node_to_text) : nil
  filter2 = format

  if count   == :all
    elements = REXML::XPath.match(element, path)

  elsif count == :first
    elements  = [ REXML::XPath.first(element, path) ].compact # Haskell hacker wishing for the Maybe monad

  else 
    raise "I don't know how to find #{count.inspect}"
  end

  elements = elements.map(&filter1).map(&filter2)

  if count == :all
    r = elements
  elsif count == :first
    r = elements.first
  end

  if(blk and elements.length > 0)
    return blk.call( r )
  end

  return r

end

.io_stream_classesObject



43
44
45
# File 'lib/tiny_xpath_helper.rb', line 43

def self.io_stream_classes
  [ IOStream ] rescue [ IO, StringIO ] # thoughtbot-paperclip fixes the ducktype mess in StringIO
end

.xml_node_for_xmlish(xml, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tiny_xpath_helper.rb', line 51

def self.xml_node_for_xmlish( xml, options = {} )
  if io_stream_classes.any?{|k| xml.is_a?(k) }
    xml = xml.read
  end
  if xml.is_a?(String)
    xml = REXML::Document.new(xml)
  end
  if xml.is_a?(REXML::Document)
    if options[:allow_empty_document] and ! xml.root
      xml = xml
    else
      xml = xml.root
    end
  end
  if not xml.is_a?(REXML::Element)
    raise TypeError.new("Expected REXML::Element, got #{xml.class}")
  end
  return xml
end

.xml_node_to_text(node) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/tiny_xpath_helper.rb', line 71

def self.xml_node_to_text(node)
  if(node.respond_to? :text)
    # XML::Elements don't to_s in the way we want
    val = node.text
  else
    val = node.to_s
  end
end

Instance Method Details

#all(xpath_expr, options = {}, &blk) ⇒ Object Also known as: []



38
39
40
# File 'lib/tiny_xpath_helper.rb', line 38

def all(xpath_expr, options = {}, &blk)
  self.find_xpath( xpath_expr, with_options(options, :find => :all), &blk)
end

#default_optionsObject



12
13
14
# File 'lib/tiny_xpath_helper.rb', line 12

def default_options
  self.class.default_options.dup.update(@options)
end

#find_xpath(xpath_expr, options = {}, &blk) ⇒ Object



29
30
31
# File 'lib/tiny_xpath_helper.rb', line 29

def find_xpath(xpath_expr, options = {}, &blk)
  self.class.find_xpath_from( node, xpath_expr, with_options(options), &blk)
end

#first(xpath_expr, options = {}, &blk) ⇒ Object Also known as: at



33
34
35
# File 'lib/tiny_xpath_helper.rb', line 33

def first(xpath_expr, options = {}, &blk)
  self.find_xpath( xpath_expr, with_options(options, :find => :first), &blk)
end

#with_options(*options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tiny_xpath_helper.rb', line 16

def with_options(*options)
  r = default_options.dup

  options.reverse.each do |option|
    if not option.is_a?( Hash )
      option = {:format => option}
    end
    r.update(option)
  end

  return r
end