Sha256: f1ca32bae50d4d259584f4cbfa0d89630c6bfd04942efeed53abc8344db24fd7

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module ProxyFetcher
  # HTML document abstraction class. Used to work with different HTML parser adapters
  # such as Nokogiri, Oga or a custom one. Stores <i>backend</i< that will handle all
  # the DOM manipulation logic.
  class Document
    # @!attribute [r] backend
    #   @return [Object] Backend object that handles DOM processing
    attr_reader :backend

    # Parses raw HTML data to abstract ProxyFetcher document.
    #
    # @param data [String] HTML
    #
    # @return [ProxyFetcher::Document]
    #   ProxyFetcher document model
    #
    def self.parse(data)
      new(ProxyFetcher.config.adapter.parse(data))
    end

    # Initialize abstract ProxyFetcher HTML Document
    #
    # @return [Document]
    #
    def initialize(backend)
      @backend = backend
    end

    # Searches elements by XPath selector.
    #
    # @return [Array<ProxyFetcher::Document::Node>]
    #   collection of nodes
    #
    def xpath(*args)
      backend.xpath(*args).map { |node| backend.proxy_node.new(node) }
    end

    # Searches elements by CSS selector.
    #
    # @return [Array<ProxyFetcher::Document::Node>]
    #   collection of nodes
    #
    def css(*args)
      backend.css(*args).map { |node| backend.proxy_node.new(node) }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
proxy_fetcher-0.10.0 lib/proxy_fetcher/document.rb
proxy_fetcher-0.9.0 lib/proxy_fetcher/document.rb
proxy_fetcher-0.8.0 lib/proxy_fetcher/document.rb
proxy_fetcher-0.7.1 lib/proxy_fetcher/document.rb
proxy_fetcher-0.7.0 lib/proxy_fetcher/document.rb
proxy_fetcher-0.6.5 lib/proxy_fetcher/document.rb
proxy_fetcher-0.6.4 lib/proxy_fetcher/document.rb
proxy_fetcher-0.6.3 lib/proxy_fetcher/document.rb