Module: HtmlEntry::Page

Defined in:
lib/html_entry/page.rb,
lib/html_entry/page/entity_fetcher.rb,
lib/html_entry/page/values_collector.rb

Overview

Page module

Defined Under Namespace

Classes: EntityFetcher, ValuesCollector

Class Method Summary collapse

Class Method Details

.fetch_node(document, instruction) ⇒ Nokogiri::XML::Element

Get node by XPath or CSS selector

Parameters:

  • document (Nokogiri::HTML::Document)
  • instruction (Hash)

Returns:

  • (Nokogiri::XML::Element)


13
14
15
16
# File 'lib/html_entry/page.rb', line 13

def fetch_node(document, instruction)
  nodes = fetch_nodes(document, instruction)
  nodes.first if nodes
end

.fetch_nodes(document, instruction) ⇒ Nokogiri::XML::NodeSet

Get nodes by XPath or CSS selector

Parameters:

  • document (Nokogiri::HTML::Document|Nokogiri::XML::Element)
  • instruction (Hash)

Returns:

  • (Nokogiri::XML::NodeSet)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/html_entry/page.rb', line 25

def fetch_nodes(document, instruction)
  unless document.instance_of?(Nokogiri::HTML::Document) || document.instance_of?(Nokogiri::XML::Element)
    raise '"document" must be an instance of Nokogiri::HTML::Document.'
  end
  if instruction[:selector]
    document.css(instruction[:selector])
  elsif instruction[:css]
    document.css(instruction[:css])
  elsif instruction[:xpath]
    if defined? document.xpath
      document.xpath(instruction[:xpath])
    else
      raise 'Cannot use this document.'
    end
  end
end