Class: AgentX::HTML
- Inherits:
-
Object
- Object
- AgentX::HTML
- Defined in:
- lib/agentx/html.rb
Constant Summary collapse
- NBSP =
Nokogiri::HTML(' ').text
Instance Method Summary collapse
- #[](attr) ⇒ Object
- #all(selector) ⇒ Object
- #attributes ⇒ Object
- #children ⇒ Object
- #first(selector) ⇒ Object
- #form_to_hash(opts = {}) ⇒ Object
-
#initialize(html) ⇒ HTML
constructor
A new instance of HTML.
- #inspect ⇒ Object
- #next ⇒ Object
- #parent ⇒ Object
- #previous ⇒ Object
- #text ⇒ Object
- #to_html ⇒ Object
- #to_nokogiri ⇒ Object
- #to_s ⇒ Object
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 |
#attributes ⇒ Object
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 |
#children ⇒ Object
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 |
#inspect ⇒ Object
66 67 68 |
# File 'lib/agentx/html.rb', line 66 def inspect to_html end |
#parent ⇒ Object
25 26 27 |
# File 'lib/agentx/html.rb', line 25 def parent HTML.new(@html.parent) end |
#previous ⇒ Object
37 38 39 |
# File 'lib/agentx/html.rb', line 37 def previous HTML.new(@html.previous) end |
#text ⇒ Object
76 77 78 |
# File 'lib/agentx/html.rb', line 76 def text @html.text.gsub(NBSP, ' ') end |
#to_html ⇒ Object
13 14 15 |
# File 'lib/agentx/html.rb', line 13 def to_html @html.to_html end |
#to_nokogiri ⇒ Object
70 71 72 |
# File 'lib/agentx/html.rb', line 70 def to_nokogiri @html end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/agentx/html.rb', line 62 def to_s to_html end |