Class: Html2rss::Html::ArticleExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/html/article_extractor.rb,
lib/html2rss/html/article_extractor/id_generator.rb,
lib/html2rss/html/article_extractor/date_extractor.rb,
lib/html2rss/html/article_extractor/image_extractor.rb,
lib/html2rss/html/article_extractor/heading_extractor.rb,
lib/html2rss/html/article_extractor/category_extractor.rb,
lib/html2rss/html/article_extractor/enclosure_extractor.rb

Overview

ArticleExtractor is responsible for extracting details (headline, url, images, etc.) from an article_tag DOM node. DOM chrome helpers live on Navigator.

Defined Under Namespace

Classes: CategoryExtractor, DateExtractor, EnclosureExtractor, HeadingExtractor, IdGenerator, ImageExtractor

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(article_tag, base_url:, selected_anchor: nil, fallback_anchorless: false) ⇒ ArticleExtractor

Returns a new instance of ArticleExtractor.

Parameters:

  • article_tag (Nokogiri::XML::Node)

    article-like container to extract from

  • base_url (String, Html2rss::Url)

    base url used to resolve relative links

  • selected_anchor (Nokogiri::XML::Node, nil) (defaults to: nil)

    explicit primary anchor for the container

  • fallback_anchorless (Boolean) (defaults to: false)

    whether to fall back to anchorless extraction

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
# File 'lib/html2rss/html/article_extractor.rb', line 28

def initialize(, base_url:, selected_anchor: nil, fallback_anchorless: false)
  raise ArgumentError, 'article_tag is required' unless 

  @article_tag = 
  @base_url = base_url
  @selected_anchor = selected_anchor
  @fallback_anchorless = fallback_anchorless
end

Class Method Details

.call(article_tag, base_url:, selected_anchor: nil, fallback_anchorless: false) ⇒ Hash{Symbol => Object}

Extracts article attributes from a DOM element.

Parameters:

  • article_tag (Nokogiri::XML::Node)

    article-like container to extract from

  • base_url (String, Html2rss::Url)

    base url used to resolve relative links

  • selected_anchor (Nokogiri::XML::Node, nil) (defaults to: nil)

    explicit primary anchor for the container

  • fallback_anchorless (Boolean) (defaults to: false)

    whether to fall back to anchorless extraction

Returns:

  • (Hash{Symbol => Object})

    extracted article attributes



18
19
20
# File 'lib/html2rss/html/article_extractor.rb', line 18

def call(, base_url:, selected_anchor: nil, fallback_anchorless: false)
  new(, base_url:, selected_anchor:, fallback_anchorless:).call
end

Instance Method Details

#callHash{Symbol => Object}

Returns extracted article attributes.

Returns:

  • (Hash{Symbol => Object})

    extracted article attributes



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/html2rss/html/article_extractor.rb', line 38

def call
  {
    title: extract_title,
    url: extract_url,
    image: extract_image,
    description: extract_description,
    id: generate_id,
    published_at: extract_published_at,
    enclosures: extract_enclosures,
    categories: extract_categories
  }
end