Sha256: c195935c01091366b788b6c22501b43ede3f86018658ae802e8f1df1ac8b5f05

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require 'nokogiri'

module MetaInspector
  ##
  # Parses the document with Nokogiri.
  #
  # Delegates the parsing of the different elements to specialized parsers,
  # passing itself as a reference for coordination purposes
  #
  class Parser
    include MetaInspector::Exceptionable

    def initialize(document, options = {})
      @document        = document
      @exception_log   = options[:exception_log]
      @head_links_parser = MetaInspector::Parsers::HeadLinksParser.new(self)
      @meta_tag_parser = MetaInspector::Parsers::MetaTagsParser.new(self)
      @links_parser    = MetaInspector::Parsers::LinksParser.new(self)
      @download_images = options[:download_images]
      @images_parser   = MetaInspector::Parsers::ImagesParser.new(self, download_images: @download_images)
      @texts_parser    = MetaInspector::Parsers::TextsParser.new(self)

      parsed           # parse early so we can fail early
    end

    extend Forwardable
    delegate [:url, :scheme, :host]                          => :@document
    delegate [:meta_tags, :meta_tag, :meta, :charset]        => :@meta_tag_parser
    delegate [:head_links, :stylesheets, :canonicals, :feed] => :@head_links_parser
    delegate [:links, :base_url]                             => :@links_parser
    delegate :images                                         => :@images_parser
    delegate [:title, :best_title, :description]             => :@texts_parser

    # Returns the whole parsed document
    def parsed
      @parsed ||= Nokogiri::HTML(@document.to_s)
    rescue Exception => e
      @exception_log << e
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
metainspector-4.7.2 lib/meta_inspector/parser.rb
metainspector-4.7.1 lib/meta_inspector/parser.rb