Class: ProxyFetcher::Document::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_fetcher/document/node.rb

Overview

Abstract class for storing HTML elements that was parsed by one of the ProxyFetcher::Document adapters class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Node

Initialize new HTML node



16
17
18
# File 'lib/proxy_fetcher/document/node.rb', line 16

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



10
11
12
# File 'lib/proxy_fetcher/document/node.rb', line 10

def node
  @node
end

Instance Method Details

#at_css(*args) ⇒ ProxyFetcher::Document::Node

Searches exact HTML element by CSS. Returns only one element.

Returns:



44
45
46
# File 'lib/proxy_fetcher/document/node.rb', line 44

def at_css(*args)
  self.class.new(node.at_css(*args))
end

#at_xpath(*args) ⇒ ProxyFetcher::Document::Node

Searches exact HTML element by XPath. Returns only one element.

Returns:



35
36
37
# File 'lib/proxy_fetcher/document/node.rb', line 35

def at_xpath(*args)
  self.class.new(node.at_xpath(*args))
end

#contentObject

Returns HTML node content.

Abstract method, must be implemented for specific adapter class.



61
62
63
# File 'lib/proxy_fetcher/document/node.rb', line 61

def content
  raise "`#{__method__}` must be implemented for specific adapter class!"
end

#content_at(*args) ⇒ String

Returns clean content (text) for the specific element.

Returns:

  • (String)

    HTML node content



53
54
55
# File 'lib/proxy_fetcher/document/node.rb', line 53

def content_at(*args)
  clear(find(*args).content)
end

#find(selector, method = :at_xpath) ⇒ Node

Searches for node in children using some selector (CSS or XPath).

Parameters:

  • selector (String)

    selector (CSS or XPath)

Returns:

  • (Node)

    child node



26
27
28
# File 'lib/proxy_fetcher/document/node.rb', line 26

def find(selector, method = :at_xpath)
  self.class.new(node.public_send(method, selector))
end

#htmlObject

Returns HTML node inner HTML.

Abstract method, must be implemented for specific adapter class.



69
70
71
# File 'lib/proxy_fetcher/document/node.rb', line 69

def html
  raise "`#{__method__}` must be implemented for specific adapter class!"
end