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. rubocop:disable Metrics/ClassLength -- leftover re-extract stays with field extractors

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, time_zone: 'UTC') ⇒ 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

  • time_zone (String) (defaults to: 'UTC')

    channel time zone for naive leftover dates

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
# File 'lib/html2rss/html/article_extractor.rb', line 31

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

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

Class Method Details

.call(article_tag, base_url:, selected_anchor: nil, fallback_anchorless: false, time_zone: 'UTC') ⇒ 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

  • time_zone (String) (defaults to: 'UTC')

    channel time zone for naive leftover dates

Returns:

  • (Hash{Symbol => Object})

    extracted article attributes



20
21
22
# File 'lib/html2rss/html/article_extractor.rb', line 20

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

Instance Method Details

#callHash{Symbol => Object}

Returns extracted article attributes.

Returns:

  • (Hash{Symbol => Object})

    extracted article attributes



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/html2rss/html/article_extractor.rb', line 42

def call # rubocop:disable Metrics/MethodLength
  title = extract_title
  lines = leftover_lines
  published_at = extract_published_at(lines)
  source, lines, published_at = parent_card_fields(title, lines, published_at)
  {
    title:,
    url: extract_url,
    image: extract_image,
    description: ArticleRules::Description.from_lines(lines, title:),
    id: generate_id,
    published_at:,
    enclosures: extract_enclosures,
    categories: CategoryExtractor.call(source, title:)
  }
end