Class: Saper::Items::XML

Inherits:
Saper::Item show all
Defined in:
lib/saper/items/xml.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Saper::Item

[], exists?, inherited, #serialize, subclasses, try, #type, type

Constructor Details

#initialize(noko) ⇒ XML

TODO:

Returns a new instance of XML


40
41
42
43
44
45
# File 'lib/saper/items/xml.rb', line 40

def initialize(noko)
  @noko = noko
  # Force UTF-8 encoding
  # https://github.com/sparklemotion/nokogiri/issues/117
  @noko.document.encoding = 'UTF-8'
end

Class Method Details

.new(item) ⇒ Object

TODO:

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/saper/items/xml.rb', line 6

def self.new(item)
  super case item
  when Nokogiri::XML
    item
  when Nokogiri::XML::Document
    item
  when Nokogiri::XML::Element
    item
  when Text
    parse(item.to_s)
  when String
    parse(item)
  else
    raise(Errors::InvalidItem, item)
  end
end

.parse(string, uri = nil, charset = nil) ⇒ Object

TODO:

24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/saper/items/xml.rb', line 24

def self.parse(string, uri = nil, charset = nil)
  if string.empty?
    raise(Errors::InvalidItem, string)
  end
  begin
    noko = Nokogiri::XML.parse(string, uri, charset)
  rescue
    raise(Errors::InvalidItem, string)
  end
  if noko.root.nil?
    raise(Errors::InvalidItem, string)
  end
  noko
end

Instance Method Details

#==(other) ⇒ Object

TODO:

53
54
55
# File 'lib/saper/items/xml.rb', line 53

def ==(other)
  to_s == other.to_s
end

#[](name) ⇒ Object

TODO:

63
64
65
# File 'lib/saper/items/xml.rb', line 63

def [](name)
  @noko.has_attribute?(name) ? Text.new(@noko[name]) : nil
end

#dupObject

TODO:

48
49
50
# File 'lib/saper/items/xml.rb', line 48

def dup
  XML.new(@noko.dup)
end

#find(xpath) ⇒ Object

TODO:

68
69
70
# File 'lib/saper/items/xml.rb', line 68

def find(xpath)
  find_all(xpath).first
end

#find_all(xpath) ⇒ Object

TODO:

73
74
75
# File 'lib/saper/items/xml.rb', line 73

def find_all(xpath)
  @noko.search(xpath).map { |element| XML.new(element) }
end

#inner_textObject

TODO:

103
104
105
# File 'lib/saper/items/xml.rb', line 103

def inner_text
  Text.new @noko.inner_text
end

#inner_xmlObject

TODO:

98
99
100
# File 'lib/saper/items/xml.rb', line 98

def inner_xml
  Text.new @noko.inner_html
end

#nameObject

TODO:

58
59
60
# File 'lib/saper/items/xml.rb', line 58

def name
  @noko.name
end

#remove_children_preserving_content(xpath) ⇒ Object

TODO:

93
94
95
# File 'lib/saper/items/xml.rb', line 93

def remove_children_preserving_content(xpath)
  dup.remove_children_preserving_content!(xpath)
end

#remove_children_preserving_content!(xpath) ⇒ Object

TODO:

88
89
90
# File 'lib/saper/items/xml.rb', line 88

def remove_children_preserving_content!(xpath)
  @noko.search(xpath).each { |item| item.replace(item.children) }; self
end

#remove_children_with_content(xpath) ⇒ Object

TODO:

83
84
85
# File 'lib/saper/items/xml.rb', line 83

def remove_children_with_content(xpath)
  dup.remove_children_with_content!(xpath)
end

#remove_children_with_content!(xpath) ⇒ Object

TODO:

78
79
80
# File 'lib/saper/items/xml.rb', line 78

def remove_children_with_content!(xpath)
  @noko.search(xpath).each { |item| item.remove }; self
end

#to_nativeObject

TODO:

108
109
110
# File 'lib/saper/items/xml.rb', line 108

def to_native
  inner_xml.to_native
end

#to_sObject

TODO:

113
114
115
# File 'lib/saper/items/xml.rb', line 113

def to_s
  @noko.is_a?(Nokogiri::XML::Document) ? to_native : @noko.to_s
end