Class: Html2rss::Html::SstArticleExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/html/sst_article_extractor.rb

Overview

Builds an Article from an SST segment / ranked segment. Port of Html::ArticleExtractor field logic onto SST::Node. rubocop:disable Metrics/ClassLength -- SST field extractors colocated for parity with ArticleExtractor

Constant Summary collapse

KICKER_CLASS_PATTERN =

CSS class tokens that mark kicker / eyebrow text (excluded from titles).

/kicker|eyebrow|pre-title|pretitle|overline/i
FALLBACK_HEADING_NAMES =

Inline emphasis tags used as title fallbacks when no heading exists.

%i[strong b].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ranked_or_segment, base_url:, scraper: nil, fallback_anchorless: false) ⇒ SstArticleExtractor

Returns a new instance of SstArticleExtractor.

Parameters:

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/html2rss/html/sst_article_extractor.rb', line 33

def initialize(ranked_or_segment, base_url:, scraper: nil, fallback_anchorless: false)
  segment = ranked_or_segment.is_a?(Scoring::RankedSegment) ? ranked_or_segment.segment : ranked_or_segment
  raise ArgumentError, 'segment is required' unless segment.is_a?(AutoSource::Segment)

  @segment = segment
  @root = segment.root_node
  @selected_anchor = segment.primary_link
  @base_url = base_url
  @scraper = scraper
  @fallback_anchorless = fallback_anchorless
end

Class Method Details

.call(ranked_or_segment, base_url:, scraper: nil, fallback_anchorless: false) ⇒ Html2rss::Article?

Parameters:

Returns:



24
25
26
# File 'lib/html2rss/html/sst_article_extractor.rb', line 24

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

Instance Method Details

#callHtml2rss::Article?

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/html2rss/html/sst_article_extractor.rb', line 47

def call # rubocop:disable Metrics/MethodLength
  attrs = {
    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,
    scraper: @scraper
  }
  article = Article.new(**attrs)
  article.valid? ? article : nil
end