Class: AgentX::HTML

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

Constant Summary collapse

NBSP =
Nokogiri::HTML(' ').text

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ HTML

Returns a new instance of HTML.



5
6
7
8
9
10
11
# File 'lib/agentx/html.rb', line 5

def initialize(html)
  if html.kind_of?(String)
    @html = Nokogiri::HTML(html)
  else
    @html = html
  end
end

Instance Method Details

#[](attr) ⇒ Object



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

def [](attr)
  @html[attr.to_s]
end

#all(selector) ⇒ Object



21
22
23
# File 'lib/agentx/html.rb', line 21

def all(selector)
  @html.css(selector).map { |e| HTML.new(e) }
end

#attributesObject



41
42
43
44
45
# File 'lib/agentx/html.rb', line 41

def attributes
  h = {}
  @html.attribute_nodes.each { |n| h[n.name] = n.value }
  h
end

#childrenObject



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

def children
  @html.children.map { |e| HTML.new(e) }
end

#first(selector) ⇒ Object



17
18
19
# File 'lib/agentx/html.rb', line 17

def first(selector)
  (e = @html.css(selector).first) && HTML.new(e)
end

#form_to_hash(opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/agentx/html.rb', line 51

def form_to_hash(opts={})
  h = {}
  all('input').each do |input|
    h[input['name']] = input['value']
  end
  opts.each do |k,v|
    h[k.to_s] = v
  end
  h
end

#inspectObject



66
67
68
# File 'lib/agentx/html.rb', line 66

def inspect
  to_html
end

#nextObject



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

def next
  HTML.new(@html.next)
end

#parentObject



25
26
27
# File 'lib/agentx/html.rb', line 25

def parent
  HTML.new(@html.parent)
end

#previousObject



37
38
39
# File 'lib/agentx/html.rb', line 37

def previous
  HTML.new(@html.previous)
end

#textObject



76
77
78
# File 'lib/agentx/html.rb', line 76

def text
  @html.text.gsub(NBSP, ' ')
end

#to_htmlObject



13
14
15
# File 'lib/agentx/html.rb', line 13

def to_html
  @html.to_html
end

#to_nokogiriObject



70
71
72
# File 'lib/agentx/html.rb', line 70

def to_nokogiri
  @html
end

#to_sObject



62
63
64
# File 'lib/agentx/html.rb', line 62

def to_s
  to_html
end