Class: RubyCheerio
- Inherits:
-
Object
- Object
- RubyCheerio
- Defined in:
- lib/ruby-cheerio.rb
Instance Attribute Summary collapse
-
#html ⇒ Object
This creates an attribute readers and writers needed for the instance variables.
-
#text ⇒ Object
This creates an attribute readers and writers needed for the instance variables.
Instance Method Summary collapse
-
#find(selector = nil) ⇒ Object
This method can find an element using element-name, class-name, id-name or all together.
-
#initialize(html = nil) ⇒ RubyCheerio
constructor
This method defines the instance of a RubyCheerio object with attributes like text, html and if an argument is not passed, it throws an ArgumentError.
-
#prop(selector, attribute) ⇒ Object
This method can return the property of an element, which is selected using the selector being passed.
Constructor Details
#initialize(html = nil) ⇒ RubyCheerio
This method defines the instance of a RubyCheerio object with attributes like text, html and if an argument is not passed, it throws an ArgumentError
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby-cheerio.rb', line 15 def initialize html=nil if valid_args? html if html.instance_of? Nokogiri::HTML::Document @html_nokogiri = html elsif html.instance_of? String and !html.empty? html = html.strip @html_nokogiri = Nokogiri::HTML(html) end @text = @html_nokogiri.text @html = Nokogiri::HTML(@html_nokogiri.to_html).css('body').inner_html end end |
Instance Attribute Details
#html ⇒ Object
This creates an attribute readers and writers needed for the instance variables.
10 11 12 |
# File 'lib/ruby-cheerio.rb', line 10 def html @html end |
#text ⇒ Object
This creates an attribute readers and writers needed for the instance variables.
10 11 12 |
# File 'lib/ruby-cheerio.rb', line 10 def text @text end |
Instance Method Details
#find(selector = nil) ⇒ Object
This method can find an element using element-name, class-name, id-name or all together. This works like finding elements using jQuery styled selectors.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruby-cheerio.rb', line 31 def find selector=nil if valid_args? selector elements_list = @html_nokogiri.css(selector).to_a.map{|e| self.class.new(e.to_html)} if !elements_list.nil? return elements_list else return nil end end end |
#prop(selector, attribute) ⇒ Object
This method can return the property of an element, which is selected using the selector being passed.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby-cheerio.rb', line 45 def prop(selector, attribute) if valid_args?(selector, attribute) element = @html_nokogiri.css(selector) if element.instance_of? Nokogiri::XML::NodeSet return element[0][attribute] elsif element.instance_of? Nokogiri::XML::Element return element[attribute] else return nil end end end |