Class: Skyscraper::Node
- Inherits:
-
Object
show all
- Defined in:
- lib/skyscraper/node.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(element) ⇒ Node
Returns a new instance of Node.
7
8
9
|
# File 'lib/skyscraper/node.rb', line 7
def initialize element
@element = element
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
89
90
91
|
# File 'lib/skyscraper/node.rb', line 89
def method_missing name
@element.attribute(name.to_s).to_s
end
|
Instance Attribute Details
#element ⇒ Object
Returns the value of attribute element.
5
6
7
|
# File 'lib/skyscraper/node.rb', line 5
def element
@element
end
|
Instance Method Details
#children(selector = nil) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/skyscraper/node.rb', line 21
def children selector = nil
if selector
children = @element.css(selector)
else
children = @element.children
end
children.select do |element|
element.parent == @element and element.is_a?(Nokogiri::XML::Element)
end.map do |child|
Node.new(child)
end
end
|
#class ⇒ Object
77
78
79
|
# File 'lib/skyscraper/node.rb', line 77
def class
@element.attribute("class").to_s
end
|
#download(options = {}) ⇒ Object
81
82
83
|
# File 'lib/skyscraper/node.rb', line 81
def download options = {}
Resource.new(self).download(options)
end
|
#find(selector) ⇒ Object
15
16
17
18
19
|
# File 'lib/skyscraper/node.rb', line 15
def find selector
@element.css(selector).map do |element|
Node.new(element)
end
end
|
#first(selector) ⇒ Object
11
12
13
|
# File 'lib/skyscraper/node.rb', line 11
def first selector
self.find(selector).first
end
|
#follow ⇒ Object
67
68
69
70
71
|
# File 'lib/skyscraper/node.rb', line 67
def follow
if self.href
Skyscraper::fetch(self.uri)
end
end
|
#have_parent? ⇒ Boolean
41
42
43
|
# File 'lib/skyscraper/node.rb', line 41
def have_parent?
self.parent.present?
end
|
#html ⇒ Object
73
74
75
|
# File 'lib/skyscraper/node.rb', line 73
def html
@element.children.to_html
end
|
#original_class ⇒ Object
3
|
# File 'lib/skyscraper/node.rb', line 3
alias :original_class :class
|
#parent ⇒ Object
35
36
37
38
39
|
# File 'lib/skyscraper/node.rb', line 35
def parent
if @element.parent.is_a? Nokogiri::XML::Element
Node.new @element.parent
end
end
|
#parents(selector = nil) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/skyscraper/node.rb', line 45
def parents selector = nil
node = self
parents = []
while node.have_parent?
node = node.parent
parents << node
end
parents.select! do |item|
item.element.matches? selector
end if selector
parents
end
|
#siblings ⇒ Object
61
62
63
64
65
|
# File 'lib/skyscraper/node.rb', line 61
def siblings
self.parent.children.select do |node|
node.element != self.element
end
end
|
#submit(params = {}) ⇒ Object
#tag ⇒ Object
97
98
99
|
# File 'lib/skyscraper/node.rb', line 97
def tag
@element.name
end
|
#text ⇒ Object
93
94
95
|
# File 'lib/skyscraper/node.rb', line 93
def text
@element.content.to_s.strip
end
|
#uri ⇒ Object
85
86
87
|
# File 'lib/skyscraper/node.rb', line 85
def uri
@element.document.path.full_path_for(self.href)
end
|